Threads ======= Overview What is a thread? Comparison to a process Why are threads of use? What are their drawbacks Scheduling thread lifecycle New, Runnable, Blocked, Terminated why can a thread be blocked? can a thread be runnable, but not running? thread scheduler decides which thread to run next a series of particular choices is called a "schedule" multi-processor scheduling vs. single-processor scheduling what is a "pre-emptive" scheduler? Safety and Liveness What are safety and liveness? How do these two interact? What is a race condition? If your program has a race condition, will it always manifest a bug? What conditions are necessary to have a storage conflict? in terms of data mutability, sharing, and read/write access What is syncrhonization? What is locking? How does locking avoid race conditions? What is deadlock? How does a wait-graph illustrate deadlock? What is a good invariant to maintain that avoids deadlock? State Dependence What is state dependence? What are some policies for ensuring it? Balking, waiting, timeouts, trying/retrying, inaction Guarding What's the difference between using wait/notify and busywaiting? Why is wait/notify preferred? Timeouts Optimistic policies: trying key idea: operate on a *copy* of the data without lock, but make sure the original hasn't changed when I go to store the result. Which of the above (balking, guarding, timeouts, trying) can be combined in the same class? Thread creation patterns autonomous loops oneway messages thread-per-activity do it in the current thread thread-per-message do it in a new thread thread-per-object pass a message to be handled by a per-object thread thread pools generalize: > 1 thread per object requires a "channel" what is the impact of unbounded channel bounded channel, "leaky" bounded channel channel with timeout or failure when full Java Threads Creation subclassing, Runnable passing parameters to threads How to make a non-threaded (synchronous) program multi-threaded? see Alarm code What is a daemon thread? What is the relationship between a synchronized block and a synchronized method? What is an object's lock-state? Owner, count, wait-set. What is each used for? Why do you need to hold the lock when calling wait/notify? How to kill a thread? Why Thread.kill() deprecated? Interruption Thread.isInterrupted(), Thread.interrupt(), ... InterruptedException Java Memory Model Synchronization not just about mutual exclusion: ordering and visibility What does "volatile" do? RMI === Remote reference to an object that lives in another JVM local "stub" object takes care of communication between the local and remote machine stub must implement the same interface as the remote object interface must extend "Remote" all methods must throw RemoteException (why?) stubs generated by rmic compiler actual object extends java.rmi.server.UnicastRemoteObject Remote method invocation Arguments to method call are "marshalled" by the stub Serializable and primitive arguments are copied Remote arguments have stubs sent instead Arguments unmarshalled on remote side to complete call there What are the consequences of using Remote vs. Serializable arguments? Code is not sent in a remote call What if the remote side doesn't have the needed classfile? Must get it from the codebase of the JVM that owns the object Codebase URL stored in an object's stub, so that if other JVM receives, it knows where to go Downloading code dictated by Security Policy of local machine RMI Registry for "bootstrapping" allows one JVM to look up a remote object by name Special Topics ============== Swing GUIs made up of "components" containers (like windows, dialogs, etc.) atomic components (like buttons, sliders, etc.) Components are event-driven register a handler with the component when an event is generated for that component, the handler is called user presses a button Scalable Servers Benchmarking methodology measure, evaluate, tune Server architectures single-threaded multi-threaded thread pools Reflection class hierarchy Class Member, Method, Field, Constructor Sample applications interpretation/configuration serialization adapting to environments