第9章Java的多线程机制.PPT
《第9章Java的多线程机制.PPT》由会员分享,可在线阅读,更多相关《第9章Java的多线程机制.PPT(46页珍藏版)》请在三一办公上搜索。
1、第9章 Java的多线程机制,本章主要讲述了线程的概念,线程的创建,线程的管理。,例9.1用类Thread的子类创建线程class CalculateThread extends Thread private String name;public CalculateThread(String name)super(name);this.name=name;,public void run()System.out.println(name+线程说:先睡1s(秒),再计算圆周率的平方根);try Thread.sleep(1000);catch(InterruptedException ie)Sy
2、stem.out.println(圆周率的平方根为:+Math.sqrt(Math.PI);System.out.println(name+线程说:再见!);,class PrintThread extends Thread private String name;public PrintThread(String name)super(name);this.name=name;,public void run()System.out.println(name+线程说:我马上打印10颗星!);for(int i=1;i=10;i+)System.out.print(*);System.out.
3、println();System.out.println(name+线程说:再见!);,class ThreadTestpublic static void main(String args)CalculateThread t1=new CalculateThread(计算);PrintThread t2=new PrintThread(打印);System.out.println(启动计算线程t1);t1.start();System.out.println(启动打印线程t2);t2.start();System.out.println(main方法说:再见!);,该程序产生的输出是:启动计
4、算线程t1启动打印线程t2main方法说:再见!计算线程说:先睡1s(秒),再计算圆周率的平方根打印线程说:我马上打印10颗星!*打印线程说:再见!圆周率的平方根为:1.7724538509055159计算线程说:再见!,Java语言支持多线程,在一个java程序中可以同时执行多个线程,使得使用java程序的用户可以减少等待时间提高工作效率。线程是一个执行体,该执行体的指令流只有一个起点、一个终点,执行中的某时刻只有一个执行点。线程不能单独存在,只能是程序中的一个部分。在一个程序中可以使用多个线程,这些线程可以同时运行,每个线程完成不同的任务。,线程之间可以共享计算机系统分配给这个程序的资源(
5、为程序分配的内存和外部设备的使用权等)。线程之间可利用共享的内存空间进行信息交换(线程间通信)。在Java中线程是主动对象。主动对象不但能接收消息也能向其他对象发送消息。声明一个Thread的子类。在子类中重构方法run,再声明子类的构造方法。然后,创建该子类的对象。public viod start()throws IllegalThreadStateException启动当前线程,方法立即返回。若该线程启动过了,再次调用方法start则抛出IllegalThreadStateException异常。线程启动后若获得CPU使用权则调用本线程的run方法。,例9.2 通过实现接口Runnabl
6、e创建线程class CalculateClass implements Runnable private String name;public CalculateClass(String name)this.name=name;,public void run()System.out.println(name+线程说:先睡1s(秒),再计算圆周率的平方根);try Thread.sleep(1000);catch(InterruptedException ie)System.out.println(圆周率的平方根为:+Math.sqrt(Math.PI);System.out.println
7、(name+线程说:再见!);,class PrintClass implements Runnable private String name;public PrintClass(String name)this.name=name;,public void run()System.out.println(name+线程说:我马上打印10颗星!);for(int i=1;i=10;i+)System.out.print(*);System.out.println();System.out.println(name+线程说:再见!);,class RunnableTestpublic stat
8、ic void main(String args)CalculateClass calculateObj=new CalculateClass(计算);PrintClass printObj=new PrintClass(打印);Thread t1=new Thread(calculateObj);Thread t2=new Thread(printObj);System.out.println(启动计算线程t1);t1.start();System.out.println(启动打印线程t2);t2.start();,System.out.println(main方法说:再见!);,先声明一个
9、实现接口Runnable的类,利用该类创建主动对象,在用这个主动对象作为构造方法的参数来创建线程对象。public static void sleep(long millis)throws InterruptedException使当前线程放弃执行(睡眠)millis毫秒。若当前线程在睡眠过程中由其他线程中断则睡眠过程结束并抛出InterruptedException异常。public Thread(Runnable runObj)创建一新线程。该线程使用主动对象runObj的run方法。新线程的名字由系统产生。,例9.2线程状态的控制class CalculateThread extends
10、 Thread private String name;public CalculateThread(String name)super(name);this.name=name;,public void run()System.out.println(name+线程说:先睡1s(秒),再计算圆周率的平方根);try Thread.sleep(1000);catch(InterruptedException ie)System.out.println(圆周率的平方根为:+Math.sqrt(Math.PI);System.out.println(name+线程说:再见!);,class Pri
11、ntThread extends Thread private String name;public PrintThread(String name)super(name);this.name=name;,class RunnableTestpublic static void main(String args)CalculateClass calculateObj=new CalculateClass(计算);PrintClass printObj=new PrintClass(打印);Thread t1=new Thread(calculateObj);Thread t2=new Thre
12、ad(printObj);System.out.println(启动计算线程t1);t1.start();System.out.println(启动打印线程t2);t2.start();,System.out.println(main方法说:再见!);,先声明一个实现接口Runnable的类,利用该类创建主动对象,在用这个主动对象作为构造方法的参数来创建线程对象。public static void sleep(long millis)throws InterruptedException使当前线程放弃执行(睡眠)millis毫秒。若当前线程在睡眠过程中由其他线程中断则睡眠过程结束并抛出Int
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 多线程 机制
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-5309210.html