Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my program Thread T1 spawns a new Thread T2 and calls join on that thread (i.e. T2.join ) and this newly spawned thread T2 calls join on T1 (i.e. T1.join). This is causing thread blocking. How this can be overcome. My Program

public class PositiveNegativeNumberProducerV1 {
    static Thread evenThread, oddThread;

    public static void main(String[] args) {

        oddThread = new Thread(new OddProducer(evenThread), "oddThread");
        oddThread.start();


    }

}
class EvenProducer implements Runnable {
    Thread t;
    EvenProducer(Thread t) {
        this.t= t;
    }
    public void run() {
        for(int i=1; i<=100; i++) {
            if(i%2==0) {
                System.out.println("i = "+i+":"+Thread.currentThread().getName());
                try {
                    System.out.println("Now join will be called on "+t.getName()+" by thread "+Thread.currentThread().getName());
                    t.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

class OddProducer implements Runnable {
    Thread t;
    OddProducer(Thread t) {
        this.t= t;
    }
    public void run() {
        for(int i=1; i<=100; i++) {
            if(i%2!=0) {
                System.out.println("i = "+i+":"+Thread.currentThread().getName());
                try {
                    if(t==null) {
                        t = new Thread(new EvenProducer(PositiveNegativeNumberProducerV1.oddThread), "evenThread");
                        t.start();
                    }
                    if(t.isAlive()) {
                        System.out.println("evenThread is alive and join will be called on "+t.getName()+" by thread "+Thread.currentThread().getName());
                        t.join();
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you just want to synchronize the output: 1 2 3 4 ... then you should not use join (which waits for thread termination, i.e. leaving the run method). Consider to use the wait() and notify() pair on a semaphore object.

        Object sema = new Object();

        new Thread( new Runnable() {
            @Override
            public void run()
            {
                for ( int i = 1; i <= 100; i++ )
                {
                    if ( i % 2 == 0 )
                    {
                        try
                        {
                            System.out.println( "Going to wait for the odd thread - "
                                + Thread.currentThread().getName());
                            synchronized (sema)
                            {
                                sema.wait();
                            }

                            System.out.println( "i = " + i + ":" + Thread.currentThread().getName());

                            System.out.println( "Going to notify the odd thread - "
                                + Thread.currentThread().getName());
                            synchronized (sema)
                            {
                                sema.notify();
                            }
                        }
                        catch ( InterruptedException e )
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "Even").start();

        new Thread( new Runnable() {
            @Override
            public void run()
            {
                for ( int i = 1; i <= 100; i++ )
                {
                    if ( i % 2 != 0 )
                    {
                        System.out.println( "i = " + i + ":" + Thread.currentThread().getName());
                        try
                        {
                            System.out.println( "Going to notify the even thread"
                                + Thread.currentThread().getName());
                            synchronized (sema)
                            {
                                sema.notify();
                            }
                            System.out.println( "Going to wait for the even thread"
                                + Thread.currentThread().getName());
                            synchronized (sema)
                            {
                                sema.wait();
                            }
                        }
                        catch ( InterruptedException e )
                        {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }, "Odd").start();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...