低级线程问题,近来帮帮我啊!!在线等
public static void main(String[] args)
{
try
{
Thread1 thread1 = new Thread1();
Thread2 thread2 = new Thread2();
while(true)
{
thread1.start();
thread1.sleep(1000);
thread2.start();
thread2.sleep(1000);
}
}catch(Exception e){e.printStackTrace();}
}
thread1跟thread2都是打印出一行字出来!
为什么我这样用不会循环啊???只打印出了2行就在那不动了,既不循环也不退出,为什么啊????
------解决方案--------------------你到底想知道什么?
提议不清 别人怎么你要干什么!
------解决方案--------------------package thread;
public class T1 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
MyThread t1 = new MyThread(1,1000);
MyThread t2 = new MyThread(2,2000);
t1.start();
t2.start();
}
}
class MyThread extends Thread{
private int id;
private int interval;
private static int i=0;
public MyThread(int id,int interval){
this.id = id;
this.interval = interval;
}
public void run(){
while(true){
System.out.println( " "+id+ ": "+i++);
try{
sleep(interval);
}catch(InterruptedException ie){
}
}
}
}
------解决方案--------------------thread1.run();
thread1.sleep(1000);
thread2.run();
thread2.sleep(1000);
------解决方案--------------------你想一下:县城是不是可以多次调用其start()方法再其被interrupt()之前?????正确的做法是把你的while循环写到线程体里,而不是在外面!!!
------解决方案--------------------关注