




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中国PVC-U绝缘耐燃电线套管数据监测报告
- 2025年中国LED室外单色显示屏数据监测报告
- 2025年中国EVA双面海绵胶带数据监测报告
- 2025年中国ABS床头落地式平板床数据监测研究报告
- 2025年中国2-氟氯苄数据监测研究报告
- 2025至2030年中国高频发生器市场分析及竞争策略研究报告
- 2025至2030年中国防雾镜片市场分析及竞争策略研究报告
- 2025至2030年中国钩型钢钉线卡市场分析及竞争策略研究报告
- 2025至2030年中国自动水溶胶复膜机市场分析及竞争策略研究报告
- 2025至2030年中国红木二胡市场分析及竞争策略研究报告
- GB/T 15231-2023玻璃纤维增强水泥性能试验方法
- 外出提携公章申请表
- 2023版押品考试题库必考点含答案
- 【本田轿车灯光系统常见故障分析及排除8200字(论文)】
- 昆明天大矿业有限公司寻甸县金源磷矿老厂箐-小凹子矿段(拟设)采矿权出让收益评估报告
- 尿动力学检查操作指南2023版
- 梦幻西游古龙服务端安装教程
- 食品安全地方标准 预制菜生产卫生规范
- 亮化工程竣工验收报告
- 《出生医学证明》单亲母亲情况声明
- PCS-915母差保护装置介绍
评论
0/150
提交评论