Question

I just made a countdown app with three threads including the main thread. I have CountdownEven set to low so that countdownOdd will display first but in the output nothing is happening. Can anyone see the problem?

//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.
    }
    }

}
Was it helpful?

Solution

I tried your code and it does produce an output.

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

Exactly the output as it should be... so whats your prob? Maybe you just have to open a new console widget/tab in eclipse or you have any active filter?

But imho I wont use Threadpriorities for this purpose, see http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Thread.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top