当前位置:网站首页>多线程——五大状态
多线程——五大状态
2022-07-25 09:27:00 【看小虫子】
五大状态,新生,就绪,运行,阻塞,死亡
停止状态
1,建议线程正常停止–利用次数自动停止,不建议死循环
2,建议使用标志位–设置一个标志位
3,不要使用stop或者destroy等过时的方法
package ThreadStatus;
public class TestStop implements Runnable{
//1,设置一个标志位
private boolean flag=true;
public static void main(String[] args) {
TestStop ts=new TestStop();
new Thread(ts).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
if(i==900){
//调用stop方法切换标志位,让线程停止
ts.stop();
System.out.println("线程该停止了");
}
}
}
@Override
public void run() {
int i=0;
while(flag){
System.out.println("run--Thread"+i++);
}
}
//,设置一个公开的方法停止线程,抓换标志位
public void stop(){
this.flag=false;
}
}
Sleep
模拟网络延时:放大问题的发生性
package ThreadStatus;
import RunnableStudy.Demo02;
import java.text.SimpleDateFormat;
import java.util.Date;
//模拟倒计时
public class TestSleep implements Runnable {
public static void main(String[] args) {
//打印系统当前时间
String simpleDateFormat=new SimpleDateFormat("HH:mm:ss").format(new Date());
Date start=new Date(System.currentTimeMillis());//获取系统当前时间
while(true){
try {
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(start));
start=new Date(System.currentTimeMillis());//更新当前时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
}
//模拟倒计时
public static void tenDown() throws InterruptedException {
int num=10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if(num<=0){
break;
}
}
}
}
礼让
礼让不一定成功,看CPU心情
package ThreadStatus;
//测试礼让线程
public class TestYield {
public static void main(String[] args) {
MyYield my=new MyYield();
new Thread(my,"A").start();
new Thread(my,"B").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();
System.out.println(Thread.currentThread().getName()+"线程停止执行");
}
}
join插队
插队之后,结束之前,不执行其它线程
package ThreadStatus;
//测试join方法,想像为插队
public class TestJoin implements Runnable{
public static void main(String[] args) throws InterruptedException {
TestJoin te= new TestJoin();
Thread thread = new Thread(te);
thread.start();
//主线程的方法
for (int i = 0; i < 1000; i++) {
if(i==200){
thread.join();//插队
}
System.out.println("main"+i);
}
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println("VIP来了"+i);
}
}
}
线程的观测
package ThreadStatus;
//观察测试线程状态
public class TestStatueDemo01 {
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) {
e.printStackTrace();
}
}
System.out.println("........");
});
//观察状态
Thread.State state =thread.getState();
System.out.println(state);//NEW
//观察启动后
thread.start();
state =thread.getState();
System.out.println(state);
while(state!=Thread.State.TERMINATED){
//只要线程不终止,就一直处于输出状态
Thread.sleep(100);
state=thread.getState();
System.out.println(state); //更新状态
}
}
}
边栏推荐
- UE4 外部打开exe文件
- canal实现mysql数据同步
- Download and installation of QT 6.2
- SQL 题目整理
- UE4源码的获取和编译
- See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent
- Yarn quick reference manual
- IO流中的输入流
- The way of code neatness -- hit the pain point directly
- 【成长必备】我为什么推荐你写博客?愿你多年以后成为你想成为的样子。
猜你喜欢

Introduction to armv8 architecture

数据库MySQL详解

File upload function

GCD详解

小程序分享功能
![[deployment of deep learning model] deploy the deep learning model using tensorflow serving + tornado](/img/62/78abf16bb6c66726c6e394c9fb4f81.png)
[deployment of deep learning model] deploy the deep learning model using tensorflow serving + tornado

安装 oh my zsh

Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)

DHCP的配置(以华为eNSP为例)
![[recommended collection] with these learning methods, I joined the world's top 500 - the](/img/95/e34473a1628521d4b07e56877fcff1.png)
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
随机推荐
史上最全面的UE4 文件操作,打开,读、写,增、删、改、查
Common methods of nodejs version upgrade or switching
yarn速查手册
SQL 题目整理
【近万字干货】别让你的简历配不上你的才华——手把手教你制作最适合你的简历
C3D模型pytorch源码逐句详析(二)
CCF 201509-2 date calculation
微信小程序跳转其他小程序
【成长必备】我为什么推荐你写博客?愿你多年以后成为你想成为的样子。
CCF 201512-4 delivery
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
Vant problem record
一.初始MySQL,MySQL安装、配置环境、初始化
多数相合问题总结
CCF 201509-4 Expressway
IDEA整体字体大小修改
js数字千位分割的常用方法
C3D模型pytorch源码逐句详析(一)
用户喜好
Internal structure of SOC chip