第11章 线程

  • 格式:pdf
  • 大小:464.02 KB
  • 文档页数:48

华中科技大学管理学院
创建线程方法2
• 一个类声明实现Runnable接口就可以充当线程体, 在接口Runnable中只定义了一个方法 run(): – public void run(); • 任何实现接口Runnable的对象都可以作为一个线 程的目标对象,类Thread本身也实现了接口 Runnable。
华中科技大学管理学院
第11章 线程
华中科技大学管理学院
主要内容
多线程Multithreading 线程Threads 两种使用方法 线程状态 中断线程 线程优先级Priorities 同步线程Synchronizing Threads 死锁Dead Lock
华中科技大学管理学院
华中科技大学管理学院
T
T
华中科技大学管理学院
两种实现多线程的方法
继承 类 ng.Thread class MyClass extends Thread
使用接口 Runnable class MyClass implements Runnable 注意:上述两种方法中都可用类Thread产生线程的对象 Thread newthread=new Thread(); • run方法是线程运行的主体,要实现用户自己的线 程控制就必须重写run( )方法。
THREAD 方法
start ( ) Are all deprecated !!! (except start) stop ( ) suspend ( ) resume ( ) destroy ( ) Destroys this thread, without any cleanup (This method is not implemented.)
THREAD 状态 Running
华中科技大学管理学院
Done
sleep ( ) wait ( ) join ( ) what else ?
yield ( )
Ready
End of sleep period interrupt ( ) notifiy ( ) what else?
Waiting
举例1(通过继承类Thread构造线程体)
继承Thread类
华中科技大学管理学院
1. public class ExtendingClassThread { 2. public static void main (String [ ] args) { 3. Alpha a1 = new alpha (“one”); 4. Alpha a2 = new Alpha (“two); 5. Alpha a3 = new Alpha (“three”); 6. a1.start ( ); 7. a2.start ( ); 8. a3 start ( ); 9. try { 10. Thread.sleep (500); / / sleep for 500 milisec 11. } catch (InterruptedException e) { } 12. a1.stop ( ); / / stop the threads 13. a2.stop ( ); 14. a3.stop ( );
华中科技大学管理学院
创建线程方法1
• Java的线程是通过ng.Thread类来实现的。 生成一个Thread类的对象之后,一个新的线程就 产生了。 • 通过它可以启动线程、终止线程、线程挂起等, 每个线程都是通过类Thread在Java的软件包 ng中定义,它的构造方法为:
继承Thread类
16. } 17. } 18. class Alpha extends Thread { 19. String str; 20. public Alpha (String s) { 21. str = s; 22. } 23. public void run ( ) { 24. while (true) 25. system.out.println (str); 26. } 27. }
• Java提供了不同Thread类构造器,允许给线程指 定名称。如果name为null时,则Java自动提供唯一 的名称。当上述构造方法的某个参数为null时,可 得到下面的几个构造方法:
– – – – – public Thread (); public Thread (Runnable target); public Thread (Runnable target,String name); public Thread (String name); public Thread (ThreadGroup group,Runnable target); – public Thread (ThreadGroup group,String name);
华中科技大学管理学院
THREAD 方法
Boolean isAlive ( ) Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. Causes the currently executing thread object to temporarily pause and allow other threads to execute. Changes the priority of this thread. Returns this thread’s priority. Interrupts this thread.
继承Thread类
1. 2. 3. 4. 5. 6. 7. 8. 9.
华中科技大学管理学院
class MyClass extends Thread { public void run ( ) { / / the code that should be executed in / / a parallel thread } } MyClass a = new MyClass ( ) ; a.start ( ) ; / / causes method “run” to / / execute in a parallel thread 方法 run 要重写 调用方法 start 启动线程. 新线程在方法run结束后停止
MyClass a = new MyClass ( ); Thread t = new Thread (a); t.start ( ); MyClass 使用接口 Runnable, 生成 ng.Thread的一个对象 MyClass 要重写 run 方法. Q: 这里使用的类Thread 的哪个构造方法? 继承类Thread,我们使用的是哪个构造方法?
小结
华中科技大学管理学院
行 20, 定义使用接口Runnable的类Alpha. 行6-8, 建立了线程类Thread三个实例, 将其引用传 给类Alpha. 行10-12, 启动三个线程. 行13, 程序挂起半秒钟 行16-18,发送停止信息给线程,使其执行完.
华中科技大学管理学院
10. 11. 12. 13. 14. 15.
使用接口 RUNNABLE
华中科技大学管理学院
15. t2.stop ( ) ; 16. t3.stop ( ) ; 17. } 18. } 19. 20. class Alpha implements Runnable { 21. String str ; 22. public Alpha (String s) { srt = s; } 23. public void run ( ) { 24. while (true) System.out.println (str) ; 25. } 26. }
华中科技大学管理学院
华中科技大学管理学院
继承Thread类
Q: 程序输出结果 ? Q: 程序有多少个线程 ?
使用接口 RUNNABLE
1. 2. 3. 4. 5.
6. 7. 8.
华中科技大学管理学院
class MyClass implements Runnable { public void run ( ) { / / the work that the thread should perform } }
public Thread (ThreadGroup group,group 指明该线程所属的线程 Runnable target, String name); target实际执行线程体的目标对 name为线程名。
象,它必须实现接口Runnable;
• Java中的每个线程都有自己的名称
华中科技大学管理学院
sleep (long millis)
yield ( )
setPriority (int) getPriority ( ) Interrupt ( )
华中科技大学管理学院
THREAD 方法
Boolean interrupted ( ) join ( ) join (long millis) Thread currentThread ( ) Tests if the current thread has benn interrupted. Waits for this thread to die. Waits at most milliseconds for this thread to die. Returns a reference to the currently executing thread object.
由 ng.object继承的方法
wait ( ) Causes current thread to wait until another wait (int millis) thread invokes the notify ( ) or the notifyAll ( ) notify ( )源自使用接口 RUNNABLE