我刚刚制作了一个包含主线程在内的三个线程的倒计时应用程序。我将 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.
}
}
}