160 likes | 254 Views
Distributed Systems. Agenda. The Java Language Threads III Advantages of threads… Thread Methods Interrupting Threads. Threads. The ability to do multiple things at once within the same application Lightweight Easy to create and destroy Shared address space
E N D
Agenda The Java Language Threads III Advantages of threads… Thread Methods Interrupting Threads
Threads • The ability to do multiple things at once within the same application • Lightweight • Easy to create and destroy • Shared address space • Can share memory variables directly • May require more complex synchronization logic because of shared address space
Advantages of threads… • Use multiple processors • Code is partitioned in order to be able to use n processors at once • Hide network/disk latency • While one thread is waiting for something, run the others • Dramatic improvements even with a single CPU • Need to efficiently block the connections that are waiting, while doing useful work with the data that has arrived • Writing good network codes relies on concurrency! • Keeping the GUI responsive • Separate worker threads from GUI thread
Java Threads • Java includes built-in support for threading! • VM transparently maps threads in Java to OS threads • Allows threads in Java to take advantage of hardware and operating system level • Keeps track of threads and schedules them to get CPU time
Java Thread class • A Thread is just another object in Java • It has an address, responds to messages etc. • Class Thread • in the default java.lang package • A Thread object in Java is a token which represents a thread of control in the VM • We send messages to the Thread object; the VM interprets these messages and does the appropriate operations on the underlying threads in the OS
Thread Methods • Thread.currentThread() • Static method • Returns a pointer to the Thread object for the current running thread • Warning! • They always affect the running thread
Thread.sleep(), Thread.yield() • Thread.sleep(milliseconds) • Blocks the current thread for approximately the given number of milliseconds • Thread.yield() • Voluntarily give up the CPU so that another thread may run • A hint to the VM, not guaranteed
Thread Priorities • getPriority() and setPriority() on Thread objects • Used to optimize behavior • Some VMs ignore priorities
getName() • Returns the String name of the Thread • Useful when debugging and printing out the name of the thread • Thread-1, Thread-2 etc. • Thread class constructor takes a string argument which sets the name of the thread!
Stop() -- deprecated • stop() • Performs a synchronous stop of the thread • Usually impossible to ensure that the object is left in a consistent state when using stop
Joining • Used when a thread wants to wait for another thread to complete its run() • Sent the thread2.join() message •Causes the current running thread to block efficiently until thread2 finishes its run() method
Join Example // create a thread Runnable runner = new Runnable() { public void run() { // do something in a loop }; Thread t = new Thread(runnner); // start a thread t.start(); // at this point, two threads may be running --me and t // wait for t to complete its run try { t.join(); } catch (InterruptedExceptionignored) {} // now t is done (or we were interrupted)
Thread Interruption • interrupt() • Signal a thread object that it should stop running • Asynchronous notification • Does not stop the thread right away • Sets an “interrupted” boolean to true • Thread must check and do appropriate thing • isInterrupted() • Checks to see if a interrupt has been requested
Interrupting Threads • Notice that interrupting a thread, using the interrupt() method, does not automatically mean that the thread should terminate. It simply grabs the attention of the thread. • In order to accomplish this functionality, place code that will decide how to react to the interruption inside the catch clause of the run() method.
Interrupting Threads • There is one problem with this - if the thread is interrupted when it is not sleeping or waiting, no InterruptedException is generated. Thus, the thread must check to make sure it has not been interrupted during the main execution loop. • This is simple by calling the interrupted() method, which returns true if the thread has been interrupted. while (!interrupted() && more work to do) { // main thread execution loop }