线程状态
新建
线程对象一旦创建就进入了新生状态。
就绪
线程对象调用start()方法,进入就绪状态,注意:线程开启不一定立即执行,他是根据CPU调度执行的。
运行
CPU调度后进入运行状态,线程才真正执行线程体的代码块。
阻塞
当调用sleep,wait或同步锁定时,线程进入阻塞状态,阻塞解除后进入就绪状态。
死亡
线程中断后者结束,进入死亡状态,一旦进入死亡状态,就不能再次启动。
停止线程
不推荐使用JDK提供的stop(),destroy()方法,推荐让线程自己停下来。建议使用一个标志位进行终止变量,当flag=false时,则终止线程运行。
示例:
package com.xct.test;
public class TestStop implements Runnable{
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag){
System.out.println("线程启动"+i++);
}
}
public void stop(){
this.flag = false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i &#61; 0; i < 1000; i&#43;&#43;) {
System.out.println("main"&#43;i);
if(i &#61;&#61; 900){
testStop.stop();
System.out.println("线程该停止了");
}
}
}
}
线程休眠sleep
1.sleep指当前线程阻塞的毫秒数。
2.sleep存在异常InterruptedException。
3.sleep时间达到后线程进入就绪状态。
4.每个对象都有一把锁&#xff0c;sleep不会释放锁。
示例&#xff1a;通过sleep设置倒计时
package com.xct.test2;
public class TestSleep {
public static void main(String[] args) throws InterruptedException {
int num &#61; 10;
while (true){
Thread.sleep(1000);
System.out.println(num);
num--;
if(num<0){
break;
}
}
}
}
示例&#xff1a;通过sleep获取系统当前时间
package com.xct.test2;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep2 {
public static void main(String[] args) throws InterruptedException {
Date startTime &#61; new Date(System.currentTimeMillis());
while (true){
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
Thread.sleep(1000);
startTime &#61; new Date(System.currentTimeMillis());
}
}
}
线程礼让yield
1.让当前正在执行的线程暂停&#xff0c;但不阻塞。
2.将线程状态从运行状态转为就绪状态。
3.让CPU重新调度&#xff0c;礼让不一定成功&#xff0c;看CPU心情。
示例&#xff1a;
package com.xct.test2;
public class TestYield implements Runnable{
&#64;Override
public void run() {
System.out.println(Thread.currentThread().getName()&#43;"开始");
Thread.yield();
System.out.println(Thread.currentThread().getName()&#43;"结束");
}
public static void main(String[] args) {
new Thread(new TestYield(),"a").start();
new Thread(new TestYield(),"b").start();
}
}
Join合并线程
待此线程执行完成后&#xff0c;再执行其他线程&#xff0c;其他线程阻塞。&#xff08;想象为插队即可&#xff09;
示例&#xff1a;
package com.xct.test2;
public class TestJoin implements Runnable{
&#64;Override
public void run() {
for (int i &#61; 0; i < 800; i&#43;&#43;) {
System.out.println("vip"&#43;i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread &#61; new Thread(new TestJoin());
thread.start();
for (int i &#61; 0; i < 500; i&#43;&#43;) {
if(i &#61;&#61; 200){
thread.join();
}
System.out.println("普通用户"&#43;i);
}
}
}
总结
示例&#xff1a;查看线程状态
package com.xct.test2;
public class TestState {
public static void main(String[] args) throws InterruptedException {
Thread thread &#61; new Thread(()->{
for (int i &#61; 0; i < 5; i&#43;&#43;) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("/");
}
});
Thread.State state &#61; thread.getState();
System.out.println(state);
thread.start();
state &#61; thread.getState();
System.out.println(state);
while(state !&#61; Thread.State.TERMINATED){
Thread.sleep(1000);
state &#61; thread.getState();
System.out.println(state);
}
}
}