0

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

现在你知道如何做到这一点了吗?

4

3 回答 3

0

不确定您是否受限于使用这种特定方法(等待/通知),但是更好的方法是利用Java 并发 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");
    }
}

无论如何,如果您有选择的话,这只是另一种方法。

更新:

这是一个关于这个主题的博客。

于 2012-12-14T06:52:40.900 回答
0

几点:

  • 您应该在调用等待或通知方法之前获取锁。锁必须在同一个对象上。在代码中,您在 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); 不需要声明。

于 2012-12-14T06:33:54.397 回答
0

这是使用连接方法的另一种实现

    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();
        }
    }
于 2012-12-14T06:45:51.253 回答