Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章

上传人:E**** 文档编号:89346427 上传时间:2019-05-23 格式:PPT 页数:47 大小:473KB
返回 下载 相关 举报
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章_第1页
第1页 / 共47页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章_第2页
第2页 / 共47页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章_第3页
第3页 / 共47页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章_第4页
第4页 / 共47页
Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章_第5页
第5页 / 共47页
点击查看更多>>
资源描述

《Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章》由会员分享,可在线阅读,更多相关《Java JDK 6学习笔记 教学课件 ppt 作者 978-7-302-14995-8 第15章(47页珍藏版)》请在金锄头文库上搜索。

1、第15章,线程 线程入门 同步化议题 concurrent套件新增类别,继承Thread,继承java.lang.Thread类别,并重新定义run()方法 实例化您自定义的Thread类别 使用start()方法启动线程,继承Thread,public class EraserThreadextends Thread private boolean active; private String mask; /重新定义run()方法 public void run () while(isActive() System.out.print(mask); try /暂停目前的线程50毫秒 Thre

2、ad.currentThread().sleep(50); catch(InterruptedException e) e.printStackTrace(); ,继承Thread,/启动Eraser线程 EraserThread eraserThread = new EraserThread(#); eraserThread.start(); String password = scanner.next(); eraserThread.setActive(false);,在Java SE 6中可以使用System.console()来取得java.io.Console物件 使用Console

3、物件的readPassword()方法,就可以避免输入的密码被窥视的问题,实作Runnable界面,如果您的类别已经要继承某个类别,那么您就不能继承Thread类别 继承了Thread类别,您就不能再继承其它类别 实作java.lang.Runnable接口来定义具线程功能的类别 Runnable接口中定义一个run()方法要实作 在实例化一个Thread对象时,可以传入一个实作Runnable接口的对象作为自变量,实作Runnable界面,public class EraserimplementsRunnable /实作Runnable private boolean active; pri

4、vate String mask; /重新定义run()方法 public void run () while(isActive() System.out.print(mask); try /暂停目前的线程50毫秒 Thread.currentThread().sleep(50); catch(InterruptedException e) e.printStackTrace(); ,实作Runnable界面,/Eraser实作Runnable界面 Eraser eraser = new Eraser(#); /启动Eraser线程 Thread eraserThread = new Thre

5、ad(eraser); eraserThread.start(); String password = scanner.next(); eraser.setActive(false);,Daemon线程,一个Daemon线程是一个在背景执行服务的线程 如果所有的非Daemon的线程都结束了,则Daemon线程自动就会终止 从Main方法开始的是一个非Daemon线程 如果希望某个线程在产生它的线程结束后跟着终止,要将它设为Daemon线程,Daemon线程,Thread thread = new Thread( /这是匿名类别的写法 new Runnable() public void run

6、() while(true) System.out.print(“T“); ); /设定为Daemon线程 thread.setDaemon(true); thread.start();,使用setDaemon()方法来设定一个线程是否为Daemon线程 预设所有从Daemon线程产生的线程也是Daemon线程,线程生命周期,执行start()之后,线程进入Runnable状态,此时线程尚未真正开始执行 必须等待排班器(Scheduler)的排班,线程生命周期,线程有其优先权,由1(Thread.MIN_PRIORITY)到10(Thread.MAX_PRIORITY) 优先权越高,排班器越优

7、先排入执行,如果优先权相同,则输流执行(Round-robin方式),线程生命周期,如果您想要让目前线程礼让一下其它线程,让它们有机会取得执行权,您可以呼叫绪行绪的yield()方法,/ . Thread thread = new Thread(new Runnable() public void run() / while(true) / yield(); /暂时让出执行权 ); thread.start(); / ,线程生命周期,有几种状况会让线程进入Blocked状态 等待输入输出完成 呼叫sleep()方法 尝试取得对象锁定 呼叫wait()方法,线程生命周期,进入Blocked状态,

8、以下的几个对应情况让线程回到Runnable状态 输入输出完成 呼叫interrupt() 取得对象锁定 呼叫notify()或notifyAll(),线程生命周期,Thread thread = new Thread(new Runnable() public void run() try /暂停99999毫秒 Thread.sleep(99999); catch(InterruptedException e) System.out.println(“Im interrupted!“); ); thread.start(); thread.interrupt(); / interrupt i

9、t right now,线程的加入(join),当线程使用join()加入至另一个线程时,另一个线程会等待这个被加入的线程工作完毕,然后再继续它的动作 join()的意思表示将线程加入成为另一个线程的流程之一,线程的加入(join),Thread threadB = new Thread(new Runnable() public void run() try catch(InterruptedException e) e.printStackTrace(); ); threadB.start(); try / Thread B加入Thread A threadB.join(); catch(

10、InterruptedException e) e.printStackTrace(); ,线程的停止,不建议使用stop()来停止一个线程的运行,public class SomeThread implements Runnable private boolean isContinue = true; public void terminate() isContinue = false; public void run() while(isContinue) / . some statements ,线程的停止,不建议使用stop()来停止一个线程的运行,Thread thread = ne

11、w Thread(new SomeThread(); thread.start(); thread.interrupt();,ThreadGroup,每一个线程产生时,都会被归入某个线程群组 如果没有指定,则归入产生该子线程的线程群组中 可以自行指定线程群组,线程一但归入某个群组,就无法更换群组,ThreadGroup,java.lang.ThreadGroup类别正如其名,可以统一管理整个群组中的线程 ThreadGroup中的某些方法,可以对所有的线程产生作用 interrupt()方法可以interrupt群组中所有的线程 setMaxPriority()方法可以设定群组中线程所能拥有的

12、最大优先权,ThreadGroup threadGroup1 = new ThreadGroup(“group1“); ThreadGroup threadGroup2 = new ThreadGroup(“group2“); Thread thread1 = new Thread(threadGroup1, “group1s member“); Thread thread2 = new Thread(threadGroup2, “group2s member“);,ThreadGroup,想要一次取得群组中所有的线程来进行某种操作,可以使用enumerate()方法,Thread threa

13、ds = new ThreadthreadGroup1.activeCount(); threadGroup1.enumerate(threads);,ThreadGroup,uncaughtException()方法是当群组中某个线程发生非受检例外(Uncheckedexception)时,由执行环境呼叫进行处理,ThreadGroup threadGroup1 = /这是匿名类别写法 new ThreadGroup(“group1“) /继承ThreadGroup并重新定义以下方法 /在线程成员丢出unchecked exception /会执行此方法 public void uncaug

14、htException(Thread t, Throwable e) System.out.println(t.getName() + “: “ + e.getMessage(); ;,UncaughtExceptionHandler,可以让您的例外处理类别实作Thread.UncaughtExceptionHandler界面,并实现其uncaughtException()方法,public class ThreadExceptionHandler implements Thread.UncaughtExceptionHandler public void uncaughtException(

15、Thread t, Throwable e) System.out.println(t.getName() + “: “ + e.getMessage(); ,thread1.setUncaughtExceptionHandler(handler);,同步化,如果一个对象所持有的数据可以被多线程同时共享存取时,您必须考虑到数据同步的问题 数据同步指的是两份数据整体性、一致性,同步化,同步化,数据的不同步而可能引发的错误通常不易察觉 可能是在您程序执行了几千几万次之后,才会发生错误 这通常会发生在您的产品已经上线之后,甚至是程序已经执行了几年之后,同步化,public void setNameA

16、ndID(String name, String id) this.name = name; this.id = id; if(!checkNameAndIDEqual() System.out.println(count + “) illegal name or ID.“); count+; ,同步化,使用“synchronized“关键词,publicsynchronized void setNameAndID(String name, String id) this.name = name; this.id = id; if(!checkNameAndIDEqual() System.out.println(

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


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

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