线程的四个状态
1,新生状态
2,就绪状态
3,运行状态
4,死亡状态
运行状态阻塞后进入就绪状态,死亡状态后不能进入新生状态,线程的对象一旦创建就进入到了新生状态(Thread t = new Thread()),当调用start()方法,线程立即进入就绪状态,但是并不一定立即执行,它需要等待cpu的调用,进入运行状态,线程才真正执行线程体的代码块,当调用sleep(),wait()或同步锁定时,线程进入阻塞状态,代码不往下执行,在等待着,同理,也不是调用上面的方法就立即阻塞,阻塞时间解除后,重新进入就绪状态,再一次等待cpu的调用,进入死亡状态后,不能在调用start()再次启动线程。每一个线程都有自己的工作空间,与主内存进行交互。
有四种原因进入就绪状态
1,调用start()方法
2,阻塞事件解除后
3,调用yield()方法
4,jvm自己从本地线程切换到其它线程
有四种原因导致阻塞
1,调用sleep()方法(占据资源)
2,调用wait()方法(不占据资源)
3,调用join()方法(插队)
4,io中的read()和write()
线程停止的办法不推荐使用,可以让代码自己执行完比如说有次数的循环,或者使用标识让线程终止
终止线程的方法
package cn.com.state;
//终止线程两种方法
//1 线程正常执行完毕 有限的次数
//2 外部干涉 加入标识
public class TestTerminate implements Runnable{
// private boolean flag = true;
// private String name;
//
// public TestTerminate(String name) {
// this.name = name;
// }
//
// @Override
// public void run() {
// int i = 0;
// while(flag) {
// System.out.println(name+"-->"+i++);
// }
// }
// public void terminate() {
// this.flag = false;
// }
// public static void main(String[] args) {
// TestTerminate tt = new TestTerminate("小红");
// new Thread(tt).start();
// for (int i = 0; i <= 99; i++) {
// if (i ==88) {
// tt.terminate();
// System.out.println("tt线程终止");
// }
// System.out.println("main--->"+i);
// }
// }
private boolean flag = true;
private String name;
public TestTerminate(String name) {
this.name = name;
}
@Override
public void run() {
int i = 0;
while(flag) {
System.out.println(name+"-->"+i++);
}
}
public void terminate() {
this.flag = false;
}
public static void main(String[] args) {
TestTerminate tt= new TestTerminate("小红");
new Thread(tt).start();
for (int i = 0; i <= 100; i++) {
if (i == 66) {
tt.terminate();
System.out.println("tt线程终止");
}
System.out.println("main-->"+i);
}
}
}
测试sleep()方法
package cn.com.thread;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestSleep {
public static void main(String[] args) {
// int num = 10;
// while(true) {
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println(num--);
// }
//倒计时
// Date endTime = new Date(System.currentTimeMillis()+1000*10);
// long end = endTime.getTime();
// while(true) {
// System.out.println(new SimpleDateFormat("mm:ss").format(endTime));
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// endTime = new Date(endTime.getTime()-1000);
// if (end - 10000 > endTime.getTime()) {
// break;
// }
// }
Date date = new Date(System.currentTimeMillis()+1000*10);
long end = date.getTime();
while(true) {
System.out.println(new SimpleDateFormat("hh:mm:ss").format(date));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
date = new Date(date.getTime()-1000);
if (end - 10000 > date.getTime()) {
break;
}
}
}
}
测试yield()方法
package cn.com.thread;
public class TestYield {
public static void main(String[] args) {
// Yield y1 = new Yield();
// Yield y2 = new Yield();
// new Thread(y1,"小红").start();
// new Thread(y2,"小明").start();
new Thread(()->{
for (int i = 0; i < 100; i++) {
System.out.println("lambda...."+i);
}
}).start();
for (int i = 0; i < 100; i++) {
if (i %20 == 0) {
Thread.yield();
}
System.out.println("main...."+i);
}
}
}
//class Yield implements Runnable {
// @Override
// public void run() {
// System.out.println(Thread.currentThread().getName()+"-->start");
// Thread.yield();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println(Thread.currentThread().getName()+"-->end");
// }
//}
测试join()方法
package cn.com.thread;
public class TestJoin {
public static void main(String[] args) {
//join插队 是一个成员方法,必须使用对象去调用它
// Thread t = new Thread(()->{
// for (int i = 0; i < 100; i++) {
// System.out.println("lambda..."+i);
// }
// });
// t.start();
// for (int i = 0; i <100 ; i++) {
// if (i == 15) {
// try {
// t.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// System.out.println("main..."+i);
// }
// Thread t = new Thread(()->{
// for (int i = 0; i < 100; i++) {
// System.out.println("t的线程:"+i);
// }
// });
// t.start();
// for (int i = 0; i < 100; i++) {
// if ( i == 50) {
// try {
// t.join(); //main被阻塞了
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// System.out.println("main的线程:"+i);
// }
System.out.println("爸爸和儿子买烟的故事");
new Thread(new Father()).start();
}
}
class Father extends Thread {
@Override
public void run() {
System.out.println("想抽烟,发现烟没了");
System.out.println("让儿子去买中华");
Thread t = new Thread(new Son());
t.start();
try {
t.join();//father被阻塞
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("孩子走丢了,老爸找孩子去了");
}
System.out.println("老爸接过烟,把零钱给了儿子");
}
}
class Son extends Thread {
@Override
public void run() {
System.out.println("接过老爸的钱出去了");
System.out.println("路边有个游戏厅,玩了10秒");
for (int i = 0; i < 10; i++) {
System.out.println(i+"秒过去了。。。");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("赶紧买烟去");
System.out.println("手拿一包中华回来了");
}
}