Java语言程序设计(第三版清华)第8章new.ppt
《Java语言程序设计(第三版清华)第8章new.ppt》由会员分享,可在线阅读,更多相关《Java语言程序设计(第三版清华)第8章new.ppt(67页珍藏版)》请在三一办公上搜索。
1、第九章 线程,2,目录,Java中的线程线程的生命周期Thread的子类创建线程使用Runable接口线程的常用方法线程的优先级线程同步在同步方法中使用wait()、notify 和notifyAll()方法挂起、恢复和终止线程本章小结,3,线程的概念,进程和线程的区别(Example9_11)进程一个独立程序的每一次运行称为一个进程,例如用字处理软件编辑文稿时,同时打开mp3播放程序听音乐,这两个独立的程序在同时运行,称为两个进程设置一个进程要占用相当一部分处理器时间和内存资源大多数操作系统不允许进程访问其他进程的内存空间,进程间的通信很不方便,编程模型比较复杂,进程通信方式 共享存储器系统
2、、消息传到机制、管道通信,4,线程一个程序中多段代码同时并发执行,称为多线程通过多线程,一个进程表面上看同时可以执行一个以上的任务并发创建线程比创建进程开销要小得多,线程之间的协作和数据交换也比较容易(进程独站资源)线程间共享资源(内存、代码、数据)有利于并行处理,线程的概念(续),5,线程的状态与生命周期,新建:当一个Thread类或其子类的对象被声明并创建时,新生的线程对象处于新建状态。此时它已经有了相应的内存空间和其他资源。就绪状态:线程等待 CPU运行状态:start()方法开始执行中断(阻塞)状态:sleep(),wait(),I/O 完成 or 发布 I/O 请求sleep():使
3、当前线程进入等待状态,参数设定其等待时间wait():使当前线程进入等待状态,调用nofify(),or notifyAll(),使其重新进入线程等待队列死亡:run()方法完成 stop()方法被调用,6,Life Cycle of Thread,7,诞生状态线程刚刚被创建就绪状态线程的 start 方法已被执行线程已准备好运行运行状态处理机分配给了线程,线程正在运行阻塞状态(Blocked)在线程发出输入/输出请求且必须等待其返回遇到用synchronized标记的方法而未获得其监视器暂时不能进入执行时休眠状态(Sleeping)执行sleep方法而进入休眠死亡状态线程已完成或退出,线程的
4、几种基本状态(续),8,主线程:Java应用程序总是从主类的main方法开始执行。当JVM加载代码,发现main方法之后,启动的线程称作“主线程”,该线程负责执行main方法。在main方法的执行中再创建的线程,就称为程序中的其它线程。如果main方法中没有创建其他的线程,那么当main方法执行完最后一个语句,JVM就会结束Java应用程序。如果main方法中又创建了其他线程,那么JVM就要在主线程和其他线程之间轮流切换,main方法即使执行完最后的语句,JVM也不会结束程序,JVM一直要等到程序中的所有线程都结束之后,才结束Java应用程序。,9,Java中创建线程的两种方法Thread 类
5、的子类创建线程对象,子类重写Thread类中的run()方法使用Thread类直接创建线程对象,但需要使用Runable接口,10,Thread类,Thread类在Java程序中创建多线程的方法之一是继承Thread类 封装了Java程序中一个线程对象需要拥有的属性和方法从Thread类派生一个子类,并创建这个子类的对象,就可以产生一个新的线程。这个子类应该重写Thread类的run()方法,在run方法中写入需要在新线程中执行的语句段。这个子类的对象需要调用start方法来启动,新线程将自动进入run方法。原线程将同时继续往下执行它位于java.lang包中,因而程序开头不用import任何
6、包就可直接使用例子:Example9_1.java,public class Example9_1 public static void main(String args)Lefthand left;Righthand right;left=new Lefthand();/创建线程 right=new Righthand();left.start();right.start();for(int i=1;i=3;i+)(我是主线程);,class Lefthand extends Thread public void run()for(int i=1;i=3;i+)(我是左手线程);class
7、Righthand extends Thread public void run()for(int i=1;i=3;i+)(我是右手线程);,我是主线程我是主线程我是主线程我是左手线程我是左手线程我是左手线程我是右手线程我是右手线程我是右手线程,12,Thread类(续),在新线程中完成计算某个整数的阶乘public class Ex8_1 public static void main(String args)System.out.println(main thread starts);FactorialThread thread=new FactorialThread(10);thread
8、.start();System.out.println(main thread ends);class FactorialThread extends Thread private int num;public FactorialThread(int num)this.num=num;,13,public void run()int i=num;int result=1;System.out.println(new thread started);while(i0)result=result*i;i=i-1;System.out.println(The factorial of+num+is+
9、result);System.out.println(new thread ends);运行结果main thread startsmain thread endsnew thread startedThe factorial of 10 is 3628800new thread ends,Thread类(续),14,结果说明main线程已经执行完后,新线程才执行完main函数调用thread.start()方法启动新线程后并不等待其run方法返回就继续运行(执行(),thread.run函数在一边独自运行,不影响原来的main函数的运行源程序修改如果启动新线程后希望主线程多持续一会再结束,可
10、在start语句后加上让当前线程(main)休息1毫秒的语句:try Thread.sleep(1);catch(Exception e);,Thread类(续),15,修改后运行结果main thread startsnew thread staredThe factorial of 10 is 3628800new thread endsmain thread ends运行结果说明新线程结束后main线程才结束例子 Ex8_1.java,Thread类(续)例8_1修改后运行结果,16,Thread类(续)常用API函数,17,Thread类(续)常用API函数,18,Thread类(续)
11、常用API函数,19,创建3个新线程,每个线程睡眠一段时间(06秒),然后结束public class Ex8_2 public static void main(String args)/创建并命名每个线程 TestThread thread1=new TestThread(thread1);TestThread thread2=new TestThread(thread2);TestThread thread3=new TestThread(thread3);(Starting threads);thread1.start();/启动线程1 thread2.start();/启动线程2 t
12、hread3.start();/启动线程3(Threads started,main endsn);,Thread类(续)例8_2,20,class TestThread extends Thread private int sleepTime;public TestThread(String name)super(name);/调用父类构造函数为线程命名 sleepTime=(int)(Math.random()*6000);public void run()try(getName()+going to sleep for+sleepTime);Thread.sleep(sleepTime)
13、;/线程休眠 catch(InterruptedException exception);(getName()+finished,Thread类(续)例8_2,21,运行结果Starting threadsThreads started,main endsthread1 going to sleep for 3519thread2 going to sleep for 1689thread3 going to sleep for 5565thread2 finishedthread1 finishedthread3 finished说明由于线程3休眠时间最长,所以最后结束,线程2休眠时间最短,
14、所以最先结束每次运行,都会产生不同的随机休眠时间,所以结果都不相同,Thread类(续)例8_2运行结果,22,Runnable接口,Runnable接口在编写复杂程序时相关的类可能已经继承了某个基类,而Java不支持多继承,在这种情况下,便需要通过实现Runnable接口来生成多线使用Thread创建线程对象时,通常使用的构造方法是:Thread(Runnable target)该构造方法中的参数是一个Runnable类型的接口,因此,在创建线程 对象时必须向构造方法的参数传递一个实现Runnable接口类的实例,该实例对象称作所创线程的目标对象.当线程调用start方法后,一旦轮到它来享用
15、CPU资源,目标对象就会自动调用接口中的run方法(接口回调).对于使用同一目标对象的线程,目标对象的成员变量自然就是这些线程的共享数据单元不同run()方法中的局部变量互不干扰。修改Ex8_1.java 例子:workspace/ThreadDemo2_2.java(java2006/example9/ThreadDemo1_1,ThreadDemo2_2)例子:Example9_3.java,TestThread extends Thread.,TestThread implements Runnable new Thread(Runnable th).start,23,使用Runnabl
16、e接口实现Ex8_1功能(使用一个目标对象)public class Ex8_1 public static void main(String args)System.out.println(main thread starts);FactorialThread t=new FactorialThread(10);new Thread(t).start();/创建Thread对象 System.out.println(new thread started,main thread ends);,Runnable接口(续)例8_1_1,24,class FactorialThread implem
17、ents Runnable private int num;public FactorialThread(int num)this.num=num;public void run()int i=num;int result=1;while(i0)result=result*i;i=i-1;System.out.println(The factorial of+num+is+result);System.out.println(new thread ends);,Runnable接口(续)例8_3,25,使用Runnable接口实现例8_2功能(使用不同目标对象)public class Ex8
18、_4 public static void main(String args)TestThread thread1=new TestThread();TestThread thread2=new TestThread();TestThread thread3=new TestThread();(Starting threads);new Thread(thread1,Thread1).start();new Thread(thread2,Thread2).start();new Thread(thread3,Thread3).start();(Threads started,main ends
19、n);,Runnable接口(续)例8_4,26,class TestThread implements Runnable private int sleepTime;public TestThread()sleepTime=(int)(Math.random()*6000);public void run()try(Thread.currentThread().getName()+going to sleep for+sleepTime);Thread.sleep(sleepTime);catch(InterruptedException exception);(Thread.current
20、Thread().getName()+finished);,Runnable接口(续)例8_4,Starting threadsThreads started,main endsThread2 going to sleep for 1498Thread1 going to sleep for 4544Thread3 going to sleep for 3251Thread2 finishedThread3 finishedThread1 finished,27,线程间的数据共享,代码共享多个线程的执行代码来自同一个类的run方法时,即称它们共享相同的代码数据共享当共享访问相同的对象时,即它们
21、共享相同的数据不同线程的run()方法中的局部变量互不干扰(Example9_5_1.java)使用Runnable接口可以轻松实现多个线程共享相同数据,只要用同一个实现了Runnable接口的实例作为参数创建多个线程即可(Ex8_5.java,p233-例9.3),class Example9_5_1 public static void main(String args)Move move=new Move();new Thread(move,zhangsan).start();new Thread(move,lisi).start();,class Move implements Run
22、nable/int i=0;public void run()int i=0;while(i=5)if(Thread.currentThread().getName().equals(zhangsan)i=i+1;System.out.println(Thread.currentThread().getName()+线程的局部变量i=+i);else if(Thread.currentThread().getName().equals(lisi)i=i+1;System.out.println(Thread.currentThread().getName()+线程的局部变量i=+i);try
23、Thread.sleep(800);catch(InterruptedException e),30,修改例8_4,只用一个Runnable类型的对象为参数创建3个新线程。public class Ex8_5 public static void main(String args)TestThread threadobj=new TestThread();(Starting threads);new Thread(threadobj,Thread1).start();new Thread(threadobj,Thread2).start();new Thread(threadobj,Threa
24、d3).start();(Threads started,main endsn);,线程间的数据共享(续)例8_5,31,class TestThread implements Runnable private int sleepTime;public TestThread()sleepTime=(int)(Math.random()*6000);public void run()try(Thread.currentThread().getName()+going to sleep for+sleepTime);Thread.sleep(sleepTime);catch(Interrupted
25、Exception exception);(Thread.currentThread().getName()+finished);,线程间的数据共享(续)例8_5,32,运行结果Starting threadsThread1 going to sleep for 966Thread2 going to sleep for 966Threads started,main endsThread3 going to sleep for 966Thread1 finishedThread2 finishedThread3 finished说明因为是用一个Runnable类型对象创建的3个新线程,这三个
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 语言程序设计 第三 清华 new
链接地址:https://www.31ppt.com/p-6510203.html