java线程(暂停、恢复、结束)前引


一、suspend、resume、stop  (过期的暂停、恢复、结束)

线程得益于它的run方法,在其中不断地循环来达到预期的目的,而很多时候,经常需要对这略过机械化的“小东西 ”进行一些控制。

在线程Thread方法中,原先存在着很方便、很人性化的控制,让其可以乖乖 暂停、恢复、结束,不过是一句话的事而已。

如下:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TEST1 {

	public static void main(String[] args) throws InterruptedException {
		DateFormat format=new SimpleDateFormat("HH:mm:ss");
		Thread printThread=new Thread(new Runner(),"printThread");
		printThread.start();
		TimeUnit.SECONDS.sleep(3);//main暂停3秒
		System.out.println("main suspend the printfThread at "+format.format(new Date()));
		printThread.suspend();//线程暂停
		TimeUnit.SECONDS.sleep(3);//main暂停3秒
		System.out.println("main resume the printfThread at "+format.format(new Date()));
		printThread.resume();
		TimeUnit.SECONDS.sleep(3);//main暂停3秒
		System.out.println("main stop the printfThread at "+format.format(new Date()));
		printThread.stop();
		TimeUnit.SECONDS.sleep(3);//main暂停3秒 此时printThread 已经停止 以下不会再有输出
	}
	static	class Runner implements Runnable{
		@Override
		public void run() {
			DateFormat format=new SimpleDateFormat("HH:mm:ss");
			while(true){
				System.out.println(Thread.currentThread().getName()+" Run at"+format.format(new Date()));
				try {
					Thread.sleep(1000);//睡眠1秒
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
<strong><span style="color:#ff0000;">输出结果:</span></strong><pre name="code" class="java">printThread Run at13:54:22
printThread Run at13:54:23
printThread Run at13:54:24
main suspend the printfThread at 13:54:25
main resume the printfThread at 13:54:28
printThread Run at13:54:28
printThread Run at13:54:29
printThread Run at13:54:30
main stop the printfThread at 13:54:31
</pre>


然而,suspend、resume、stop 具有一定的副作用,或者说它处理的不够好,虽然表面上它是达到了意图,但suspend()方法在调用暂停之后,该线程并不会去释放已经占有的资源(比如你使用了锁对象,用其进行一些同步操作,而suspend将此线程暂停之后却没有释放锁,导致其他线程无法获得对象,将会继续排队等待,一旦这样的操作多了,那么这就是另一种死锁的体现。suspend、resume、stop等方法并不能很好地解决线程的控制问题,反而还会造成不可预料的影响。因此,被舍弃使用。

暂停与恢复 一般以等待/通知 机制来实现。 条件循环 处理逻辑等。

结束线程以打破循环条件为主。

例:

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class Test {
public static void main(String[] args) {
	myThread one =new myThread(1);
	Thread aa=new Thread(one);
	aa.start();
	myThread two =new myThread(2);
	Thread aaa=new Thread(two);
	aaa.start();
	try {
		TimeUnit.SECONDS.sleep(3);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	//aa.interrupt();
	one.exit();
}
}

class myThread implements Runnable{
	private volatile boolean flag=true;
 private int i,k;
  public myThread(int k) {
	this.k=k;
}
	@Override
	public void run() {
	while(this.flag&&!Thread.currentThread().isInterrupted()){
		i++;
	}
		System.out.println(k+"Thread is stop and i="+i);
	}
	public void exit(){
		this.flag=false;
	} 
}

输出结果:  1Thread is stop and i=1567966856     运用interrupt 和exit 修改while 条件 均可达到结束效果。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值