1 / 16

Distributed Systems

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

fedora
Télécharger la présentation

Distributed Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Distributed Systems

  2. Agenda The Java Language Threads III Advantages of threads… Thread Methods Interrupting Threads

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Thread Priorities • getPriority() and setPriority() on Thread objects • Used to optimize behavior • Some VMs ignore priorities

  10. 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!

  11. 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

  12. 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

  13. 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)

  14. 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

  15. 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.

  16. 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 }

More Related