版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第14章:多线程编程多线程常用操作方法线程的命名和取得No.方法类型描述1public Thread(Runnable target, String name)构造实例化线程对象,接收Runnable接口子类对象,同时设置线程名称2public final void setName(String name)普通设置线程名字3public final String getName()普通取得线程名字范例:观察线程的命名操作package cn.mldn.demo;class MyThread implements Runnable Overridepublic void run() System
2、.out.println(Thread.currentThread().getName();/ 当前线程名称public class ThreadDemo public static void main(String args) throws Exception MyThread mt = new MyThread();new Thread(mt, 线程A).start(); / 设置了线程的名字new Thread(mt).start(); / 未设置线程名字new Thread(mt, 线程B).start(); / 设置了线程的名字范例:观察如下程序代码package cn.mldn.d
3、emo;class MyThread implements Runnable Overridepublic void run() System.out.println(Thread.currentThread().getName();/ 获取线程名称public class ThreadDemo public static void main(String args) throws Exception MyThread mt = new MyThread();/ 线程类对象new Thread(mt, 线程对象).start(); / 设置了线程的名字mt.run(); / 对象直接调用run
4、()方法范例:子线程处理复杂逻辑package cn.mldn.demo;public class ThreadDemo public static void main(String args) throws Exception System.out.println(1、执行操作任务一。);/ 主线程执行new Thread()-/ 子线程负责统计int temp = 0 ;for (int x = 0 ; x / Runnable接口实例for (int x = 0; x 10; x+) System.out.println(Thread.currentThread().getName()
5、+ 、x = + x);try Thread.sleep(1000); / 暂缓1秒(1000毫秒)执行 catch (InterruptedException e) / 强制性异常处理e.printStackTrace(); ;for (int num = 0; num System.out.println(【BEFORE】准备睡觉10秒钟的时间,不要打扰我);try Thread.sleep(10000); / 预计准备休眠10秒System.out.println(【FINISH】睡醒了,开始工作和学习生活); catch (InterruptedException e) System.
6、out.println(【EXCEPTION】睡觉被打扰了,坏脾气像火山爆发一样袭来 ););thread.start(); / 线程启动Thread.sleep(1000); / 保证子线程先运行1秒if (!thread.isInterrupted() / 该线程中断?System.out.println(【INTERRUPT】敲锣打鼓欢天喜地的路过你睡觉的地方!);errupt(); / 中断执行范例:线程强制执行线程强制执行:public final void join() throws InterruptedExceptionpackage cn.mldn.dem
7、o;public class ThreadDemo public static void main(String args) throws Exception Thread mainThread = Thread.currentThread() ; / 获得主线程Thread thread = new Thread() - for (int x = 0 ; x 100 ; x +) if (x = 3) / 设置强制执行条件try mainThread.join();/ 强制执行线程任务 catch (InterruptedException e) e.printStackTrace(); t
8、ry Thread.sleep(100);/ 延缓执行 catch (InterruptedException e) e.printStackTrace();System.out.println(Thread.currentThread().getName() + 执行、x = + x);,玩耍的线程) ;thread.start();for (int x = 0 ; x for (int x = 0 ; x 100 ; x +) if (x % 3 = 0) Thread.yield();/ 线程礼让System.out.println(【YIELD】线程礼让, + Thread.curre
9、ntThread().getName();try Thread.sleep(100); catch (InterruptedException e) e.printStackTrace();System.out.println(Thread.currentThread().getName() + 执行、x = + x);,玩耍的线程) ;thread.start();for (int x = 0 ; x / 线程类对象for (int x = 0; x 10; x+) try Thread.sleep(1000);/ 暂缓执行 catch (InterruptedException e) e.printStackTrace();System.out.println(Thread.currentThread().getName() + 执行。);Thread threadA = new Thread(run, 线程对象A);/ 线程对象Thread threadB = new Thread(run, 线程对象B);/ 线程对象Thread threadC = new Thread(run, 线程对象C);/ 线程对象threadA.setPriority(Thread.MIN_PRIORITY);/ 修改线程优先级threadB.se
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 保育员培训-空气清洁消毒
- 读后续写+极地惊魂:公园里的不速之客+讲义 高三上学期新高考联合质量测评9月联考英语试题
- 扶养老人协议书(标准版)
- 2024教师聘用协议书范文(32篇)
- 教师个人事迹材料500字(10篇)
- 中职教师年度述职报告范文(3篇)
- 教师国培学习心得体会范文(33篇)
- 新学期教师国旗下讲话(34篇)
- 沟通礼仪与审美 课件 第二章第二节 赞美与批评艺术
- 2024年硬胶囊剂机械合作协议书
- 武术教案(长拳一段)(共5页)
- 物理化学实验教材电子
- 四川建龙软件资料全套表格
- 局部氧疗(TOT)治疗创面
- 承接查验协议书
- 警惕‘委托代办邮政业务’之规定被曲解与滥用
- 内部控制风险评估报告
- 新航空运费计算
- XX县民办教育发展状况的调研报告
- 吊篮计算书(精编版)
- 在比较还原中解读《秋天的怀念》
评论
0/150
提交评论