Java语言程序设计第8章课件.ppt
《Java语言程序设计第8章课件.ppt》由会员分享,可在线阅读,更多相关《Java语言程序设计第8章课件.ppt(45页珍藏版)》请在三一办公上搜索。
1、第8章 线程,郑 莉,JAVA语言程序设计,1,第8章 线程郑 莉JAVA语言程序设计1,本讲内容,线程基础线程的概念Thread类Runnable接口资源共享和线程同步线程间的通信线程的生命周期线程的优先级程序举例,2,本讲内容线程基础2,线程的概念,进程:程序的一次执行线程:一个进程中的多个控制流通过多线程,一个进程表面上看同时可以执行一个以上的任务并发多数程序设计语言不支持并发,支持多线程要借助于操作系统“原语(primitives)”Java是第一个支持内置线程操作的主流编程语言,线 程 基 础,3,线程的概念进程:程序的一次执行线 程 基 础3,Thread类简介,线程类需要继承Th
2、read类Thread 的构造函数public Thread( String threadName )public Thread()在线程的run方法中编写线程的主要任务sleep 方法使线程休眠interrupt 方法中断一个运行中的线程 isAlive方法检查线程的状态setName方法设置线程名join方法等待线程结束,以执行当前线程,线 程 基 础,4,Thread类简介线程类需要继承Thread类线 程 基 础,例8_1 在新线程中计算整数的阶乘,public class Ex8_1 public static void main( String args ) System.out.
3、println(main thread starts); FactorialThread thread= new FactorialThread(10); thread.start(); System.out.println( new thread started,main thread ends ); / end main ,线 程 基 础,5,例8_1 在新线程中计算整数的阶乘public class,/ class FactorialThread controls thread executionclass FactorialThread extends Thread private i
4、nt 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); ,6,/ class FactorialThread contr,运行结果如下:main thread startsnew thread starte
5、d,main thread endsThe factorial of 10 is 3628800new thread ends,7,运行结果如下:7,例8_2 创建3个新线程,每个线程睡眠任意0-6秒钟,然后结束。,ublic class Ex8_2 public static void main( String args ) /创建并命名每个线程 TestThread thread1 = new TestThread( thread1 ); TestThread thread2 = new TestThread( thread2 ); TestThread thread3 = new Tes
6、tThread( thread3 ); System.out.println( Starting threads ); thread1.start(); / 启动线程1 thread2.start(); / 启动线程2 thread3.start(); / 启动线程3 System.out.println( Threads started, main endsn ); ,线 程 基 础,8,例8_2 创建3个新线程,每个线程睡眠任意0-6秒钟,然后结,class TestThread extends Thread private int sleepTime; public TestThread
7、( String name )/构造函数 super( name ); /调用基类构造函数为线程命名 /获得随机休息毫秒数 sleepTime = ( int ) ( Math.random() * 6000 ); public void run() /线程启动并开始运行后要执行的方法 try System.out.println( getName() + going to sleep for + sleepTime ); Thread.sleep( sleepTime ); /线程休眠 catch ( InterruptedException exception ) ; /运行结束,给出提示
8、信息 System.out.println( getName() + finished ); ,9,class TestThread extends Threa,运行结果为:Starting threadsThreads started, main endsthread1 going to sleep for 3519thread2 going to sleep for 1689thread3 going to sleep for 5565thread2 finishedthread1 finishedthread3 finished,10,运行结果为:10,Runnable接口,如果希望一个
9、已经有基类的类支持多线程,则不能再继承Thread类了(Java不支持多继承)解决方法:使类实现Runnable接口,再与Thread类结合使用一个内部类提供Runnable的实现代码,将内部类的对象与Thread结合相对于Thread类,它更适合于多个线程处理同一资源,线 程 基 础,11,Runnable接口如果希望一个已经有基类的类支持多线程,则,例8_3 使用Runnable接口实现例8_1功能,public class Ex8_1 public static void main( String args ) System.out.println(main thread starts)
10、; FactorialThread t=new FactorialThread(10); new Thread(t).start(); System.out.println( new thread started,main thread ends ); / end main ,线 程 基 础,12,例8_3 使用Runnable接口实现例8_1功能publi,/ class FactorialThread controls thread executionclass FactorialThread implements Runnable private int num; public Fact
11、orialThread( 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); ,13,/ class FactorialThread contr,例8_4 使用Runnable接口实现例8_2功能,ublic class Ex8_4 public static vo
12、id main( String args ) /创建3个实现Runnable接口类的对象 TestThread thread1 = new TestThread(); TestThread thread2 = new TestThread(); TestThread thread3 = new TestThread(); System.out.println( Starting threads ); /分别以三个对象为参数创建三个新线程, /第二个参数为新线程命名并启动之 new Thread(thread1,Thread1).start(); new Thread(thread2,Threa
13、d2).start(); new Thread(thread3,Thread3).start(); System.out.println( Threads started, main endsn ); ,线 程 基 础,14,例8_4 使用Runnable接口实现例8_2功能publi,class TestThread implements Runnable private int sleepTime; public TestThread() /构造方法 /获得随机休息毫秒数 sleepTime = ( int ) ( Math.random() * 6000 ); public void r
14、un() /线程启动并开始运行后要执行的方法 try System.out.println( Thread.currentThread().getName() + going to sleep for + sleepTime ); Thread.sleep( sleepTime ); /线程休眠 catch ( InterruptedException exception ) ; /运行结束,给出提示信息 System.out.println( Thread.currentThread().getName() + finished ); ,15,class TestThread impleme
15、nts Ru,运行结果如下:Starting threadsThread1 going to sleep for 1487Thread2 going to sleep for 1133Threads started, main endsThread3 going to sleep for 2328Thread2 finishedThread1 finishedThread3 finished,16,运行结果如下:16,资源共享和线程同步,独立的同时运行的线程有时需要共享一些数据并且考虑到彼此的状态和动作。例如生产/消费问题:生产线程产生数据流,然后这些数据流再被消费线程消费。具体来说,假设一个
16、Java应用程序,其中有一个线程负责往文件写数据,另一个线程从同一个文件中往出都数据,因为涉及到同一个资源,这里是同一个文件,这两个线程必须保证某种方式的同步。当多个线程的执行代码来自同一个类的实例(若干个)时,即称它们共享相同的代码,当共享访问相同的对象时,即它们共享相同的数据。使用Runnable接口可以轻松实现多个线程共享相同数据,只要用同一个实现了Runnable接口的实例作为参数创建多个线程就可以了。,线 程 基 础,17,资源共享和线程同步独立的同时运行的线程有时需要共享一些数据并,例8_5 修改例8_4,/只用一个Runnable类型的对象为参数创建3个新线程。public cl
17、ass Ex8_5 public static void main( String args ) /只创建1个实现Runnable接口类的对象 TestThread thread = new TestThread(); System.out.println( Starting threads ); /只用一个Runnable类型对象为参数创建三个新线程, /命名并启动之 new Thread(thread,Thread1).start(); new Thread(thread,Thread2).start(); new Thread(thread,Thread3).start(); Syste
18、m.out.println( Threads started, main endsn ); ,线 程 基 础,18,例8_5 修改例8_4/只用一个Runnable类型的对象,class TestThread implements Runnable private int sleepTime; public TestThread()/构造函数 /获得随机休息毫秒数 sleepTime = ( int ) ( Math.random() * 6000 ); public void run() /线程启动并开始运行后要执行的方法 try System.out.println( Thread.cur
19、rentThread().getName() + going to sleep for + sleepTime ); Thread.sleep( sleepTime ); /线程休眠 catch ( InterruptedException exception ) ; /运行结束,给出提示信息 System.out.println( Thread.currentThread().getName() + finished ); ,19,class TestThread implements Ru,Starting threadsThread1 going to sleep for 966Thre
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 语言程序设计 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-1285849.html