Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2002 by Ashby M. Woolf Revision 3 Java Threads Teaching your programs to walk and chew gum at the same time.

Similar presentations


Presentation on theme: "© 2002 by Ashby M. Woolf Revision 3 Java Threads Teaching your programs to walk and chew gum at the same time."— Presentation transcript:

1 © 2002 by Ashby M. Woolf Revision 3 Java Threads Teaching your programs to walk and chew gum at the same time.

2 © 2002 by Ashby M. Woolf Revision 3 The Agenda Why Threads? Starting a New Thread Shutting Down a Thread Daemon Threads Avoiding Conflicts Which Thread Is First? Cooperating Threads

3 © 2002 by Ashby M. Woolf Revision 3 Why Threads? You Expect Programs To Do Many Things At Once –Respond to a Mouse Click –Spell Check Spelling –Print –Do Network Communications –Do I/O Performance Utilize Multiprocessors

4 © 2002 by Ashby M. Woolf Revision 3 Starting A New Thread Four Variations on Starting a Thread

5 © 2002 by Ashby M. Woolf Revision 3 Starting a New Thread public void run() { } Write a method with the signature: Put your threads actions in the method's body.

6 © 2002 by Ashby M. Woolf Revision 3 Starting a New Thread public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } Wrap the run method in a class that implements the Runnable interface.

7 © 2002 by Ashby M. Woolf Revision 3 Starting a New Thread class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); }

8 © 2002 by Ashby M. Woolf Revision 3 Starting a New Thread class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } java DoARunnable

9 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked return; from run() start() I/O Unblocked JVM/OS Decision I/O Blocked Running

10 © 2002 by Ashby M. Woolf Revision 3 Starting a New Thread class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyThread extends Thread { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyThread extends Thread { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread { public static void main(String[] args) { MyThread mt = new MyThread(); Thread t = new Thread(mr); mt.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyThread extends Thread { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread { public static void main(String[] args) { MyThread mt = new MyThread(); //Thread t = new Thread(mr); mt.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } java DoAThread

11 © 2002 by Ashby M. Woolf Revision 3 A Self Starting Thread class MyThread extends Thread { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread { public static void main(String[] args) { MyThread mt = new MyThread(); mt.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } Move start(); to the constructor.

12 © 2002 by Ashby M. Woolf Revision 3 class MyThread extends Thread { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread { public static void main(String[] args) { MyThread mt = new MyThread(); mt.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } A Self Starting Thread class MyThread2 extends Thread { MyThread2() { start();} public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread2 { public static void main(String[] args) { MyThread2 mt = new MyThread2(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyThread2 extends Thread { MyThread2() { start();} public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread2 { public static void main(String[] args) { MyThread2 mt = new MyThread2(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyThread2 extends Thread { MyThread2() { start();} public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAThread2 { public static void main(String[] args) { new MyThread2(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } java DoAThread2

13 © 2002 by Ashby M. Woolf Revision 3 Self Starting a Runnable class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } Move new Thread and start() to the constructor.

14 © 2002 by Ashby M. Woolf Revision 3 Self Starting a Runnable class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunnable2 implements Runnable { MyRunnable2() { Thread t new Thread(this); t.start(); } public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable2 { public static void main(String[] args) { MyRunnable2 mr = new MyRunnable2(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunnable2 implements Runnable { MyRunnable2() { Thread t new Thread(this); t.start(); } public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable2 { public static void main(String[] args) { MyRunnable2 mr = new MyRunnable2(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunnable2 implements Runnable { MyRunnable2() { Thread t new Thread(this); t.start(); } public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable2 { public static void main(String[] args) { new MyRunnable2(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } java DoARunnable2

15 © 2002 by Ashby M. Woolf Revision 3 class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoRunnables { public static void main(String[] args) { MyRunner mr = new MyRunner(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoRunnables { public static void main(String[] args) { MyRunner mr = new MyRunner(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 2000; i++) { System.out.println( text + i ); } public class DoRunnables { public static void main(String[] args) { MyRunner mr = new MyRunner(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 2000; i++) { System.out.println( text + i ); } public class DoRunnables { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyRunner mr = new MyRunner(); Thread t = new Thread(mr); t.start(); } for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 2000; i++) { System.out.println( text + i ); } public class DoRunnables { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyRunner mr = new MyRunner("Runner " + j + ": "); Thread t = new Thread(mr); t.start(); } for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 500 ; i++) { System.out.println( text + i ); } public class DoRunnables { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyRunner mr = new MyRunner("Runner " + j + ": "); Thread t = new Thread(mr); t.start(); } for(int i = 0; i < 500 ; i++) { System.out.println("main(): " + i); } class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 500 ; i++) { System.out.println( text + i ); } public class DoRunnables { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyRunner mr = new MyRunner("Runner " + j + ": "); Thread t = new Thread(mr); t.start(); } for(int i = 0; i < 500 ; i++) { System.out.println("main(): " + i); } Lots of Threads java DoRunnables

16 © 2002 by Ashby M. Woolf Revision 3 Which Threads Runs First? Thread Priorities

17 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked return; from run() start() I/O Unblocked JVM/OS Decision I/O Blocked Running We can set priorities for the JVM.

18 © 2002 by Ashby M. Woolf Revision 3 class MyRunner implements Runnable { String text; MyRunner(String s) { text = s; } public void run() { for(int i = 0; i < 500 ; i++) { System.out.println( text + i ); } public class DoRunnables { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyRunner mr = new MyRunner("Runner " + j + ": "); Thread t = new Thread(mr); t.start(); } for(int i = 0; i < 500 ; i++) { System.out.println("main(): " + i); } class MyPriority implements Runnable { String text; MyPriority(String s) { text = s; } public void run() { for(int i = 0; i < 500 ; i++) { System.out.println( text + i ); } public class DoPriorities { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyPriority mr = new MyPriority("Runner " + j + ": "); Thread t = new Thread(mr); t.start(); } for(int i = 0; i < 500 ; i++) { System.out.println("main(): " + i); } class MyPriority implements Runnable { String text; MyPriority(String s) { text = s; } public void run() { for(int i = 0; i < 500 ; i++) { System.out.println( text + i ); } public class DoPriorities { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyPriority mr = new MyPriority("Runner " + j + ": "); Thread t = new Thread(mr); t.setPriority(j+1); t.start(); } for(int i = 0; i < 500 ; i++) { System.out.println("main(): " + i); } class MyPriority implements Runnable { String text; MyPriority(String s) { text = s; } public void run() { for(int i = 0; i < 500 ; i++) { System.out.println( text + i ); } public class DoPriorities { public static void main(String[] args) { for(int j = 0; j < 10; j++) { MyPriority mr = new MyPriority("Runner " + j + ": "); Thread t = new Thread(mr); t.setPriority(j+1); t.start(); } for(int i = 0; i < 500 ; i++) { System.out.println("main(): " + i); } java DoPriorities Thread Priority

19 © 2002 by Ashby M. Woolf Revision 3 How Do I Stop a Thread? Return Watch a Variable and Return Daemon Threads

20 © 2002 by Ashby M. Woolf Revision 3 How to Stop a Thread Return Watch a Control Variable and Return –Set a Shared Variable –Set a Member Variable with a Method Make a Daemon Thread and Stop the Non-Daemons

21 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked return; from run() start() I/O Unblocked JVM/OS Decision I/O Blocked Running

22 © 2002 by Ashby M. Woolf Revision 3 class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyQuitable1 implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAQuit1 { public static void main(String[] args) { MyQuitable1 mq = new MyQuitable1(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyQuitable1 implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoAQuit1 { static boolean go = true; public static void main(String[] args) { MyQuitable1 mq = new MyQuitable1(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable1 implements Runnable { public void run() { for(int i = 0; DoAQuit1.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit1 { static boolean go = true; public static void main(String[] args) { MyQuitable1 mq = new MyQuitable1(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable1 implements Runnable { public void run() { for(int i = 0; DoAQuit1.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit1 { static boolean go = true; public static void main(String[] args) { MyQuitable1 mq = new MyQuitable1(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } java DoAQuit1 Watching a Shared Variable

23 © 2002 by Ashby M. Woolf Revision 3 class MyQuitable1 implements Runnable { public void run() { for(int i = 0; DoAQuit1.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit1 { static boolean go = true; public static void main(String[] args) { MyQuitable1 mq = new MyQuitable1(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } Using a Method to Quit class MyQuitable2 implements Runnable { public void run() { for(int i = 0; DoAQuit2.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { static boolean go = true; public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable2 implements Runnable { public void run() { for(int i = 0; DoAQuit2.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { static boolean go = true; public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable2 implements Runnable { boolean go = true; public void run() { for(int i = 0; DoAQuit2.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable2 implements Runnable { boolean go = true; public void quit() {go = false;} public void run() { for(int i = 0; DoAQuit1.go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable2 implements Runnable { boolean go = true; public void quit() {go = false;} public void run() { for(int i = 0; go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) go = false; } class MyQuitable2 implements Runnable { boolean go = true; public void quit() {go = false;} public void run() { for(int i = 0; go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) mq.quit(); } class MyQuitable2 implements Runnable { boolean go = true; public void quit() {go = false;} public void run() { for(int i = 0; go ; i++) { System.out.println("run(): " + i); } public class DoAQuit2 { public static void main(String[] args) { MyQuitable2 mq = new MyQuitable2(); Thread t = new Thread(mq); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); if( i == 1000 ) mq.quit(); } java DoAQuit2

24 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked return; from run() start() I/O Unblocked JVM/OS Decision I/O Blocked Running Lonely Daemons

25 © 2002 by Ashby M. Woolf Revision 3 class MyRunnable implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoARunnable { public static void main(String[] args) { MyRunnable mr = new MyRunnable(); Thread t = new Thread(mr); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } Daemon Threads class MyDaemon implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoADaemon { public static void main(String[] args) { MyDaemon md = new MyDaemon(); Thread t = new Thread(md); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyDaemon implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run(): " + i); } public class DoADaemon { public static void main(String[] args) { MyDaemon md = new MyDaemon(); Thread t = new Thread(md); t.setDaemon(true); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } class MyDaemon implements Runnable { public void run() { for(int i = 0; true ; i++) { System.out.println("run(): " + i); } public class DoADaemon { public static void main(String[] args) { MyDaemon md = new MyDaemon(); Thread t = new Thread(md); t.setDaemon(true); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i); } java DoADaemon

26 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked return; from run() start() I/O Unblocked JVM/OS Decision I/O Blocked Running Lonely Daemons destroy() stop()

27 © 2002 by Ashby M. Woolf Revision 3 Putting a Thread to Sleep Z Z Z Z Z Z Z z z z z z z z z z z z z.....

28 © 2002 by Ashby M. Woolf Revision 3 Sleep class MySleep implements Runnable { public void run() { for(int i = 0; i < 2000; i++) { System.out.println("run() : " + i ); if(i == 500) { System.out.println("Nap Time"); try {Thread.currentThread().sleep(5000L); }catch(Exception e) { e.printStackTrace(); } } public class DoMySleep { public static void main(String[] args) { MySleep ms = new MySleep(); Thread t = new Thread(ms); t.start(); for(int i = 0; i < 2000; i++) { System.out.println("main(): " + i ); } java DoMySleep

29 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked Sleep State return; from run() start() I/O Unblocked JVM/OS Decision I/O Blocked Running sleep(ms) Lonely Daemons

30 © 2002 by Ashby M. Woolf Revision 3 How Do I Avoid Access Conflicts? Synchronize Your Work

31 © 2002 by Ashby M. Woolf Revision 3 Coordinating Access to Shared Data temp = a; a = b; b = temp; temp = a; a = b; b = temp; class Foo { int a = 1; int b = 2; int temp; void f() { temp = a; a = b; b = temp; } temp = ? a = 1 b = 2 temp = 1 a = 1 b = 2 temp = 1 a = 2 b = 2 temp = 2 a = 2 b = 2

32 © 2002 by Ashby M. Woolf Revision 3 Simple Swap class Foo { int a = 1; int b = 2; int temp; void f() { temp = a; a = b; b = temp; } class MySwap extends Thread { Foo foo; MySwap(Foo f) { foo = f; } public void run() { for(int i = 0; i < 25000000; i++) { foo.f(); } public class DoASwap { public static void main(String[] args) { Foo foo = new Foo(); MySwap ms = new MySwap(foo); ms.start(); for(int i = 0; i < 25000000; i++) { foo.f(); if(foo.a == foo.b) { System.out.print("a:b " + foo.a + ":" + foo.b); System.out.println("\t " + i); } java DoASwap

33 © 2002 by Ashby M. Woolf Revision 3 Coordinating Access to Shared Values and Objects synchronized ( expression ) { } synchronized ( expression ) { // Code accessing // shared values // and objects } Expression must result in a reference type. Frequently the Object with shared data Locks the lock associated with the object before: Unlocks the lock associated with the object after:

34 © 2002 by Ashby M. Woolf Revision 3 Coordinating Access to Shared Values and Objects synchronized ( expression ) { // Code accessing // shared values // and objects } "Lock" only applies to other synchronized code.

35 © 2002 by Ashby M. Woolf Revision 3 class Foo { int a = 1; int b = 2; int temp; void f() { temp = a; a = b; b = temp; } Coordinating Access to Shared Data class Foo { int a = 1; int b = 2; int temp; void f() { synchronized(this) { temp = a; a = b; b = temp; } class Foo { int a = 1; int b = 2; int temp; void f() { synchronized(this) { temp = a; a = b; b = temp; } temp = a; a = b; b = temp; temp = a; a = b; b = temp; Locks this Unlocks this Locks this Unlocks this

36 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked Object Lock return; from run() start() I/O Unblocked Lock Unavailable Lock Free JVM/OS Decision I/O Blocked Running Lonely Daemons Sleep State sleep(ms)

37 © 2002 by Ashby M. Woolf Revision 3 class Foo { int a = 1; int b = 2; int temp; void f() { synchronized(this) { temp = a; a = b; b = temp; } class Foo { int a = 1; int b = 2; int temp; synchronized void f() { temp = a; a = b; b = temp; } Synchronized Method Syntax temp = a; a = b; b = temp; temp = a; a = b; b = temp; Locks this Unlocks this Locks this Unlocks this

38 © 2002 by Ashby M. Woolf Revision 3 class Foo { int a = 1; int b = 2; int temp; void f() { temp = a; a = b; b = temp; } class MySwap extends Thread { Foo foo; MySwap(Foo f) { foo = f; } public void run() { for(int i = 0; i < 25000000; i++) { foo.f(); } public class DoASwap { public static void main(String[] args) { Foo foo = new Foo(); MySwap ms = new MySwap(foo); ms.start(); for(int i = 0; i < 25000000; i++) { foo.f(); if(foo.a == foo.b) { System.out.print("a:b " + foo.a + ":" + foo.b); System.out.println("\t " + i); } class Foo { int a = 1; int b = 2; int temp; synchronized void f() { temp = a; a = b; b = temp; } class MySwap extends Thread { Foo foo; MySwap(Foo f) { foo = f; } public void run() { for(int i = 0; i < 25000000; i++) { foo.f(); } public class DoASwap { public static void main(String[] args) { Foo foo = new Foo(); MySwap ms = new MySwap(foo); ms.start(); for(int i = 0; i < 25000000; i++) { foo.f(); if(foo.a == foo.b) { System.out.print("a:b " + foo.a + ":" + foo.b); System.out.println("\t " + i); } class Foo { int a = 1; int b = 2; int temp; synchronized void f() { temp = a; a = b; b = temp; } class MySwap extends Thread { Foo foo; MySwap(Foo f) { foo = f; } public void run() { for(int i = 0; i < 25000000; i++) { foo.f(); } public class DoASwap { public static void main(String[] args) { Foo foo = new Foo(); MySwap ms = new MySwap(foo); ms.start(); for(int i = 0; i < 25000000; i++) { foo.f(); if( i%1000000 == 0 ) { System.out.print("a:b " + foo.a + ":" + foo.b); System.out.println("\t " + i); } Simple Swap 2 java DoASwap2

39 © 2002 by Ashby M. Woolf Revision 3 Review - Member and Local Variables class Dog { int i; public int cat(int j) { int k = 4; return i + j + k; } j and k are Local i is a Member Variable Local variables don't share.

40 © 2002 by Ashby M. Woolf Revision 3 How do I Coordinate Threads? The wait() and notify() Methods Can Build Efficient Coordination.

41 © 2002 by Ashby M. Woolf Revision 3 Got Space? Data Got Data? Producer Consumer Example ProducerConsumer Shared Data SourceCoordinationSink Got Data? Got Space? Data Got Data? put(int i) int get()

42 © 2002 by Ashby M. Woolf Revision 3 Source - Producer class Source extends Thread { Coordinator c; Source(Coordinator coord) {c = coord;} public void run() { for(int i = 0; i < 500; i++) { c.put(i); System.out.println("put; " + i); try{ sleep( (int)(Math.random()*10) ); } catch(InterruptedException e) {} }

43 © 2002 by Ashby M. Woolf Revision 3 Sink - Consumer class Sink extends Thread { Coordinator c; Sink(Coordinator coord) {c = coord;} public void run() { for(int i = 0; i < 500; i++) { System.out.println("get: " + c.get()); }

44 © 2002 by Ashby M. Woolf Revision 3 Coordinator class Coordinator { int data; public void put(int in) { data = in; } public int get() { return data; }

45 © 2002 by Ashby M. Woolf Revision 3 DoCoordinator public class DoCoordinator { public static void main(String[] args) { Coordinator c = new Coordinator(); Source so = new Source(c); Sink si = new Sink(c); so.start(); si.start(); } java DoCoordinator

46 © 2002 by Ashby M. Woolf Revision 3 Coordinator class Coordinator2 { int data; public void put(int in) { data = in; } public int get() { return data; } class Coordinator2 { int data; boolean ready = false; public void put(int in) { data = in; } public int get() { return data; } class Coordinator2 { int data; boolean ready = false; public void put(int in) { while( ready ) { wait(); } data = in; } public int get() { return data; } class Coordinator2 { int data; boolean ready = false; public void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; } public int get() { return data; } class Coordinator2 { int data; boolean ready = false; public void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; ready = true; } public int get() { return data; } class Coordinator2 { int data; boolean ready = false; public void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; ready = true; notifyAll() } public int get() { return data; }

47 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked Sleep State Wait State Object Lock return; from run() start() notify() notifyAll() I/O Unblocked wait() Lock Unavailable Lock Free JVM/OS Decision I/O Blocked Running sleep(ms) Lonely Daemons Releases Locks Keeps Locks

48 © 2002 by Ashby M. Woolf Revision 3 Coordinator 2 class Coordinator2 { int data; boolean ready = false; public void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; ready = true; notifyAll() } public int get() { return data; } class Coordinator2 { int data; boolean ready = false; public void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; ready = true; notifyAll() } public int get() { while( !ready ) { try( wait(); } catch(InteruptedException e) {} } ready = false; notifyAll(); return data; } class Coordinator2 { int data; boolean ready = false; public synchronized void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; ready = true; notifyAll() } public synchronized int get() { while( !ready ) { try( wait(); } catch(InteruptedException e) {} } ready = false; notifyAll(); return data; } class Coordinator2 { int data; boolean ready = false; public synchronized void put(int in) { while( ready ) { try { wait(); } catch(InterruptedException e) {} } data = in; ready = true; notifyAll() } public synchronized int get() { while( !ready ) { try( wait(); } catch(InteruptedException e) {} } ready = false; notifyAll(); return data; } java DoCoordinator2

49 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() Suspend State I/O Blocked Sleep State Wait State Object Lock return; from run() start() notify() notifyAll() I/O Unblocked wait() resume() Lock Unavailable Lock Free JVM/OS Decision suspend() I/O Blocked Running sleep(ms) Lonely Daemons

50 © 2002 by Ashby M. Woolf Revision 3 What is a Good Way to Manage Many Threads? You can Manage Threads with Thread Groups.

51 © 2002 by Ashby M. Woolf Revision 3 Thread Groups main subgroup1 subgroup3 subgroup2 Thread_c Thread_gThread_f Thread_d Thread_e Thread_bThread_a new Thread(sg1, "Thread_c") sg1 = new ThreadGroup(main, "subgroup1")

52 © 2002 by Ashby M. Woolf Revision 3 Controlling Thread Groups Collection Management Methods Methods That Operate on the Group Methods that Operate on Threads of a Group Access Restriction Methods

53 © 2002 by Ashby M. Woolf Revision 3 Summary Starting Threads –Extend Thread –Implement Runnable Priorities Thread States and Transitions

54 © 2002 by Ashby M. Woolf Revision 3 Blocked Thread States New Runnable Dead new Thread() I/O Blocked Sleep State Wait State Object Lock return; from run() start() notify() notifyAll() I/O Unblocked wait() Lock Unavailable Lock Free JVM/OS Decision I/O Blocked Running sleep(ms) Lonely Daemons

55 © 2002 by Ashby M. Woolf Revision 3 Summary Starting Threads –Extend Thread –Implement Runnaable Priorities Thread States and Transitions Starting Threads –Extend Thread –Implement Runnaable Priorities Thread States and Transitions Thread Groups

56 © 2002 by Ashby M. Woolf Revision 3 End of Content


Download ppt "© 2002 by Ashby M. Woolf Revision 3 Java Threads Teaching your programs to walk and chew gum at the same time."

Similar presentations


Ads by Google