《第十章Java的多线程机制.ppt》由会员分享,可在线阅读,更多相关《第十章Java的多线程机制.ppt(43页珍藏版)》请在三一文库上搜索。
1、第十章 Java的多线程机制,程序是一段静态的代码,它是应用程序执行的蓝本,进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程,线程 是进程执行过程中产生的多条执行线索,线程是比进程执行更小的单位,10.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(InterruptedExce
2、ption 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=
3、new TeacherThread(); student=new StudentThread(); teacher.start(); student.start(); ,10.2多线程实现 Java中实现多线程应用有两种途径: 创建Thread类的子类 在程序中使用Runnable接口,10.2.1用Thread类的子类创建线程 一、只需从Thread类派生出一个子类,在子类中一定要重写run().例: public void run() . ,二、然后用该子类创建一个对象 StudentThread student=new StudentThread();,三、用start()方法启动线程
4、student.start();,在程序中实现多线程,关键性操作: 定义用户线程操作,即run()方法的实现 在适当的时候启动线程 例:ThreadTest.java,主线程,T1.start(),Tn.start(),T2.Start(),主线程,10.2.2 Runnable()接口 用Runnable()接口实现多线程时,也必须必须实现run()方法,也需用start()启动 线程,但此时常用Thread类的构造方法来创建线程对象,例:class BallThread extends Applet implements Runnable public void start() threa
5、d=new Thread(this); thread.start(); . private Thread thread; ,例:一个模拟小球平抛和自由落体的例子BallThread.java,API:java.lang.Thread Thread(Runnable target) 创建一个新线程,它调用target的run(), Target是一个实现了Runnable接口的类的实例,public class BallThread extends Applet implements Runnable Thread red,blue; Graphics redPen,bluePen; int t
6、=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.start(); ,public void run() while(true) t=t+1; if(Thread.currentThread()=red) if(t100)t=0;
7、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.fillOval(120+7*t,(int)(1.0/2*t*9.8),15,15); tryblue.sleep(40); catch(InterruptedException e) ,线
8、程机制实现的关键在于它的“并行性”,怎样才能让一个线程让出CPU,供其它线程使用呢?,API: start() 启动线程对象 run()用来定义线程对象被调度之后所执 行的操作,用户必须重写run()方法 yield()强制终止线程的执行 isAlive()测试当前线程是否在活动 sleep(int millsecond)使线程休眠一段时间,长短由参数所决定 Void Wait() 使线程处于等待状态,10.3线程的属性 10.3.1 线程的生命周期 线程一共有四种状态:新建(new) 可运行状态 (runnable) 死(dead) 堵塞(blocked),新建(new)线程对象刚刚创建,还
9、没有启动,此时还处于不可运行状态。如: Thread thread=new Thread(“test”) 此时线程thread处于新建状态 但已有了相应的内存空间以及其它资源,可运行状态(runnable) 此时的线程已经启动,控制已处于线程的run()方法之中,此时的线程可能运行,也可能不运行,取决于CPU是否空闲。,调用线程的start()方法可使线程处于“可运行”状态 thread.start();,二是当线程处于“可运行”状态时,调用了stop()方法结束了线程的运行,使其进入了死状态。 thread.stop();,线程死亡的原因有二: 一是run()方法中最后一个语句执行完毕,死(
10、dead),一个正在执行的线程因特殊原因,被暂停执行,进入堵塞状态,堵塞时线程不能进入队列排队,必须等到引起堵塞的原因消除,才可重新进入排队队列,引起堵塞的原因很多,不同原因要用不同的方法解除,sleep(),wait()是两个常用引起堵塞的方法,堵塞(blocked),当run()执行结束返回时,线程自动终止 使用stop()也可以终止线程的执行 在程序中常常调用interrupt()来终止线程, interrupt()不仅可中断正在运行的线程, 而且也能中断处于blocked状态的线程, 此时interrupt()会抛出一个InterruptedException异常 Java提供了几个用
11、于测试线程是否被中断的方法,10.3.2. 线程中断,API: java.lang.Thread Void interrupt() 向一个线程发送一个中断请求,同时把这个线程的“interrupted”状态置为true. 若该线程处于blocked状态,会抛出InterruptedException. Static boolean interrupted() 检测当前线程是否已被中断,并重置状态“interrupted”值为false,boolean isInterrupted() 检测当前线程是否已被中断,不改变状态“interrupted”值,例:一个多线程的例子BounceThread.
12、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 void actionPerformed( ActionEvent evt) Ball b=new Ball(canvas); b.start(); ); ,Java 的线程调
13、度采用优先级策略: 优先级高的先执行,优先级低的后执行;,多线程系统会自动为每个线程分配一个优先级,缺省时,继承其父类的优先级,任务紧急的线程,其优先较高,优先级的线程按“先进先出”的原则,10.4线程优先级BounceExpress.java,垃圾回收是一个优先级很低的线程,当CPU空闲,又没有别的高优先级线程在运行,此时垃圾回收被线程被激活,Thread类有三个与线程优先级有关的静态量: MAX_PRIORITY:最大优先权,值为10 MIN_PRIORITY:最小优先权,值为1 NORM _PRIORITY:默认优先权,值为5,API:java.lang.Thread Void setP
14、riority(int newPriority) 重置线程优先级 Int getPriority()获得当前线程优先级 Static void yield() 使当前线程放弃执行权,用setPriority()改变线程的优先级,例:BounceExpress.java, addButton(p,“Start“,new ActionListener() public void actionPerformed(ActionEvent evt) Ball b=new Ball(canvas,Color.black); b.setPriority(Thread.NORM_PRIORITY); b.st
15、art(););,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(); );,10.5线程同步,案例:会计和出纳同用一帐本的情况 会计负责存款、出纳负责取款,假设,会计每次存入30万,共存三次;然后出纳开始取款,每次取会计存款的一半,共取二次;这个过程一共重复三次。,如
16、果帐面原来有100万,那么会计、出纳操作完后,帐面应该如下:,会计第 一次存款后,会计第 三次存款后,会计第 二次存款后,出纳第 二次取款后,出纳第 一次取款后,出纳第 三次取款后,2、用money表示帐本,会计、出纳都要对其操作,3、设计一个 chunqu方法,会计、出纳利用这个方法对帐本money进行操作,问题分析: 1、设计两个线程,一个表示会计kuaiji、一个表示出纳 chuna,程序设计如下:,public void run() if(Thread.currentThread()=kuaiji| Thread.currentThread()=chuna) for(int i=1;i
17、=3;i+) chunqu(30); ,public void start() kuaiji.start(); chuna.start();,public void chunqu(int number) if(Thread.currentThread()=kuaiji) for(int i=1;i=3;i+) money=money+number; tryThread.sleep(1000); catch(InterruptedException e) text1.append(“n“+money); else if(Thread.currentThread()=chuna) for(int
18、i=1;i=2;i+) money=money-number/2; tryThread.sleep(1000); catch(InterruptedException e) text2.append(“n“+money); ,运行结果:,存在问题: 在大多数实用线程应用中,都 存在两个或两个以上的线程需要共享相同的对象,假设每个线程都调用了改变它状态的方法,就会产生这样的结果。,解决:引入线程同步机制- synchronized 用synchronized修饰的方法,当一个线程要使用该方法,就只能等待其它线程使用完后,方可使用。,public synchronized void chunqu(
19、 int number) if(Thread.currentThread()=kuaiji) for(int i=1;i=3;i+) money=money+number; tryThread.sleep(1000); catch(InterruptedException e) text1.append(“n“+money); ,10.6wait()、notify()和notifyall()方法,案例:一个售票的例子 本周上映影片天下无贼 票价:五元,Student1:用20元买票,售票员找不开钱,student1只能等待其它student2用零钱来买票,以便售票员可以把钱找开,问题可归纳为:
20、 当一个线程使用的同步方法中用到某个变量,而该变量需要等到其它线程修改后才能符合本线程的需要,此时要用wait()方法,使其处于阻塞状态,当其它线程使用该方法,对该变量进行修改后,引起阻塞的原因解除,此时可用notify()或notifyall()方法通知等待使用该方法的线程结束等待,继续执行,问题分析: 1、设计两个线程,一个表示student1、一个表示student2,2 变量five=2、ten=0、twenty=0表示五元、十元和二十元的张数,3、方法售票规则用来实现卖票过程,返回值是一个String,public synchronized String 售票规则(String st
21、u,int money) if(money=5) five=five+1; s=stu+“给您入场券“+“您的钱正好“; else if(money=20) while(five3) trywait(); catch(InterruptedException e) five=five-3; twenty=twenty+1; s=stu+“给您入场券“+“ 您给我20 ,找您15元“; notifyAll(); return s; ,public void run() if(Thread.currentThread()=student1) String s=sellperson.售票规则(“student1:“,20); text.append(“n“+s); else if(Thread.currentThread()=student2) String s=wang.售票规则(“student2:“,5); text.append(“n“+s); ,总结: 1、多线程的概念 2、多线程的实现方法 3、线程的生命周期 4、线程的调度原则 5、sleep 、wait方法 6、同步机制 7、唤醒机制,
链接地址:https://www.31doc.com/p-2584918.html