当前位置: 首页 > news >正文

线程停止、休眠、礼让、强制执行、观测线程状态

一、线程停止:

1.不推荐使用jdk提供的stop()、destroy()方法。【已废弃】

2.推荐线程自己停下,建议使用一个标识位进行终止变量,当flag=false,则终止线程运行

//测试stop
//1.建议线程正常停止--》利用次数,不建议死循环
//2.建议使用一个标识符public class TestStop implements Runnable{private boolean flag = true;@Overridepublic void run() {int i = 0;while (flag){System.out.println("run----->" + i++);}}public void stop(){this.flag = false;}public static void main(String[] args) {TestStop testStop = new TestStop();new Thread(testStop).start();for (int i = 0; i < 1000; i++) {System.out.println("main---->" + i);if (i==900){testStop.stop();System.out.println("线程停止了");}}}
}

二、线程休眠:

1.sleep (时间)指定当前线程阻塞的毫秒数;

2. sleep存在异常InterruptedException;

3.sleep时间达到后线程进入就绪状态;

4.sleep可以模拟网络延时,倒计时等。

5.每一个对象都有一个锁,sleep不会释放锁;

public class TestSleep1 {public static void main(String[] args) {try {tenDown();} catch (InterruptedException e) {throw new RuntimeException(e);}}//模拟倒计时public static void tenDown() throws InterruptedException {int num = 10;while (true){Thread.sleep(1000);System.out.println(num--);if (num == 0){break;}}}}
public static void main(String[] args) {//打印当前系统时间Date date = new Date(System.currentTimeMillis());//获取系统当前时间while (true){try {Thread.sleep(1000);System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));date = new Date(System.currentTimeMillis());//更新系统时间} catch (InterruptedException e) {throw new RuntimeException(e);}}}

三、线程礼让:

1.礼让线程,让当前正在执行的线程暂停,但不阻塞

2.将线程从运行状态转为就绪状态

3.让cpu重新调度,礼让不一定成功!看CPU心情

public class TestYield {public static void main(String[] args) {MyYield myYield = new MyYield();new Thread(myYield,"a").start();new Thread(myYield,"b").start();}
}class MyYield implements Runnable{@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+ "线程开始");Thread.yield();//礼让System.out.println(Thread.currentThread().getName()+ "线程结束");}
}

四、线程强制执行:

Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞

public class TestJoin implements Runnable{@Overridepublic void run() {for (int i = 1; i <= 100 ; i++) {System.out.println("线程VIP来了" + i);}}public static void main(String[] args) {TestJoin testJoin = new TestJoin();Thread thread = new Thread(testJoin);thread.start();for (int i = 1; i < 1000; i++) {if (i ==100){try {thread.join();//插队} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("main" + i);}}
}

五、观测线程状态:

public class TestState {public static void main(String[] args) throws InterruptedException {Thread thread = new Thread(()->{for (int i = 0; i < 5; i++) {try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("////////");});//观察状态Thread.State state = thread.getState();System.out.println(state);//NEW//观察启动后thread.start();//启动线程state = thread.getState();System.out.println(state);//Runwhile (state != Thread.State.TERMINATED){//只要线程不终止,就一直输出状态Thread.sleep(100);state = thread.getState();//更新线程状态System.out.println(state);}}
}
http://www.dtcms.com/a/532164.html

相关文章:

  • 复盘|嵌入式Linux驱动开发之I2C子系统
  • AI Agent常用的RAG有哪些种,分别适用于什么情况
  • 对中兴光猫zteOnu.exe项目的简单分析(提供下载地址)
  • 有没有专门做中考卷子的网站网站建设培训要多久
  • 做网站图片用什么格式最好个人网站备案能做宣传用么
  • JAVA1026 方法;类:抽象类、抽象类继承;接口、接口继承 Linux:Mysql
  • 密码学系列 - 零知识证明(ZKP) - NTT与MSM的总结
  • 《解决界面交互痛点:WaterFlow 瀑布流、双层嵌套滚动、键盘避让与跨 Ability 跳转实践》
  • 网页设计建立站点步骤做网站的广告词
  • Spring Boot3零基础教程,生命周期启动加载机制,笔记64
  • StarGantt在线甘特图最新版本评测
  • 43.渗透-Kali Linux-工具-Ettercap(dns欺骗)
  • 详解C++中的迭代器
  • 区块链论文速读 CCF A--USENIX Security 2025(2)
  • 基于区块链的新能源管理平台的设计与实现(源码+文档)
  • 2025年10月26日 AI大事件
  • 企业建站个人建站源码沧州专业网站建设公司
  • 基于springboot的电影评论网站系统设计与实现
  • 1.2.1.3 大数据方法论与实践指南-一种跨团队业务结算方式探索
  • 西安地产网站建设百度手机管家
  • NoSQL 简介
  • C++ 分治 快速排序优化 三指针快排 力扣 面试题 17.14. 最小K个数 题解 每日一题
  • 网站建设业务员怎么做适合建设网站的国外服务器
  • 【第1章】基于FPGA的图像形态学处理学习教程——目录
  • stm32单片机命名规则,c6t6只有32KB,c8t6有64KB
  • 基于华为设备的 OSPF+MSTP+DHCP+NAT 综合网络架构实现
  • 使用uniapp——实现微信小程序的拖拽排序(vue3+ts)
  • 5.23基于 LabVIEW 的科学计算器设计
  • STM32 异常和中断
  • OWL 简介