3

メインスレッドを含む3つのスレッドでカウントダウンアプリを作成しました。countdownEvenをlowに設定して、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.
    }
    }

}
4

1 に答える 1

2

あなたのコードを試してみましたが、出力が生成されます。

Thread-0 Count: 10
Thread-0 Count: 8
Thread-0 Count: 6
Thread-0 Count: 4
Thread-0 Count: 2
Thread-1 Count: 9
Thread-1 Count: 7
Thread-1 Count: 5
Thread-1 Count: 3
Thread-1 Count: 1

本来あるべき出力とまったく同じです...だから、あなたの問題は何ですか?たぶん、Eclipse で新しいコンソール ウィジェット/タブを開く必要があるか、またはアクティブなフィルターがありますか?

しかし、私はこの目的で Threadpriorities を使用しません。http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html を参照してください

于 2012-04-30T15:02:00.323 に答える