class TestThread1 extends Thread
{
int i;
public static boolean running = false;
public TestThread1()
{
i = 0;
}
public void run()
{
System.out.println("TestThread1中的if语句外语句块已经被执行!");
if(running)
while(true)
{
System.out.println("Running in TestThread2 has been changed!");
i++;
if(i == 50)
break;
System.out.println("TestThead1 run() called!");
} }
} class TestThread2 extends Thread
{
int j;
public TestThread2()
{
j = 0;
} public void run()
{
TestThread1.running = true;
while(true)
{
j++;
System.out.println("TestThead2 run() called!");
if(j == 10)
break;
}
}
} public class Test
{
public static void main(String[] args)
{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
//t1.setPriority(6);
//t2.setPriority(7);
t1.start();
t2.start();
}
}大家先看看执行结果!t2执行后,就会把TestThread1里面的running设置为true,即
TestThread1中的run()里面被if罩住的代码块就会执行。但结果却和我预期的完全不一样,多次测试,结果就是一样。如下
==============================
TestThread1中的if语句外语句块已经被执行!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!==============================为什么,t1执行到TestThread1中的 System.out.println("TestThread1中的if语句外语句块已经被执行!");
后,下面就不执行了呢!=======希望大家踊跃发言。给个说法!
{
int i;
public static boolean running = false;
public TestThread1()
{
i = 0;
}
public void run()
{
System.out.println("TestThread1中的if语句外语句块已经被执行!");
if(running)
while(true)
{
System.out.println("Running in TestThread2 has been changed!");
i++;
if(i == 50)
break;
System.out.println("TestThead1 run() called!");
} }
} class TestThread2 extends Thread
{
int j;
public TestThread2()
{
j = 0;
} public void run()
{
TestThread1.running = true;
while(true)
{
j++;
System.out.println("TestThead2 run() called!");
if(j == 10)
break;
}
}
} public class Test
{
public static void main(String[] args)
{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
//t1.setPriority(6);
//t2.setPriority(7);
t1.start();
t2.start();
}
}大家先看看执行结果!t2执行后,就会把TestThread1里面的running设置为true,即
TestThread1中的run()里面被if罩住的代码块就会执行。但结果却和我预期的完全不一样,多次测试,结果就是一样。如下
==============================
TestThread1中的if语句外语句块已经被执行!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!
TestThead2 run() called!==============================为什么,t1执行到TestThread1中的 System.out.println("TestThread1中的if语句外语句块已经被执行!");
后,下面就不执行了呢!=======希望大家踊跃发言。给个说法!
public static void main(String[] args)
{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
//t1.setPriority(6);
//t2.setPriority(7);
t2.start();
try{
Thread.sleep(3000);//令main()主线程sleep3秒,目的是:放弃CPU,让t2去占有CPU。等它醒来时再运行线程t1,就行了。
}catch(InterruptedException e){}
t1.start(); }
======================================
呵呵,谢谢啊,你的方法又让我学到了很多东西,只是我还有一个问题,就是为什么t2线程执行完毕了t1才执行,执行多次结果都是一样
如下========================================
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread2 run() called!
TestThread1中的if语句外语句块已经被执行!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!
TestThread1 run() called!==============================
我想达到的目的就是让t2先执行,然后t2和t1可以交替执行。不知道可不可以办到。
补充一下,上面TestThread1中的run方法里面的那个输出语句System.out.println("Running in TestThread2 has been changed!");
已经被我注销掉了!
答:当然可以啦。而且可以让线程1多做几次后再夹着做一次线程2,因为线程1的工作量大些。
代码如下:(已测试通过)供你参考class TestThread1 extends Thread
{
int i;
public static boolean running = false;
public TestThread1()
{
i = 0;
}
public void run()
{
System.out.println("TestThread1中的if语句外语句块已经被执行!");
if(running)
while(true)
{
//System.out.println("Running in TestThread2 has been changed!");
i++;
if(i == 50)
break;
System.out.println("TestThead1 run() called!");
try{
Thread.sleep(100); //给线程2执行的机会
}catch(InterruptedException e){}
} }
} class TestThread2 extends Thread
{
int j;
public TestThread2()
{
j = 0;
} public void run()
{
TestThread1.running = true;
while(true)
{
j++;
System.out.println("TestThead2 run() called!");
try{
Thread.sleep(400); //给线程1执行的机会
}catch(InterruptedException e){}
if(j == 10)
break;
}
}
} public class Test
{
public static void main(String[] args)
{
TestThread1 t1 = new TestThread1();
TestThread2 t2 = new TestThread2();
//t1.setPriority(6);
//t2.setPriority(7);
t2.start();
try{
Thread.sleep(100);//令main()主线程sleep0.1秒,目的是:放弃CPU,让t2去占有CPU。等它醒来时再运行线程t1,就行了。
}catch(InterruptedException e){}
t1.start();
}
}
你的意思是不是:理论上cpu会随机挑选线程执行,所以当Thread2执行的时候,也有几率去选择Thread1去执行?
可是线程的这种随机性不是你想看到的时候就能出现的,有时候要N长时间运行才可能会偶尔出现那么一次。
所以还是用sleep操作一下吧~不知道说的对不对,请高手指点