Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

Java Threading Join Method

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

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

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...