知识积累之线程的睡眠时间

近日在学习java的多线程编程,有一练习题:创建几个任务,它们各将睡眠一定时间,显示它们的睡眠时间并退出。

我刚开始写的代码是这样的:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class sleepAWhile implements Runnable{

	private static int taskCount=0;
	private final int id =taskCount++;
	
	private int time =0;
	public sleepAWhile(int x){
		this.time = x;
	}
	
	public void run() {
		
		while(--time>0){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}		
		}
		System.out.println("this is the "+id+" times");
	}	
	
	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i=0; i<5; i++){
			long t1 = System.currentTimeMillis();
			exec.execute(new sleepAWhile(i));
			long t2 = System.currentTimeMillis();
			System.out.println("time="+(t2-t1));
		}
		exec.shutdown();
	}
}

最终执行的结果是:

time=0
this is the 0 times
time=0
this is the 1 times
time=0
time=0
time=0
this is the 2 times
this is the 3 times
this is the 4 times

可以看到,没有收集到各任务的睡眠时间,作为初学者的我对这一结果十分困惑,在同事帮助下才明白原来exec.execute(new sleepAWhile(i));这一句只是把线程放到线程池中,其后面的语句可以立即执行。

后来我将代码改成了这样:

package thinking650;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class sleepAWhile implements Runnable{

	private static int taskCount=0;
	private final int id =taskCount++;
	
	private int time =0;
	public sleepAWhile(int x){
		this.time = x;
	}
	
	public void run() {
		long t1 = System.currentTimeMillis();
		while(--time>0){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}		
		}
		long t2 = System.currentTimeMillis();
		System.out.println("time="+(t2-t1));
		System.out.println("this is the "+id+" times");
	}	
	
	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i=0; i<5; i++){
			exec.execute(new sleepAWhile(i));
		}
		exec.shutdown();
	}
}

结果如下:

time=0
this is the 0 times
time=0
this is the 1 times
time=1000
this is the 2 times
time=2001
this is the 3 times
time=3002
this is the 4 times

这就收集到了各个线程睡眠的时间。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血大婶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值