第七章多钱程机制概要

上传人:今*** 文档编号:108439799 上传时间:2019-10-24 格式:PPT 页数:43 大小:260KB
返回 下载 相关 举报
第七章多钱程机制概要_第1页
第1页 / 共43页
第七章多钱程机制概要_第2页
第2页 / 共43页
第七章多钱程机制概要_第3页
第3页 / 共43页
第七章多钱程机制概要_第4页
第4页 / 共43页
第七章多钱程机制概要_第5页
第5页 / 共43页
点击查看更多>>
资源描述

《第七章多钱程机制概要》由会员分享,可在线阅读,更多相关《第七章多钱程机制概要(43页珍藏版)》请在金锄头文库上搜索。

1、第七章 Java的多线程机制,程序是一段静态的代码,它是应用程序执行的蓝本,进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程,线程 是进程执行过程中产生的多条执行线索,线程是比进程执行更小的单位,7.1什么是线程 一。程序、进程与线程,例:ThreadTest.java,class StudentThread extends Thread public void run() for(int i=0;i=5;i+) System.out.println(“You are Students!“); trysleep(500); catch(InterruptedExcep

2、tion e) ,class TeacherThread extends Thread public void run() for(int i=0;i=5;i+) System.out.println(“I am a Teacher!“); try sleep(300); catch(InterruptedException e) . ,public class ThreadTest static StudentThread student; static TeacherThread teacher; public static void main(String args) teacher=n

3、ew TeacherThread(); student=new StudentThread(); teacher.start(); student.start(); ,7.2多线程实现 Java中实现多线程应用有两种途径: 创建Thread类的子类 在程序中使用Runnable接口,7.2.1用Thread类的子类创建线程,一、只需从Thread类派生出一个子类,在子类中一定要重写run().例:,class StudentThread extends Thread public void run() for(int i=0;i=5;i+) System.out.println(“You ar

4、e Students!“); trysleep(500); catch(InterruptedException e) ,二、然后用该子类创建一个对象 StudentThread student=new StudentThread();,三、用start()方法启动线程 student.start();,在程序中实现多线程,关键性操作: 定义用户线程操作,即run()方法的实现 在适当的时候启动线程 例:ThreadTest.java,7.2.2 Runnable()接口 用Runnable()接口实现多线程时,也必须实现run()方法,也需用start()启动 线程,但此时常用Thread类

5、的构造方法来创建线程对象,例:class BallThread extends Applet implements Runnable public void start() thread=new Thread(this); thread.start(); . private Thread thread; ,例:一个模拟小球平抛和自由落体的例子BallThread.java,API:java.lang.Thread Thread(Runnable target),创建一个新线程,Target是一个实现了Runnable接口的类的实例,它调用target的run(),public class Ba

6、llThread extends Applet implements Runnable Thread red,blue; Graphics redPen,bluePen; int t=0; public void init() red=new Thread(this); blue=new Thread(this); redPen=getGraphics(); bluePen=getGraphics(); redPen.setColor(Color.red); bluePen.setColor(Color.blue);,public void start() red.start(); blue.

7、start(); ,public void run() while(true) t=t+1; if(Thread.currentThread()=red) if(t100)t=0; redPen.clearRect(0,0,110,400); redPen.fillOval(50,(int)(1.0/2*t*9.8),15,15); tryred.sleep(40); catch(InterruptedException e) ,else if(Thread.currentThread()=blue) bluePen.clearRect(120,0,900,500); bluePen.fill

8、Oval(120+7*t,(int)(1.0/2*t*9.8),15,15); tryblue.sleep(40); catch(InterruptedException e) ,线程机制实现的关键在于它的“并行性”,怎样才能让一个线程让出CPU,供其它线程使用呢?,API: start() 启动线程对象 run()用来定义线程对象被调度之后所执 行的操作,用户必须重写run()方法 yield()强制终止线程的执行 isAlive()测试当前线程是否在活动 sleep(int millsecond)使线程休眠一段时间,长短由参数所决定 Void Wait() 使线程处于等待状态,7.3.1

9、线程的生命周期,新建(new)线程对象刚刚创建,还没有启动,此时还处于不可运行状态。如:,7.3线程的属性,Thread thread=new Thread(“test”),此时线程thread处于新建状态,但已有了相应的内存空间以及其它资源,线程一共有四种状态:新建(new) 可运行状态(runnable) 死(dead) 堵塞(blocked),可运行状态(runnable),此时的线程可能运行,也可能不运行,取决于CPU是否空闲。,调用线程的start()方法可使线程处于“可运行”状态 thread.start();,此时的线程已经启动,控制已处于线程的run()方法之中,二是当线程处于

10、“可运行”状态时,调用了stop()方法结束了线程的运行,使其进入了死状态。 thread.stop();,线程死亡的原因有二: 一是run()方法中最后一个语句执行完毕,死(dead),一个正在执行的线程因特殊原因,被暂停执行,进入堵塞状态,堵塞时线程不能进入队列排队,必须等到引起堵塞的原因消除,才可重新进入排队队列,引起堵塞的原因很多,不同原因要用不同的方法解除,sleep(),wait()是两个常用引起堵塞的方法,堵塞(blocked),当run()执行结束返回时,线程自动终止 使用stop()也可以终止线程的执行 在程序中常常调用interrupt()来终止线程, interrupt(

11、)不仅可中断正在运行的线程, 而且也能中断处于blocked状态的线程, 此时interrupt()会抛出一个InterruptedException异常 Java提供了几个用于测试线程是否被中断的方法,7.3.2. 线程中断,API: java.lang.Thread Void interrupt() 向一个线程发送一个中断请求,同时把这个线程的“interrupted”状态置为true. 若该线程处于blocked状态,会抛出InterruptedException. Static boolean interrupted() 检测当前线程是否已被中断,并重置状态“interrupted”值

12、为false,boolean isInterrupted() 检测当前线程是否已被中断,不改变状态“interrupted”值,例:一个多线程的例子BounceThread.java,class Ball extends Thread public Ball(JPanel b) box=b; ,public void run() try draw(); for(int i=1;i=1000;i+) move(); sleep(5); catch(InterruptedException e) , addButton(p,“Start“,new ActionListener() public v

13、oid actionPerformed( ActionEvent evt) Ball b=new Ball(canvas); b.start(); ); ,Java 的线程调度采用优先级策略: 优先级高的先执行,优先级低的后执行;,多线程系统会自动为每个线程分配一个优先级,缺省时,继承其父类的优先级,任务紧急的线程,其优先较高,优先级的线程按“先进先出”的原则,7.4线程优先级BounceExpress.java,垃圾回收是一个优先级很低的线程,当CPU空闲,又没有别的高优先级线程在运行,此时垃圾回收被线程被激活,Thread类有三个与线程优先级有关的静态量: MAX_PRIORITY:最大优

14、先权,值为10 MIN_PRIORITY:最小优先权,值为1 NORM _PRIORITY:默认优先权,值为5,API:java.lang.Thread Void setPriority(int newPriority) 重置线程优先级 Int getPriority()获得当前线程优先级 Static void yield() 使当前线程放弃执行权,用setPriority()改变线程的优先级,例:BounceExpress.java, addButton(p,“Start“,new ActionListener() public void actionPerformed(ActionEve

15、nt evt) Ball b=new Ball(canvas,Color.black); b.setPriority(Thread.NORM_PRIORITY); b.start(););,addButton(p,“Express“,new ActionListener() public void actionPerformed(ActionEvent evt) for(int i=0;i5;i+) Ball b=new Ball(canvas,Color.red); b.setPriority(Thread.NORM_PRIORITY+2); b.start(); );,7.5线程同步,案例

16、:会计和出纳同用一帐本的情况 会计负责存款、出纳负责取款,假设,会计每次存入30万,共存三次;然后出纳开始取款,每次取会计存款的一半,共取二次;这个过程一共重复三次。,如果帐面原来有100万,那么会计、出纳操作完后,帐面应该如下:,会计第 一次存款后,会计第 三次存款后,会计第 二次存款后,出纳第 二次取款后,出纳第 一次取款后,出纳第 三次取款后,2、用money表示帐本,会计、出纳都要对其操作,3、设计一个 chunqu方法,会计、出纳利用这个方法对帐本money进行操作,问题分析: 1、设计两个线程,一个表示会计kuaiji、一个表示出纳 chuna,程序设计如下:,public void run() if(Thread.currentTh

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

当前位置:首页 > 高等教育 > 大学课件

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