Java语言程序设计第8章课件

上传人:我*** 文档编号:142099123 上传时间:2020-08-16 格式:PPT 页数:44 大小:135KB
返回 下载 相关 举报
Java语言程序设计第8章课件_第1页
第1页 / 共44页
Java语言程序设计第8章课件_第2页
第2页 / 共44页
Java语言程序设计第8章课件_第3页
第3页 / 共44页
Java语言程序设计第8章课件_第4页
第4页 / 共44页
Java语言程序设计第8章课件_第5页
第5页 / 共44页
点击查看更多>>
资源描述

《Java语言程序设计第8章课件》由会员分享,可在线阅读,更多相关《Java语言程序设计第8章课件(44页珍藏版)》请在金锄头文库上搜索。

1、1,本讲内容,线程基础 线程的概念 Thread类 Runnable接口 资源共享和线程同步 线程间的通信 线程的生命周期 线程的优先级 程序举例,2,线程的概念,进程:程序的一次执行 线程:一个进程中的多个控制流 通过多线程,一个进程表面上看同时可以执行一个以上的任务并发 多数程序设计语言不支持并发,支持多线程要借助于操作系统“原语(primitives)” Java是第一个支持内置线程操作的主流编程语言,线 程 基 础,3,Thread类简介,线程类需要继承Thread类 Thread 的构造函数public Thread( String threadName )public Thread

2、() 在线程的run方法中编写线程的主要任务 sleep 方法使线程休眠 interrupt 方法中断一个运行中的线程 isAlive方法检查线程的状态 setName方法设置线程名 join方法等待线程结束,以执行当前线程,线 程 基 础,4,例8_1 在新线程中计算整数的阶乘,public class Ex8_1 public static void main( String args ) System.out.println(main thread starts); FactorialThread thread= new FactorialThread(10); thread.start

3、(); System.out.println( new thread started,main thread ends ); / end main ,线 程 基 础,/ class FactorialThread controls thread execution class FactorialThread extends Thread private int num; public FactorialThread( int num ) this.num=num; public void run() int i=num; int result=1; while(i0) result=resul

4、t*i; i=i-1; System.out.println(The factorial of +num+ is +result); System.out.println(new thread ends); ,运行结果如下: main thread starts new thread started,main thread ends The factorial of 10 is 3628800 new thread ends,7,例8_2 创建3个新线程,每个线程睡眠任意0-6秒钟,然后结束。,public class Ex8_2 public static void main( String

5、 args ) /创建并命名每个线程 TestThread thread1 = new TestThread( thread1 ); TestThread thread2 = new TestThread( thread2 ); TestThread thread3 = new TestThread( thread3 ); System.out.println( Starting threads ); thread1.start(); / 启动线程1 thread2.start(); / 启动线程2 thread3.start(); / 启动线程3 System.out.println( Th

6、reads started, main endsn ); ,线 程 基 础,class TestThread extends Thread private int sleepTime; public TestThread( String name )/构造函数 super( name ); /调用基类构造函数为线程命名 /获得随机休息毫秒数 sleepTime = ( int ) ( Math.random() * 6000 ); public void run() /线程启动并开始运行后要执行的方法 try System.out.println( getName() + going to s

7、leep for + sleepTime ); Thread.sleep( sleepTime ); /线程休眠 catch ( InterruptedException exception ) ; /运行结束,给出提示信息 System.out.println( getName() + finished ); ,运行结果为: Starting threads Threads started, main ends thread1 going to sleep for 3519 thread2 going to sleep for 1689 thread3 going to sleep for

8、5565 thread2 finished thread1 finished thread3 finished,10,Runnable接口,如果希望一个已经有基类的类支持多线程,则不能再继承Thread类了(Java不支持多继承) 解决方法: 使类实现Runnable接口,再与Thread类结合 使用一个内部类提供Runnable的实现代码,将内部类的对象与Thread结合 相对于Thread类,它更适合于多个线程处理同一资源,线 程 基 础,11,例8_3 使用Runnable接口实现例8_1功能,public class Ex8_1 public static void main( Str

9、ing args ) System.out.println(main thread starts); FactorialThread t=new FactorialThread(10); new Thread(t).start(); System.out.println( new thread started,main thread ends ); / end main ,线 程 基 础,/ class FactorialThread controls thread execution class FactorialThread implements Runnable private int

10、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); ,13,例8_4 使用Runnable接口实现例8_2功能,public class Ex8_4 public static void main( Stri

11、ng 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,Thread2).start();

12、new Thread(thread3,Thread3).start(); System.out.println( Threads started, main endsn ); ,线 程 基 础,class TestThread implements Runnable private int sleepTime; public TestThread() /构造方法 /获得随机休息毫秒数 sleepTime = ( int ) ( Math.random() * 6000 ); public void run() /线程启动并开始运行后要执行的方法 try System.out.println(

13、Thread.currentThread().getName() + going to sleep for + sleepTime ); Thread.sleep( sleepTime ); /线程休眠 catch ( InterruptedException exception ) ; /运行结束,给出提示信息 System.out.println( Thread.currentThread().getName() + finished ); ,运行结果如下: Starting threads Thread1 going to sleep for 1487 Thread2 going to

14、sleep for 1133 Threads started, main ends Thread3 going to sleep for 2328 Thread2 finished Thread1 finished Thread3 finished,16,资源共享和线程同步,独立的同时运行的线程有时需要共享一些数据并且考虑到彼此的状态和动作。 例如生产/消费问题:生产线程产生数据流,然后这些数据流再被消费线程消费。具体来说,假设一个Java应用程序,其中有一个线程负责往文件写数据,另一个线程从同一个文件中往出都数据,因为涉及到同一个资源,这里是同一个文件,这两个线程必须保证某种方式的同步。 当

15、多个线程的执行代码来自同一个类的实例(若干个)时,即称它们共享相同的代码,当共享访问相同的对象时,即它们共享相同的数据。 使用Runnable接口可以轻松实现多个线程共享相同数据,只要用同一个实现了Runnable接口的实例作为参数创建多个线程就可以了。,线 程 基 础,17,例8_5 修改例8_4,/只用一个Runnable类型的对象为参数创建3个新线程。 public class 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(); System.out.println( Threads started, main endsn ); ,线

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号