当前位置: 代码迷 >> J2ME >> 关于线程的有关问题
  详细解决方案

关于线程的有关问题

热度:4134   发布时间:2013-02-25 21:34:57.0
关于线程的问题
class Test implements Runnable
{
  Thread m_thread = null;
  Test(){
  m_thread = new Thread(this);
  m_thread.start();
  }
  public viod run(){
Thread curThread=Thread.currentThread();
while (m_thread == curThread) {
Sytem.out.println("hellword");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("out of thread");
  }
  public static void main(String argv[]){
Test a= new Test();
  }
}
什么时间out of thread会被打印??

------解决方案--------------------------------------------------------
一个线程循环调用自身的线程,你的代码会让机器over的你知道吗,呵呵,系统将会无休止地为你开辟无穷多个线程,也就是说你那个while循环根本没有机会执行到System.out.println("out of thread"); 而且你的Sytem.out.println("hellword"); 也不可能是在while循环里面被打印出来的,结果中不断出现hellword是系统开辟出来的不同进程所打印的
------解决方案--------------------------------------------------------
如果是只有1个线程的话,那么是永远不会退出的,因为m_thread == curThread是永真的
如果是4个的话,回按时间状态知道剩下最后一个一直循环
------解决方案--------------------------------------------------------
怎么这么怪.一直打印不出来.
------解决方案--------------------------------------------------------
up
------解决方案--------------------------------------------------------
m_thread == curThread是永真的,
因为在每个对象里他都是满足的
  相关解决方案