Java线程连接方法

时间:2017-01-14 14:41:49

标签: java

在我的程序中,线程T1产生一个新的线程T2,并在该线程上调用join(即T2.join),这个新生成的线程T2在T1上调用join(即T1.join)。这导致线程阻塞。如何克服这一点。 我的计划

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

1 个答案:

答案 0 :(得分:0)

如果您只想同步输出:1 2 3 4 ...那么您不应该使用join(等待线程终止,即保留run方法)。考虑在信号量对象上使用wait()和notify()对。

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