Multithreading In Java


Published on

Published in: Technology

Multithreading In Java

  1. 1. Multithreading <ul><ul><li>Parag Shah </li></ul></ul><ul><ul><li>Adaptive Software Solutions </li></ul></ul><ul><ul><li> </li></ul></ul>
  2. 2. Agenda <ul><li>Multithreading concepts </li></ul><ul><li>Creating threads </li></ul><ul><li>Controlling threads </li></ul><ul><li>Thread states </li></ul><ul><li>Summary </li></ul>
  3. 3. Multithreading concepts <ul><li>Multithreading concepts </li></ul><ul><li>Creating threads </li></ul><ul><li>Controlling threads </li></ul><ul><li>Thread states </li></ul><ul><li>Summary </li></ul>
  4. 4. Multithreading concepts <ul><li>Need for concurrency </li></ul><ul><li>Multi processing </li></ul><ul><li>Multi threading </li></ul><ul><li>Multi threading makes user interfaces more responsive </li></ul><ul><li>Multi threading makes better utilization of time when the program is blocking for IO </li></ul>
  5. 5. Creating Threads <ul><li>A thread represents code that will be run concurrently with other code in the same process </li></ul><ul><li>Creating a thread </li></ul><ul><ul><li>Use the Thread class </li></ul></ul><ul><ul><li>Use the Runnable interface </li></ul></ul><ul><li>Pros and Cons </li></ul><ul><ul><li>Extending thread is simpler </li></ul></ul><ul><ul><li>Implementing Runnable allows subclassing other classes </li></ul></ul>
  6. 6. Creating Threads – Extending Thread <ul><li>Extend java.lang.Thread </li></ul><ul><li>Implement the run method </li></ul><ul><li>See [] </li></ul>
  7. 7. Creating Threads – Implementing Runnable <ul><li>Extend java.lang.Thread </li></ul><ul><li>Implement the run mthod </li></ul><ul><li>See [] </li></ul><ul><li>A Runnable cannot throw a checked exception </li></ul>
  8. 8. Thread Priorities <ul><li>Priorities are between 1 – 10 </li></ul><ul><li>Best to use the constants defined in Thread </li></ul><ul><ul><li>MAX_PRIORITY </li></ul></ul><ul><ul><li>MIN_PRIORITY </li></ul></ul><ul><ul><li>NORM_PRIORITY </li></ul></ul><ul><li>Uses of thread priority </li></ul>
  9. 9. States Of A Thread <ul><li>A thread can be in any one of these 4 states </li></ul><ul><ul><li>New: The thread object has been created but it has not been initialized </li></ul></ul><ul><ul><li>Runnable: The thread object is ready to run whenever the CPU gives it a time slot </li></ul></ul><ul><ul><li>Blocked: The thread could be run, but is currently not in a runnable state </li></ul></ul><ul><ul><li>Dead: The thread has terminated </li></ul></ul>
  10. 10. Controlling Thread Behavior <ul><li>Yielding </li></ul><ul><li>Sleeping </li></ul><ul><li>Setting Priorities </li></ul><ul><li>Joining </li></ul>
  11. 11. Controlling Thread Behavior Yielding <ul><li>A thread can be made to yield when it comes to a point when the CPU can be relinquished to another thread </li></ul><ul><li>Yielding is done by calling the yield() method </li></ul><ul><li>A call to yield() is a hint to the underlying operating system. It does not guarantee that the CPU will be immediately given to another thread </li></ul><ul><li>See [] </li></ul>
  12. 12. Controlling Thread Behavior Sleeping <ul><li>A thread can be put to sleep for xxx ms </li></ul><ul><li>Use the sleep(long x) call on the thread to put it to sleep for x ms </li></ul><ul><li>The thread is guaranteed to sleep for x ms </li></ul><ul><ul><li>It may take more than that for the scheduler to give it control </li></ul></ul><ul><ul><li>It may be interrupted by another thread </li></ul></ul><ul><li>See [] </li></ul>
  13. 13. Controlling Thread Behavior Priorities <ul><li>Every thread can have a priority </li></ul><ul><li>Scheduler will choose thread with highest priority for running first </li></ul><ul><li>Exact mechanism of choosing the thread is indeterminate </li></ul><ul><li>JDK supports 10 priority levels </li></ul><ul><ul><li>For portability it is best to use one of the predefined (MAX_PRIORITY, NORM_PRIORITY, MIN_PRIORITY) priority levels </li></ul></ul>
  14. 14. Controlling Thread Behavior Joining Another Thread <ul><li>Joining a thread is the process of suspending till the other thread completes execution </li></ul><ul><li>Join can be called with or without a timeout </li></ul><ul><li>Exercise </li></ul>
  15. 15. Some Deprecated Methods <ul><li>Deprecated methods </li></ul><ul><ul><li>suspend & resume </li></ul></ul><ul><ul><li>stop & destroy </li></ul></ul><ul><li>These methods are deadlock prone </li></ul><ul><li>An alternate mechanism for stopping threads </li></ul>
  16. 16. Sharing Resources In A Multithreaded Environment <ul><li>Multiple threads may try to access the same resource </li></ul><ul><li>The point at which a thread will be suspended and another will resume is indeterminate </li></ul><ul><li>A thread may not have finished totally processing a data structure before it is suspended. </li></ul><ul><ul><li>Another thread accessing the same data structure may find it in an improper state </li></ul></ul>
  17. 17. Controlling Access To Shared Resources <ul><li>Put the shared resource in an object </li></ul><ul><li>Make all methods in the object which access the shared resource, synchronized </li></ul><ul><ul><li>Every object has a lock (monitor) </li></ul></ul><ul><ul><li>Before a thread enters a synchronized method it must obtain the lock </li></ul></ul><ul><ul><li>The lock is released when the thread leaves the synchronized method </li></ul></ul><ul><ul><li>Only one thread can have a lock to an object at a time </li></ul></ul><ul><li>Synchronize a critical section on an object </li></ul>
  18. 18. Atomic Operations <ul><li>Atomic operations are those operations that are guaranteed to be completed before a context switch </li></ul><ul><li>Simple assignments and 'returning values' are some atomic operations </li></ul><ul><ul><li>An exception is when dealing with double and long values </li></ul></ul><ul><li>Be careful while omitting the synchronized keyword when multiple methods are accessing the shared resource </li></ul>
  19. 19. Thread Blocking <ul><li>A thread can be blocked due to one of the following reasons </li></ul><ul><ul><li>It is waiting for I/O </li></ul></ul><ul><ul><li>It has called its sleep(long ms) </li></ul></ul><ul><ul><li>It has called the wait method </li></ul></ul><ul><ul><li>The thread has called join on another thread </li></ul></ul><ul><ul><li>The thread has called a synchronized method/block on another object and the lock is unavailable </li></ul></ul>
  20. 20. Using wait() and notify() with threads <ul><li>sleep(long ms) does not release the locks held by that thread </li></ul><ul><li>Use wait() with notify() or notifyAll() instead </li></ul><ul><li>See [] </li></ul>
  21. 21. Deadlock <ul><li>What is deadlock </li></ul><ul><li>The dining philosophers problem </li></ul><ul><li>Conditions for a deadlock to occur </li></ul><ul><ul><li>At least one resource used by threads is not shareable </li></ul></ul><ul><ul><li>At least one thread holds a resource and is waiting on another resource which is being held by another thread </li></ul></ul><ul><ul><li>A resource cannot be taken away from a thread </li></ul></ul><ul><ul><li>A circular wait happens </li></ul></ul>
  22. 22. Threading In JDK 1.5 <ul><li>JDK 1.5 has increased support for threading </li></ul><ul><li>Has a new concurrency packages </li></ul><ul><ul><li>java.util.concurrent </li></ul></ul><ul><ul><li>java.util.concurrent.atomic </li></ul></ul><ul><ul><li>java.util.concurrent.locks </li></ul></ul>
  23. 23. Summary <ul><li>It is important to understand when to use threads </li></ul><ul><li>Caution must be exercised while writing multi-threaded code </li></ul><ul><ul><li>Thorough testing is recommended </li></ul></ul><ul><li>It is important to understand which classes and methods should be made thread safe </li></ul>
  24. 24. Where to Get More Information <ul><li>Bruce Eckel's – Thinking In Java </li></ul><ul><li>Taming Java Threads – Allen Holub </li></ul>