簡體   English   中英

在這種情況下,如何實現線程等待通知?

[英]How do I implement thread wait notify in this case?

我有2節課。 該類的一個方法調用另一個類的方法,但是它必須等到該方法完成后才能繼續執行其余代碼。

這是我要制作的大致代碼。 而且我知道這行不通。

public class Example 
{
    Thread thread;

    public Example(Thread thread)
    {
        this.thread = thread;
    }

    public void doSomethingElse()
    {
        System.out.println("Do something else");
        thread.notify();
    }
}

public class Example2 
{
    Thread thread;
    Example example;

    public Example2()
    {
        example = new Example(thread);
        thread = new Thread()
        {
            public void run()
            {
                example.doSomethingElse();
                try {
                    this.wait();
                } catch (InterruptedException ex) {                    
                }
                System.out.println("Do something");
            }
        };
    }

    public void doSomething()
    {
        thread.run();
    }
}

現在您知道該怎么做嗎?

要點:

  • 您應該在調用wait或notify方法之前獲取鎖。 鎖必須在同一對象上。 在代碼中,您正在對example2對象調用wait,但在其他對象上調用notify。
  • thread.run()表示調用線程對象的run方法,它不創建新線程,其方式與example.doSomething()相同。 創建線程時,通過調用start方法啟動該線程。

這是我的實現

    class Example implements Runnable 
    {
        public void run()
        {
            doSomething();
        }
        public void doSomething(){
            synchronized(this){
                System.out.println("Do something else");
                try{
                   Thread.sleep(1000); 
                   this.notify();    
                }catch (InterruptedException ignore) {}            
            }    
        }
    }

    class Example2 implements Runnable 
    {
        Thread thread;
        Example example;

        public Example2(Example example){
            this.example = example;
        }


        public void run(){
            doSomething();    
        }

        public void doSomething(){
            synchronized(example){
                System.out.println("waiting for example 1 to complete");
                try{
                    example.wait();    
                }catch (InterruptedException ignore) {}

            }
            System.out.println("Do something");
        }
    }

    public class Entry{
        public static void main(String[] args){

            Example example = new Example();

            Example2 obj = new Example2(example);
            Thread t = new Thread(obj);
            t.start();

            Thread t2 = new Thread(example);
            t2.start();
        }
    }

在代碼Thread.sleep(1000); 不需要聲明。

這是使用join方法的另一種實現

    class Example implements Runnable 
    {
        public void run()
        {
            doSomething();
        }
        public void doSomething(){
            System.out.println("Do something else");

            try{
                Thread.sleep(1000);    
            }catch (InterruptedException ignore) {}                
        }
    }

    class Example2 implements Runnable 
    {
        Thread thread;
        Example example;

        public Example2(Example example){
            this.example = example;
        }


        public void run(){
            System.out.println("waiting for example 1 to complete");
            Thread t = new Thread(example);
            try{
                t.start();
                t.join();
            }catch(InterruptedException ie){

            }

            doSomething();    
        }

        public void doSomething(){
            System.out.println("Do something");
        }
    }

    public class Entry{
        public static void main(String[] args){

            Example example = new Example();

            Example2 obj = new Example2(example);
            Thread t = new Thread(obj);
            t.start();
        }
    }

不知道您是否受限於使用此特定方法(等待/通知),但是更好的方法是利用Java Concurrency API

public class ExampleCountDownLatch
{
    public void doSomething () throws InterruptedException
    {
        final CountDownLatch latch = new CountDownLatch(1);

        Thread thread = new Thread()
        {
            public void run ()
            {
                System.out.println("do something");
                latch.countDown();
            }
        };

        System.out.println("waiting for execution of method in your example class");
        thread.start();
        // wait for reasonable time otherwise kill off the process cause it took
        // too long.
        latch.await(3000, TimeUnit.MILLISECONDS);

        // now I can do something from your example 2
        System.out.println("now i can execute from example 2 do something else");
    }
}

無論如何,如果您有選擇的話,那只是另一種方法。

更新:

這是有關此主題的博客。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM