我刚刚用三个线程(包括主线程)制作了一个倒计时应用程序。我将CountdownEven设置为较低的优先级,以便让countdownOdd先显示,但是在输出中没有发生任何事情。有人看到问题了吗?
//Main
public class CountdownApp
{
public static void main(String[] args)
{
new CountdownApp().start();
}
public void start()
{
Thread count1 = new CountdownEven();
Thread count2 = new CountdownOdd();
count1.setPriority(Thread.MIN_PRIORITY);
count2.setPriority(Thread.MAX_PRIORITY);
count1.start();
count2.start();
}
}
public class CountdownEven extends Thread
{
public void run()
{
for(int i = 10; i > 0; i-=2)
{
System.out.println(this.getName()+ " Count: " +i);
Thread.yield();//This is to allow the other thread to run.
}
}
}
public class CountdownOdd extends Thread
{
public void run()
{
for(int i = 9; i > 0; i-=2)
{
System.out.println(this.getName()+ " Count: " +i);
Thread.yield();//This is to allow the other thread to run.
}
}
}
线程1 计数:9
/线程1 计数:7
/线程1 计数:5
等等。 - assylias