sasklacz
sasklacz

Reputation: 3638

Threads in Java

I've created simple program to test Threads in Java. I'd like it to print me numbers infinitely, like 123123123123123. Dunno why, but currently it stops after one cycle finishing 213 only. Anyone knows why ?

public class Main {
    int number;

    public Main(int number){
    }

    public static void main(String[] args) {
        new Infinite(2).start();
        new Infinite(1).start();
        new Infinite(3).start();
    }
}

class Infinite extends Thread {
    static int which=1;
    static int order=1;
    int id;
    int number;
    Object console = new Object();

    public Infinite(int number){
        id = which;
        which++;
        this.number = number;
    }

    @Override
    public void run(){
        while(1==1){
            synchronized(console){
                if(order == id){
                    System.out.print(number);
                    order++;
                    if(order >= which){
                        order = 1;
                    }
                    try{
                        console.notifyAll();
                        console.wait();
                    }
                    catch(Exception e)
                    {}                    
                }
                else {
                    try{
                        console.notifyAll();
                        console.wait();
                    }
                    catch(Exception e)
                    {}                    
                }
            }
            try{Thread.sleep(0);} catch(Exception e) {}
        }
    }
}

Upvotes: 2

Views: 445

Answers (4)

Dragisa Krsmanovic
Dragisa Krsmanovic

Reputation: 596

Keep in mind that System.out.print(..) will not automatically flush the output.

Also, take a look at java.util.concurrent.Semaphore. You might be able to do what you are trying (execute threads in predefined order) without synchronizing and calling waits.

Upvotes: 1

VHans
VHans

Reputation: 71

Each Thread is synchronizing on a different object. When control goes to console.wait(), the executing thread will wait for another thread to call notify()/notifyAll() on the control object. There is no other thread that is doing this because each thread has its own copy of the control object. Hence, all the threads are waiting indefinitely after printing just once. To solve this problem, keep a common object on which all the threads can wait.

Upvotes: 2

Inv3r53
Inv3r53

Reputation: 2969

The threads re blocked probably waiting to acquire the monitor to call wait. you are not using the wait notify mechanism properly.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#wait%28%29

Upvotes: 3

Chris
Chris

Reputation: 10445

Each of your Infinite instances is synchronising on, then waiting for a notification on, its own console Object. Once the thread gets to console.wait(), it's not going to continue.

You seem to want them all to synchronise on the same object - so you'll need to, for example, make console static.

Upvotes: 9

Related Questions