java基础教程-第9章.ppt_第1页
java基础教程-第9章.ppt_第2页
java基础教程-第9章.ppt_第3页
java基础教程-第9章.ppt_第4页
java基础教程-第9章.ppt_第5页
已阅读5页,还剩41页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第9章Java的多线程机制,牙齿章节主要介绍线程的概念、线程的创建和线程的管理。示例9.1类使用Thread的子类创建线程class calculate Thread extends Thread private string name。public calculate thread(string name)super(name);T=name,public void run()system . out . print ln(name线程表示先睡1s(秒),然后计算圆周率的平方根。try thread . sleep(1000);catch(interrupted except

2、ion ie)system . out . print ln(圆周率的平方根是Math.sqrt(Math .PI);System.out.println(name线程说:你好!);class print thread extends thread private string name;public print thread(string name)super(name);T=name,public void run()system . out . print ln(name线程说:我立即打印10颗星星!);for(int I=1);I=10I)system . out .

3、print(*);system . out . print ln();System.out.println(name线程说:你好!);classthread test public static void main(string args)calculate thread t1=new calculate thread(计算);PrintThread t2=new PrintThread(打印);System.out.println(开始计算线程t1);t1 . start();System.out.println(启动打印线程T2);T2 . start();System.out.print

4、ln(主要方法是:再见!);牙齿程序的输出如下:计算线程开始t1打印线程开始T2 main方法3360再见!计算线程先睡1s(秒),计算出圆周率的平方根打印线程。我马上打印10颗星星!* * * * * * * * * * * * *打印线程说:再见!圆周率的平方根是:1.7724538509055159计算线程是“再见!Java语言支持多线程,允许在一个Java节目中同时执行多个线程,从而使使用Java程序的用户减少等待时间并提高工作效率。线程是执行体,牙齿执行体的命令流只有一个起点和一个终点,在执行的特定时间点只有一个执行点。(David aser,Northern Exposure(美国

5、电视电视剧),run)线程不能单独存在,它可能是程序的一部分。您可以在一个程序中使用多个线程。这些线程可以同时运行,每个线程徐璐执行其他任务。线程之间可以共享电脑系统分配给程序的资源(分配给程序的内存、外部设备权限等)。线程之间可以使用共享内存空间进行信息更换(线程之间的通信)。在Java中,线程是活动对象。活动对象不仅可以接收消息,还可以向其他对象发送消息。声明Thread的子类。在子类中重建方法run,然后声明子类的配置方法。然后创建子类中的对象。public viod start()throws illegalthreadstateexception牙齿启动当前线程时,将立即返回方法。线

6、程启动后,再次调用方法start将引发IllegalThreadStateException异常。如果线程启动后获得CPU许可证,则调用牙齿线程的run方法。,示例实施9.2介面Runnable以生成线程class calculate class implements run nable private string name;public calculate class(string name)this . name=name;public void run()system . out . print ln(name线程表示先睡1s(秒),然后计算圆周率的平方根。try thread . s

7、leep(1000);catch(interrupted exception ie)system . out . print ln(圆周率的平方根是Math.sqrt(Math .PI);System.out.println(name线程说:你好!);class print class implements runnable private string name;public print class(string name)this . name=name;public void run()system . out . print ln(name线程说:我立即打印10颗星星!);for(in

8、t I=1);I=10I)system . out . print(*);system . out . print ln();System.out.println(name线程说:你好!);class runnable test public static voidmain(string args)calculate class calculate obj=new calculate class(计算);print class print obj=new print class(打印);threadt 1=newthread(calculate obj);Thread T2=new Threa

9、d(print obj);System.out.println(开始计算线程t1);t1 . start();System.out.println(启动打印线程T2);T2 . start();system.out.println(主方法是:再见!);首先,声明实现介面Runnable的类以创建活动对象,并将牙齿活动对象用作配置方法的参数以创建线程对象。public static void sleep(long millis)throws interrupted exception使当前线程放弃millis毫秒运行(睡眠)。如果当前线程在睡眠期间被另一个线程中断,则睡眠过程将终止,并抛出Int

10、erruptedException异常。Public Thread(Runnable runObj)创建新线程。牙齿线程使用活动对象runObj的run方法。新线程的名称由系统生成。示例9.2线程状态控制class calculate thread extends thread private string name;public calculate thread(string name)super(name);T=name,public void run()system . out . print ln(name线程表示先睡1s(秒),然后计算圆周率的平方根。try thre

11、ad . sleep(1000);catch(interrupted exception ie)system . out . print ln(圆周率的平方根是Math.sqrt(Math .PI);System.out.println(name线程说:你好!);class print thread extends thread private string name;public print thread(string name)super(name);T=name,class runnable test public static voidmain(string args)

12、calculate class calculate obj=new calculate class(计算);print class print obj=new print class(打印);threadt 1=newthread(calculate obj);Thread T2=new Thread(print obj);System.out.println(开始计算线程t1);t1 . start();System.out.println(启动打印线程T2);T2 . start();system.out.println(主方法是:再见!);首先,声明实现介面Runnable的类以创建活动

13、对象,并将牙齿活动对象用作配置方法的参数以创建线程对象。public static void sleep(long millis)throws interrupted exception使当前线程放弃millis毫秒运行(睡眠)。如果当前线程在睡眠期间被另一个线程中断,则睡眠过程将终止,并抛出InterruptedException异常。Public Thread(Runnable runObj)创建新线程。牙齿线程使用活动对象runObj的run方法。新线程的名称由系统生成。,示例9.3线程状态控制class calculate thread extends thread private s

14、tring name;public calculate thread(string name)super(name);T=name,public void run()system . out . print ln(name线程表示先睡1s(秒),然后计算圆周率的平方根。try thread . sleep(1000);catch(interrupted exception ie)system . out . print ln(圆周率的平方根是Math.sqrt(Math .PI);System.out.println(name线程说:你好!);class print threa

15、d extends thread private string name;public print thread(string name)super(name);T=name,public void run()system . out . print ln(name线程说:我立即打印10颗星星!);for(int I=1);I=10I)system . out . print(*);system . out . print ln();try suspend();安全执行se(catch)system . out . print ln(name线程说:你好!);classthre

16、adstate public static void main(string args)calculate threadt 1=new calculate thread(计算);PrintThread t2=new PrintThread(打印);System.out.println(开始计算线程t1);t1 . start();System.out.println(启动打印线程T2);T2 . start();try t1 . join();catch(interrupted exception ie)try T2 . resume();安全执行se(catch)try T2 . join(

17、);catch(interrupted exception ie)if(!t1 . is alive()system . out . print ln(结束计算线程);If(!T2 . is alive()system . out . print ln(终止打印线程);System.out.println(主要方法是:再见!);牙齿程序的输出如下:启动计算线程以启动打印线程计算行。先睡4s(秒),然后计算圆周率的平方根打印线。马上打印10颗星星!* * * * * * * * * * *圆周率的平方根是1.772 453 850 905 515 9计算线程。再见!印刷线程说:你好!计算线程结束

18、。打印线程结束。main方法结束。再见。使用IsAlive,您可以验证线程是否已启动而未进入关闭状态。门t1 . join();等待计算线程结束语句t2.resume(),然后打印线程语句suspend();暂停当前线程,使其进入阻塞状态。另一个线程调用当前线程的方法resume(),然后唤醒当前线程并使其进入就绪状态。处于阻塞状态的线程可以使用stop()方法终止。示例9.4具有相同优先级的线程class same priority public static void main(string args)new demoa(长江)。start();新示范(黄河)。启动();class demoa extends thread private string name;public DemoA(String s)super(s);this . name=s;public void run()for(int I=0;I=4;I)system . out . print

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论