Java Java Programming Spring 2010 Runnablejava lang Runnable
Java程序设计 Java Programming Spring, 2010
接口Runnable-java. lang. Runnable public interface java. lang. Runnable { public abstract void run(); } 10
java. lang. Thread public class java. lang. Thread extends java. lang. Object implements java. lang. Runnable { public Thread(); public Thread(Runnable target, String name); public Thread(String name); . . . public void run(); public synchronized void start(); public static void sleep(long millis)throws Interrupted. Exception; public static void yield(); public final String get. Name(); . . . constructors methods 11
Thread类包含的常量 l Thread类包含的常量有: 1. public static final int MAX_PRIORITY: 最大优先 级,值是 10。 2. public static final int MIN_PRIORITY: 最小优先 级,值是 1。 3. public static final int NORM_PRIORITY:缺省优 先级,值是 5。 12
Extending Thread class My. Thread extends Thread { public void run() { //thread body of execution } } l 注意:Thread类中的run( )方法具有public属性, 覆盖该方法时,前面必须带上public。 17
Create and Execute Thread 1. 创建一个 thread: My. Thread thr 1 = new My. Thread(); 2. 运行一个线程: thr 1. start(); 3. 创建和运行线程: new My. Thread(). start(); 18
Example 1: class My. Thread extends Thread { private String name, msg; public My. Thread(String name, String msg) { this. name = name; this. msg = msg; } public void run() { System. out. println(name+" starts its execution"); for (int i = 0; i < 5; i++) { System. out. println(name + " says: " + msg); try { Thread. sleep(5000); } catch (Interrupted. Exception ie) {} } System. out. println(name + " finished execution"); } } 19
Example 1 (cont. ) public class test { public static void main(String[] args) { My. Thread mt 1 = new My. Thread("thread 1", "ping"); My. Thread mt 2 = new My. Thread("thread 2", "pong"); mt 1. start(); mt 2. start(); } } the threads will run in parallel 20
Example 1 (cont. ) l 输出: thread 1 starts its execution thread 1 says: ping thread 2 starts its execution thread 2 says: pong thread 1 says: ping thread 2 says: pong thread 1 finished execution thread 2 finished execution 21
Implementing Runnable interface class My. Thread extends ABC implements Runnable { . . . public void run() { // thread body of execution } } 1. 2. 3. Creating Object: My. Thread my. Object = new My. Thread(); Creating Thread Object: Thread thr 1 = new Thread(my. Object); Start Execution: thr 1. start(); 24
Example 2 class My. Thread implements Runnable { public void run() { System. out. println(" this thread is running. . . "); } } class Thread. Ex 2 { public static void main(String [] args ) { Thread t = new Thread(new My. Thread()); t. start(); } } 25
class get. Thread. Info { // �� 程示例 public static void main(String args[ ]) { String name; int p; Thread curr; } } curr=Thread. current. Thread( ); //� 得当前� 程 System. out. println("当前� 程: "+curr); name=curr. get. Name( ); p=curr. get. Priority( ); System. out. println("� 程名: "+name); System. out. println("� 先� : "+p); 程序输出结果: 当前线程: Thread[main, 5, main] 线程名 : main 优先级 : 5 27
// 程序 4 class thread. Test extends Thread{ thread. Test(String str){ super(str); } public void run( ){ try{ Thread. sleep(2); }catch(Interrupted. Exception e) { System. err. println(e. to. String( )); } System. out. println(get. Name( )+" " + get. Priority( )); } } 34
public class mult. Thead. One{ public static void main(String agrs[]){ Thread one=new thread. Test("one" ); Thread two=new thread. Test("two" ); Thread three=new thread. Test("three" ); one. set. Priority(Thread. MIN_PRIORITY); two. set. Priority(Thread. NORM_PRIORITY); three. set. Priority(Thread. MAX_PRIORITY); one. start( ); two. start( ); three. start( ); } } 程序输出结果: three 10 two 5 one 1 35
线程同步 l Examples: 1. 自动柜员机: Simultaneous(同时的) operations on your bank account, Can the following operations be done at the same time on the same account? u Deposit() u 2. Withdraw() 例子:多个线程对一个对象进行操作(程序 5)。 39
// 程序 5--线程并发引起的不确定性 class Shared. Obj{ void Play(int n) { System. out. println("运行线程 NO:"+n); try{ Thread. sleep(3); }catch(Interrupted. Exception e) { System. out. println(“线程异常, NO: "+n); } System. out. println("结束线程 NO:"+n); } } 40
class User. Mult. Thread implements Runnable{ Shared. Obj User. Obj; //共享� 象 int num; } User. Mult. Thread(Shared. Obj o, int n) { User. Obj=o; num=n; } public void run( ) { User. Obj. Play(num); } public class mult. Thead. Two { public static void main(String args[ ]) { Shared. Obj=new Shared. Obj( ); //共享� 象 } } Thread t 1=new Thread(new User. Mult. Thread(Obj, 1)); Thread t 2=new Thread(new User. Mult. Thread(Obj, 2)); Thread t 3=new Thread(new User. Mult. Thread(Obj, 3)); t 1. start( ); t 2. start( ); t 3. start( ); 41
class User. Mult. Thread implements Runnable{ Shared. Obj User. Obj; //共享� 象 int num; User. Mult. Thread(Shared. Obj o, int n) { User. Obj=o; num=n; } public void run( ) { //User. Obj. Play(num); synchronized(User. Obj) { User. Obj. Play(num); } } } 46
// 程序 5 class Shared. Obj{ synchronized void Play(int n) { System. out. println("运行� 程 NO:"+n); try{ Thread. sleep(3); }catch(Interrupted. Exception e) { System. out. println(“� 程异常, NO:"+n); } } } System. out. println("� 束� 程 NO:"+n); 48
线程间通信 l 多线程通信的方法有两种: 1. 把共享变量和方法封装在一个类中实现; 2. 通过wait( )和notify( )方法实现。 Ø public final void wait(long timeout); Ø public final void wait(long timeout, int nanos); Ø public final void wait(); Ø Ø public final void notify. All(); public final void notify(); Note: Nanosecond 十亿分之一秒, 毫微秒 Timeout 毫秒数 49
java. lang. Object Note: wait() and notify() are methods in class Object: public class java. lang. Object {. . . public final void wait() throws Interrupted. Exception; public final void notify(); public final void notify. All(); . . . } 50
//程序 6--共享类 class comm{ private int n; private boolean bool=false; void produce(int i) { while(bool) { } //bool == true, wait n=i; bool=true; System. out. println("n � 生数据:"+n); } } void readout( ) { while(!bool) { }//bool== false, wait bool=false; System. out. println(" � 取数据:"+n); } 52
//程序 6-- data. Producer class data. Producer implements Runnable{ comm cm; data. Producer(comm c) { cm=c; } public void run( ) { //生� 者� 生 5个随机数 for(int i=0; i<5; i++) cm. produce((int)(Math. random( )*100)); } } 53
//程序 6-- data. Consumer class data. Consumer implements Runnable{ comm cm; data. Consumer(comm c) { cm=c; } } public void run( ) { for(int i=0; i<5; i++) cm. readout( ); //消� 者� 取数据 } 54
//程序 6-- �� public class mult. Thead. Three { public static void main(String args[ ]) { comm cm=new comm( ); Thread t 1=new Thread( //无名� 象做参数 new data. Producer(cm)); Thread t 2=new Thread( new data. Consumer(cm)); t 1. start( ); t 2. start( ); } } 55
//修改程序 6--共享类 class comm{ private int n; private boolean bool=false; void produce(int i) { while(bool) { } //bool == true, wait n=i; bool=true; System. out. println("n � 生数据:"+n); } } void readout( ) { //while(!bool) { } //修改, 不等待。 bool=false; System. out. println(" � 取数据:"+n); } 57
// 程序 7 class comm{ private int n; private boolean bool=false; synchronized void produce(int j) { if(bool) {//bool == true, wait try{ wait( ); }catch(Interrupted. Exception e) { System. out. println("comm 出� 异常"); } }else{ n=j; bool=true; System. out. println("n� 生数据: "+n); notify( ); // � 醒另外一个� 程 } } 60
//程序 7(续) synchronized void readout( ) { if(!bool) {//bool == false, wait try{ wait( ); }catch(Interrupted. Exception e) { System. out. println(" comm 出� 异常"); } }else{ bool=false; System. out. println("� 取数据: "+n); notify( ); // � 醒另一个� 程 } } } 61
// 程序 7 class data. Producer implements Runnable{ comm cm; data. Producer (comm c) { cm=c; } public void run( ) { try{ for(int i=0; i<5; i++) { cm. produce((int)(Math. random( )*100)); Thread. sleep(10); } }catch(Interrupted. Exception e) { System. out. println("data. Producer 出� 异常"); } } } 62
// 程序 7 class data. Consumer implements Runnable{ comm cm; data. Consumer(comm c) { cm=c; } public void run( ) { try{ for(int i=0; i<5; i++) { cm. readout( ); Thread. sleep(10); } }catch(Interrupted. Exception e) { System. out. println(" data. Consumer 出� 异常"); } } } 63
// 程序 7 public class mult. Thead. Four{ public static void main(String args[ ]) { comm cm=new comm( ); Thread t 1=new Thread(new data. Producer(cm)); Thread t 2=new Thread(new data. Consumer(cm)); } } t 1. start( ); t 2. start( ); 64
t 1 t 2 run(); cm i=0 i< 5 0 yes false cm. produce(j); bool false 66
t 1 t 2 run(); cm i=0 i< 5 j yes false cm. produce(j); bool false n = j; 67
t 1 t 2 cm run(); i=0 i< 5 j true yes cm. produce(j); loop: 2 nd true bool false wait(); yes cm. readout(); bool true n = j; bool = true; notify(); i++ 68
t 1 t 2 cm run(); i=0 i< 5 j false yes cm. produce(j); loop: 2 nd true bool wait(); yes loop: 2 nd cm. readout(); false wait(); bool false true n = j; bool = false bool = true; notify(); System. out. println(" 读取数据: "+n); i++ notify(); i++ 69
线程联合 public class Thread. Test implements Runnable { public static int a = 0; public void run() { for (int k = 0; k < 5; k++) { a = a + 1; } } public static void main(String[] args) throws Exception{ Runnable r = new Thread. Test(); Thread t = new Thread(r); t. start(); t. join(); //等待t� 行完� ,main��� 行 System. out. println(a); } } • t� 程� 合“主� 程” 72
t 1 t 2 cm run(); i=0 i< 5 j true cm. produce(j); bool true cm. readout(); wait(); false wait(); bool false true n = j; bool = true; notify(); i++ 75
- Slides: 75