Learning Java 3 – Threads and Synchronization


Published on

Basic introduction to threads and synchronization in Java.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Learning Java 3 – Threads and Synchronization

  1. 1. Learning Java 3 –Threads, Synchronization Christopher Swenson Center for Information Security University of Tulsa 600 S. College Ave Tulsa, OK 74104
  2. 2. Overview <ul><li>Threads </li></ul><ul><li>Synchronization of Threads </li></ul>
  3. 3. Threads <ul><li>Everything up to now has been single processing: only one thing was executing at once </li></ul><ul><li>Threads allow multiple things to execute simultaneously </li></ul><ul><li>Most of what you need is in the Thread class </li></ul>
  4. 4. How to Make a Thread <ul><li>You need to make a public void run() function </li></ul><ul><li>Two options </li></ul><ul><ul><li>Class that implements Runnable </li></ul></ul><ul><ul><li>Class that extends Thread </li></ul></ul><ul><ul><ul><li>Anonymous inner class </li></ul></ul></ul><ul><li>t = new Thread( Runnable r); </li></ul><ul><li>t = new MyThread(); </li></ul>
  5. 5. Starting/Stopping <ul><li>Say we have a Thread t </li></ul><ul><li>t.start() – starts executing the Thread’s run() </li></ul><ul><li>t.interrupt() – interrupts a Thread </li></ul><ul><li>t.join() – waits for Thread to die </li></ul>
  6. 6. Example <ul><li>Thread t1 = new Thread(task1); </li></ul><ul><li>Thread t2 = new Thread(task2); </li></ul><ul><li>t1.start(); t2.start(); </li></ul><ul><li>t1.join(); t2.join(); </li></ul>
  7. 7. Useful Thread Methods <ul><li>Thead.sleep( long millis[, int nanos]); </li></ul><ul><ul><li>Causes current executing Thread to sleep </li></ul></ul><ul><li>Thread.yield(); </li></ul><ul><ul><li>Causes current executing Thread to be put back on the ready list </li></ul></ul>
  8. 8. Anonymous Inner Classes <ul><li>Quick hack to start a new Thread </li></ul><ul><li>Inner classes are bound to an instance of the enclosing class (so can access its variables) </li></ul><ul><li>Be careful with them </li></ul><ul><li>Thread t1 = new Thread() { </li></ul><ul><li>public void run() { </li></ul><ul><li>// Do stuff </li></ul><ul><li>} }; </li></ul><ul><li>t1.start(); </li></ul>
  9. 9. Clobbering Time! <ul><li>So now we have multiple Threads going at once, but they can trample each other’s feet </li></ul><ul><ul><li>Writing to the same variable at the same time </li></ul></ul><ul><ul><li>Reading and writing at the same time where order matters </li></ul></ul><ul><li>Some objects are inherently thread-safe (or “synchronized”) </li></ul><ul><ul><li>Vector, Hashtable, Collections.synchronizedList(List l) </li></ul></ul><ul><li>Synchronization is the key </li></ul>
  10. 10. Locks <ul><li>Locks are the primitive for doing synchronization in Java </li></ul><ul><li>Every Object is a lock </li></ul><ul><li>Two methods that have code that is synchronized on the same lock (object) cannot execute that code at the same time </li></ul>
  11. 11. Example <ul><li>ArrayList<Integer> list = new ArrayList<Integer>(); </li></ul><ul><li>//… </li></ul><ul><li>synchronized (list) </li></ul><ul><li>{ </li></ul><ul><li>list.add(4); </li></ul><ul><li>} </li></ul>
  12. 12. Synchronized methods <ul><li>If an method is synchronized, then the entire code is wrapped in an implicit synchronized (this) { … } </li></ul><ul><li>They will all be synchronized to their instance </li></ul><ul><li>Only one synchronized method of an instance of an object can be running at one time </li></ul>
  13. 13. Waiting, Notifying <ul><li>Well, what if I am waiting for something, but it may be a while? </li></ul><ul><li>Must hold the lock of the Object you are waiting / notifying on (which will be released and reacquired as necessary) </li></ul><ul><li>Object bufferFull = new Object(); </li></ul><ul><li>//… the rest should be synchronized on buffer </li></ul><ul><li>while (buffer.size() == 10) arrayFull.wait(); </li></ul><ul><li>//… </li></ul><ul><li>// notify one thread </li></ul><ul><li>buffer.remove(); </li></ul><ul><li>arrayFull.notify(); </li></ul><ul><li>// or notify everyone! </li></ul><ul><li>arrayFull.notifyAll(); </li></ul>
  14. 14. Advanced Synchronization <ul><li>Java 1.5 adds many shiny new synchronization mechanisms (java.util.concurrent.*) </li></ul><ul><li>Lock provides non-blocking access to code </li></ul><ul><ul><li>ReentrantLock </li></ul></ul><ul><li>Condition provides an implementation of condition variables </li></ul><ul><ul><li>Associated with a Lock </li></ul></ul><ul><li>Semaphore </li></ul><ul><li>AtomicInteger , etc </li></ul>