前言
上一篇我们共同学习了静态代理模式与Thread类的相似之处、Lambad表达式和其他类的实现方式的对比。本篇我们将学习线程的休眠、礼让、停止等操作和现成的状态。
一、线程的操作和状态
线程的操作:
- 线程终止(Stop)
- 线程休眠(Sleep)
- 线程礼让(Yield)
- 线程强制执行(Join)
线程的状态:
- 初始(NEW):新创建了一个线程对象,但还没有调用start()方法。
- 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running)两种状态笼统的称为“运行”。
线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。 - 阻塞(BLOCKED):表示线程阻塞于锁。
- 等待(WAITING):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)。
- 超时等待(TIMED_WAITING):该状态不同于WAITING,它可以在指定的时间后自行返回。
- 终止(TERMINATED):表示该线程已经执行完毕。
二、代码示例
1.线程的操作
代码如下(示例):
1.线程停止
//测试Stop
//1.建议线程正常停止--->利用次数不建议死循环。
//2.建议使用标志位--->设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法
public class TestStop implements Runnable {
//设置一个标志位
private boolean flag = true;
@Override
public void run() {
int i = 1;
while (flag){
System.out.println("run_Thread"+ i++);
}
}
public void Stop(){
this.flag = false;
}
public static void main(String[] args) throws InterruptedException {
TestStop testStop = new TestStop();
Thread thread = new Thread(testStop);
thread.start();
for(int i=0;i<1000;i++){
System.out.println("run_Main"+i);
if(i==200){
testStop.Stop();
System.out.println("线程终止");
}
}
}
}
运行结果:
2.线程休眠
//模拟倒计时
public class TestSleep2 {
public static void main(String[] args) {
TestSleep2 testSleep2 = new TestSleep2();
try {
testSleep2.tenDown();
}catch (InterruptedException e){
e.printStackTrace();
}
//获取系统时间
Date startTime = new Date(System.currentTimeMillis());
while(true){
try{
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
//模拟倒计时
public void tenDown() throws InterruptedException{
int num = 10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if(num<=0){
break;
}
}
}
}
运行结果:
3.线程礼让
//测试礼让
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{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程开始执行");
Thread.yield();//礼让
System.out.println(Thread.currentThread().getName()+"线程结束执行");
}
}
运行结果:
4.线程强制执行
//模拟倒计时
public class TestSleep2 {
public static void main(String[] args) {
TestSleep2 testSleep2 = new TestSleep2();
try {
testSleep2.tenDown();
}catch (InterruptedException e){
e.printStackTrace();
}
//获取系统时间
Date startTime = new Date(System.currentTimeMillis());
while(true){
try{
Thread.sleep(1000);
System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime));
startTime = new Date(System.currentTimeMillis());
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
//模拟倒计时
public void tenDown() throws InterruptedException{
int num = 10;
while (true){
Thread.sleep(1000);
System.out.println(num--);
if(num<=0){
break;
}
}
}
}
运行结果:
2.线程的状态
代码如下(示例):
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(100);
}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);
}
}
}
运行结果: