当前位置:网站首页>多线程——五大状态
多线程——五大状态
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); //更新状态
}
}
}
边栏推荐
猜你喜欢
随机推荐
CCF 201503-3 Festival
多数相合问题总结
js加密参数定位
js利用requestAnimationFrame实时检测当前动画的FPS帧率
Wechat applet jumps to other applets
@Import,Conditional和@ImportResourse注解
[tensorflow2 installation] tensorflow2.3-cpu installation pit avoidance guide!!!
NPM详解
Filter filter details (listeners and their applications)
Es6详解
ES6 detailed explanation
Advanced introduction to digital IC Design SOC
Detailed explanation of JDBC operation database
mysql历史数据补充新数据
小程序H5获取手机号方案
JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
framework打包合并脚本
SQL 题目整理
C函数不加括号的教训
GCD详解








