Download presentation
Presentation is loading. Please wait.
1
THREADS
2
Example -1
3
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(); }
4
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(); }
5
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(); }
6
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(); }
7
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(); }
8
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 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
9
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 public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
10
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 0 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
11
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 0 Fiji 1 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
12
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 0 Fiji 1 Jamaica 1 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
13
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
14
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
15
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
16
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
17
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
18
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
19
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
20
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
21
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
22
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
23
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
24
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
25
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
26
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
27
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji 9 Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
28
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji 9 Jamaica DONE! Jamaica public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
29
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji 9 Jamaica DONE! Jamaica 9 Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
30
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji 9 Jamaica DONE! Jamaica 9 Fiji DONE! Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
31
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 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji 9 Jamaica DONE! Jamaica 9 Fiji DONE! Fiji public class TwoThreadsTest { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji ").start(); }
32
0 Jamaica 0 Fiji 1 Jamaica 1 Fiji 2 Jamaica 2 Fiji 3 Jamaica 3 Fiji 4 Jamaica 5 Jamaica 4 Fiji 6 Jamaica 5 Fiji 6 Fiji 7 Jamaica 7 Fiji 8 Jamaica 8 Fiji 9 Jamaica DONE! Jamaica 9 Fiji DONE! Fiji
33
Example -2
34
class PrintThread extends Thread { private int sleepTime;
public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * ); 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" ); }
35
class PrintThread extends Thread { private int sleepTime;
public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * ); 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" ); }
36
class PrintThread extends Thread { private int sleepTime;
public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * ); 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 :
37
class PrintThread extends Thread { private int sleepTime;
public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * ); 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 :
38
class PrintThread extends Thread { private int sleepTime;
public PrintThread( String name ) super( name ); sleepTime = (int) ( Math.random( ) * ); 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 :
39
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( ) * ); 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" ); }
40
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( ) * ); 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" ); }
41
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( ) * ); 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" ); }
42
Example -3
43
-1 2498 1094 Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 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
44
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 :
45
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();
46
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
47
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;
48
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value 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
49
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to 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
50
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value 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
51
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to 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
52
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value 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
53
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to 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
54
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to 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
55
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value 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
56
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to 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
57
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value 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
58
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to 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
59
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value 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
60
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to 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
61
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to 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
62
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value 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
63
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value 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
64
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value Producer setting sharedInt to 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
65
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value 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
66
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to 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
67
Consumer retrieving sharedInt value -1 1727
reference 2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 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
68
2498 1094 Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer setting sharedInt to Consumer retrieving sharedInt value Consumer retrieving sharedInt value Producer setting sharedInt to Consumer retrieving sharedInt value Producer setting sharedInt to Producer finished producing values Terminating Producer Consumer retrieving sharedInt value Consumer retrieved values totaling: 52 Terminating Consumer
69
Example -4
70
-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
71
Thread ( c ) first wake-up
reference T sleep P : 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
72
Thread ( c ) first wake-up
reference T sleep P : 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
73
Synchronized getSharedInt running
reference T sleep P : 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
74
( sharedInt=2)(sharedcell=2) Put ( Writeable=false )
reference T sleep P : and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 T sleep C : 792 T sleep P : 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
75
T sleep P : and T sleep C : 388 Producer setting sharedInt to 1 Consumer retrieving sharedInt value 1 T sleep C : 792 T sleep P : and Producer setting sharedInt to 2 T sleep P : and Consumer retrieving sharedInt value 2 T sleep C : 1122 Producer setting sharedInt to 3 T sleep P : and Consumer retrieving sharedInt value 3 Producer setting sharedInt to 4 T sleep P : and T sleep C : 2224 Consumer retrieving sharedInt value 4 Producer setting sharedInt to 5 T sleep P : and T sleep C : 1349 Consumer retrieving sharedInt value 5 T sleep C : 2886 Producer setting sharedInt to 6 T sleep P : and Consumer retrieving sharedInt value 6 Producer setting sharedInt to 7 T sleep P : and T sleep C : 665 Consumer retrieving sharedInt value 7 T sleep C : 375 Producer setting sharedInt to 8 T sleep P : 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 : and Producer setting sharedInt to 10 Producer finished producing values Terminating Producer Consumer retrieving sharedInt value 10 Consumer retrieved values totaling: 55 Terminating Consumer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.