views:

181

answers:

5
+2  Q: 

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) {}
        }
    }
}
A: 

you might wanna try catching those exceptions so you can see what went wrong:

instead of:

catch(Exception e){}

try:

catch(Exception e){
    e.printStackTrace();
}

BTW: Thread.sleep(0) seems wrong to me but im too lazy to read the javadoc :)

smeg4brains
If I recall correctly `Thread.sleep(0);` is equivalent to a yield
Michael Mrozek
+9  A: 

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.

Chris Smith
That is what I was thinking. In fact, when I copied that same code and removed the .wait() and the .notifyAll() I got a bunch of numbers printed on my console when I stopped the program from running.
npinti
+3  A: 

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

daedlus
+1  A: 

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.

dragisak
+2  A: 

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.