浏览 2623 次
锁定老帖子 主题:『提问』高优先的线程竟然还有负数的时间??
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-09-05
问个问题 有2个子线程A,B A线程的优先级是NORM_PRIORITY+2,B是NORM_PRIORITY-2.运行时计算他们的时间(main线程是10S),时间的计算原理是每运行一次线程就时间加1。 当我运行时候,就发现了一个问题 高优先的线程竟然还有负数的时间(有时候) 请教为什么? 配置文件: 代码在这里 class clicker implements Runnable { int click =0; Thread t; private volatile boolean running =true; public clicker(int p){ t = new Thread(this); t.setPriority(p); } public void run(){ while (running) { click++; } } public void stop(){ running=false; } public void start(){ t.start(); } } class HiLoPri { public static void main(String args[]){ Thread.currentThread().setPriority(Thread.MAX_PRIORITY); clicker hi = new clicker(Thread.NORM_PRIORITY+2); clicker lo = new clicker(Thread.NORM_PRIORITY-2); lo.start(); hi.start(); try{ Thread.sleep(10000); }catch(InterruptedException e){ System.out.println("Main thread interrupted."); } lo.stop(); hi.stop(); try{ hi.t.join(); lo.t.join(); }catch(InterruptedException e){ System.out.println("IntrruptedException caught"); } System.out.println("Low-priority thread:"+lo.click); System.out.println("High-priority thread:"+hi.click); } }; 错误提示信息: 当我运行时候,就发现了一个问题 高优先的线程竟然还有负数的时间(有时候) 你的分析: 请教为什么? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-09-05
溢出
|
|
返回顶楼 | |
发表时间:2006-09-05
是long的原因吗?
好象换了就行了 但是总觉得还有别的原因把?? |
|
返回顶楼 | |