THREADS
Example -1
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : 0 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference ref1 “Jamaica” ---------------- sleep run setName getName : 0 Jamaica 401 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 DONE! Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
public class TwoThreadsTest { public class SimpleThread extends Thread { int st; public SimpleThread(String str) super(str); } public void run() for (int i = 0; i < 10; i++) System.out.print(" "+ i + " " + getName()); try st=(int) (Math.random() * 1000); System.out.println( " " + st); sleep(st); catch (InterruptedException e) System.out.println("DONE! " + getName()); reference reference ref1 ref2 “Jamaica” ---------------- sleep run setName getName : “Fijir” ---------------- sleep run setName getName : 0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 DONE! Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
0 Jamaica 401 0 Fiji 492 1 Jamaica 947 1 Fiji 937 2 Jamaica 97 2 Fiji 447 3 Jamaica 598 3 Fiji 986 4 Jamaica 699 5 Jamaica 164 4 Fiji 482 6 Jamaica 796 5 Fiji 144 6 Fiji 597 7 Jamaica 853 7 Fiji 522 8 Jamaica 152 8 Fiji 582 9 Jamaica 325 DONE! Jamaica 9 Fiji 373 DONE! Fiji
Example -2
class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference thread1 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }
class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }
class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : reference public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); } thread3 “thread3” Sleeptime=33617 ---------------- sleep run setName getName :
class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : reference reference public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); } thread3 thread4 “thread3” Sleeptime=33617 ---------------- sleep run setName getName : “thread4” Sleeptime=15264 ---------------- sleep run setName getName :
class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); reference reference thread1 thread2 “thread1” Sleeptime=12708 ---------------- sleep run setName getName : “thread2” Sleeptime=18411 ---------------- sleep run setName getName : reference reference public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); } thread3 thread4 “thread3” Sleeptime=33617 ---------------- sleep run setName getName : “thread4” Sleeptime=15264 ---------------- sleep run setName getName :
Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 Name: thread3; sleep: 33617 Name: thread4; sleep: 15264 Starting threads public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }
Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 Name: thread3; sleep: 33617 Name: thread4; sleep: 15264 Starting threads thread1 going to sleep Threads started thread2 going to sleep thread3 going to sleep thread4 going to sleep public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }
Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 class PrintThread extends Thread { private int sleepTime; public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * 50000 ); System.err.println( "Name: " + getName( ) + "; sleep: " + sleepTime ); } public void run( ) try System.err.println( getName( ) + " going to sleep" ); Thread.sleep( sleepTime ); catch ( InterruptedException interruptedException ) System.err.println( interruptedException.toString() ); System.err.println( getName( ) + " done sleeping" ); Name: thread1; sleep: 12708 Name: thread2; sleep: 18411 Name: thread3; sleep: 33617 Name: thread4; sleep: 15264 Starting threads thread1 going to sleep Threads started thread2 going to sleep thread3 going to sleep thread4 going to sleep thread1 done sleeping thread4 done sleeping thread2 done sleeping thread3 done sleeping public class ThreadTester { public static void main( String args[] ) PrintThread thread1, thread2, thread3, thread4; thread1 = new PrintThread( "thread1" ); thread2 = new PrintThread( "thread2" ); thread3 = new PrintThread( "thread3" ); thread4 = new PrintThread( "thread4" ); System.err.println( "\nStarting threads" ); thread1.start(); thread2.start(); thread3.start(); thread4.start(); System.err.println( "Threads started\n" ); }
Example -3
-1 2498 1094 Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 52 Terminating Consumer sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) sum
public class SharedCell { public static void main( String args[] ) reference sObject reference public class SharedCell { public static void main( String args[] ) Unsyn sObject = new Unsyn(); Producer p = new Producer( sObject ); Consumer c= new Consumer( sObject ); p.start(); c.start(); } sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName :
p.start(); public class Producer extends Thread { reference public class Producer extends Thread { private Unsync sObject; public Producer( Unsyn s ) super( "Producer" ); sObject = s; } public void run() for ( int count = 1; count <= 10; count++ ) try Thread.sleep( ( int ) ( Math.random() * 3000 ) ); catch( InterruptedException exception ) System.err.println( exception.toString() ); sObject.setSharedInt( count ); System.err.println(getName() + " finished producing values" + "\nTerminating " + getName() ); sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } p.start();
c.start(); public class Consumer extends Thread { reference public class Consumer extends Thread { private Unsyn sObject; public Consumer( Unsyn s) { super( "Consumer" ); sObject = s; } public void run() int value, sum = 0; do { try { Thread.sleep( (int) ( Math.random() * 3000 ) ); catch( InterruptedException exception ) { System.err.println( exception.toString() ); value = sObject.getSharedInt(); sum += value; } while ( value != 10 ); System.err.println( getName() + " retrieved values totaling: “ + sum + "\nTerminating " + getName() ); sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) c.start(); sum
public class Unsyn { private int sharedInt = -1; public void setSharedInt( int value ) System.err.println( Thread.currentThread().getName() + " setting sharedInt to " + value ); sharedInt = value; } public int getSharedInt() System.err.println( Thread.currentThread().getName() + " retrieving sharedInt value " + sharedInt ); return sharedInt;
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) -1 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) -1 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 1 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 2 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 2 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 2 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 3 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 2 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 4 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 2 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 4 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 6 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 5 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 6 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 5 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 11 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 6 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 11 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 6 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 17 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 7 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 17 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 8 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 17 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 8 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 25 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 8 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 33 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 9 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 33 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 9 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 42 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 10 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 42 sum
Consumer retrieving sharedInt value -1 1727 reference 2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 52 Terminating Consumer sObject reference sharedInt=-1 ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell 10 Thread (p) for count=1 to 10 { sleeping random time and each time put setShsreInt=count } Thread (c) do { sleeping random time and each time put value=getShsreInt } While (value!=10) 52 sum
2498 1094 Consumer retrieving sharedInt value -1 1727 Producer setting sharedInt to 1 1202 Consumer retrieving sharedInt value 1 2582 Producer setting sharedInt to 2 2822 Consumer retrieving sharedInt value 2 2681 Producer setting sharedInt to 3 1334 Producer setting sharedInt to 4 1760 Consumer retrieving sharedInt value 4 2825 Producer setting sharedInt to 5 2032 Consumer retrieving sharedInt value 5 950 Producer setting sharedInt to 6 526 Consumer retrieving sharedInt value 6 1400 Producer setting sharedInt to 7 657 Producer setting sharedInt to 8 1200 Consumer retrieving sharedInt value 8 180 Consumer retrieving sharedInt value 8 796 Producer setting sharedInt to 9 1123 Consumer retrieving sharedInt value 9 2902 Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 52 Terminating Consumer
Example -4
-1 true reference sObject reference sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt reference p “Producer” sObject ---------------- sleep run setName getName : c “Consumer” sObject ---------------- sleep run setName getName : sharedcell -1 writeablel true Thread (p) for count=1 to 10 { sleeping random time and each time and setShsreInt=count } Thread (c) do { sleeping random time and each time and value=getShsreInt } While (value!=10) sum
Thread ( c ) first wake-up reference T sleep P : 2784 and T sleep C : 388 sObject reference reference Two threads begin to run and going to sleep deferent times 2784 and 388 sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Thread ( c ) first wake-up “Consumer” sObject ---------------- sleep run setName getName : Calling getSharedInt Synchronized getSharedInt running sharedcell -1 if (Writeable=true) -> watting to become T sleep P=2784 writeablel true Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum
Thread ( c ) first wake-up reference T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 sObject reference reference Two threads begin to run and going to sleep deferent times 2784 and 388 sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Thread ( c ) first wake-up “Consumer” sObject ---------------- sleep run setName getName : Calling getSharedInt Synchronized getSharedInt running sharedcell 1 if (Writeable=true) -> watting to become T sleep P=2784 writeablel false thread ( p) wake-up ( sharedInt=1)(sharedcell=1) Put ( Writeable=false ) Notify thread (c) become ready Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum
Synchronized getSharedInt running reference T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 sObject reference reference sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Thread ( c ) running “Consumer” sObject ---------------- sleep run setName getName : Calling getSharedInt Synchronized getSharedInt running sharedcell 1 Writeable=true value = sharedObject.getSharedInt() sum += value writeablel true Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum 1
( sharedInt=2)(sharedcell=2) Put ( Writeable=false ) reference T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 T sleep C : 792 T sleep P : 96 and Producer setting sharedInt to 2 sObject reference reference sharedInt=-1 Writeable=true ---------------- setShsreInt getShraeInt p “Producer” sObject ---------------- sleep run setName getName : c Two threads begin to run again and going to sleep deferent times 96 and 792 “Consumer” sObject ---------------- sleep run setName getName : thread ( p) wake-up ( sharedInt=2)(sharedcell=2) Put ( Writeable=false ) Notify thread (c) become ready sharedcell 2 writeablel false Thread (p) for count=1 to 10 { sleeping random time and each time and setShsredInt=count } Thread (c) do { sleeping random time and each time and value=getShsredInt } While (value!=10) sum 1
T sleep P : 2784 and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 T sleep C : 792 T sleep P : 96 and Producer setting sharedInt to 2 T sleep P : 559 and Consumer retrieving sharedInt value 2 T sleep C : 1122 Producer setting sharedInt to 3 T sleep P : 192 and Consumer retrieving sharedInt value 3 Producer setting sharedInt to 4 T sleep P : 1545 and T sleep C : 2224 Consumer retrieving sharedInt value 4 Producer setting sharedInt to 5 T sleep P : 2122 and T sleep C : 1349 Consumer retrieving sharedInt value 5 T sleep C : 2886 Producer setting sharedInt to 6 T sleep P : 1869 and Consumer retrieving sharedInt value 6 Producer setting sharedInt to 7 T sleep P : 185 and T sleep C : 665 Consumer retrieving sharedInt value 7 T sleep C : 375 Producer setting sharedInt to 8 T sleep P : 2157 and Consumer retrieving sharedInt value 8 T sleep C : 1061 Producer setting sharedInt to 9 Consumer retrieving sharedInt value 9 T sleep C : 2731 T sleep P : 1365 and Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 55 Terminating Consumer