在 Java 中同步 2 个线程的更简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7042762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:26:00  来源:igfitidea点击:

Easier way to synchronize 2 threads in Java?

javamultithreadingsynchronization

提问by Andrey Agibalov

I wan't to be sure that some piece of my code within the main thread will be executed after some piece of code executed withing the secondary thread. Here's what I got:

我不想确定主线程中的某些代码将在使用辅助线程执行的某些代码之后执行。这是我得到的:

    final Object lock = new Object();
    final Thread t = new Thread(new Runnable() {
        public void run() {
            synchronized(lock) {
                System.out.println("qwerty");
                lock.notify();
            }
        }
    });

    synchronized(lock) {
        t.start();
        lock.wait();
    }

    System.out.println("absolutely sure, qwerty is above");
  1. Is it correct solution?
  2. Any shorter ways to do the same?
  1. 它是正确的解决方案吗?
  2. 有没有更短的方法来做同样的事情?

采纳答案by parsifal

Assuming that your main thread needs to initiate the secondary thread's processing, an Exchangerwould be the simplest solution.

假设您的主线程需要启动辅助线程的处理,Exchanger将是最简单的解决方案。

If the secondary thread is independent, then some form of BlockingQueuewould be appropriate.

如果辅助线程是独立的,那么某种形式的BlockingQueue将是合适的。



An example using Exchanger (with proper exception handling). The one Exchanger could be substituted with two queues.

使用 Exchanger 的示例(具有适当的异常处理)。一个 Exchanger 可以用两个队列代替。

public static void main(String[] argv)
throws Exception
{
    final Exchanger<String> exchanger = new Exchanger<String>();
    new Thread(new Runnable() 
    {
        @Override
        public void run() 
        {
            try
            {
                String s = exchanger.exchange("");
                System.out.println(s);
                exchanger.exchange("this came from subthread");
            }
            catch (InterruptedException ex)
            {
                System.out.println("interrupted while waiting for message");
            }
        }
    }).start();

    exchanger.exchange("this came from main thread");
    String s = exchanger.exchange("");
    System.out.println(s);
}

回答by SyntaxT3rr0r

Things like notify()and wait()are really low-level synchronization primitives.

诸如notify()wait() 之类的东西实际上是低级同步原语。

You should use higher-level abstractions whenever possible, like in this case, say, a CountDownLatch.

您应该尽可能使用更高级别的抽象,例如在这种情况下,例如CountDownLatch

The following is just an example that should get you started (for example the timeout issues aren't taken into account here):

以下只是一个应该让您入门的示例(例如,此处不考虑超时问题):

    final CountDownLatch latch = new CountDownLatch(1);
    final Thread t = new Thread(new Runnable() {
        public void run() {
            System.out.println("qwerty");
            latch.countDown();
        }
    });

    t.start();
    latch.await();

    System.out.println("absolutely sure, qwerty as been printed");

Low-level things like waitand notifyare really just low-level Java idiosynchrasies that you shouldn't be concerned with (unless you're writing concurrency APIs).

诸如等待通知之类的低级事物实际上只是您不应该关心的低级 Java 特性(除非您正在编写并发 API)。

Additionally, I'd suggest reading the amazing book: Java Concurrency In Practice.

此外,我建议阅读这本很棒的书:Java Concurrency In Practice

回答by DoubleMalt

final Thread t = new Thread(new Runnable() {
    public void run() {
        System.out.println("qwerty");
    }
});

t.start();
t.join();

System.out.println("absolutely sure, qwerty is above");

回答by avh

The best solution for the code you have given is:

您给出的代码的最佳解决方案是:

System.out.println("qwerty");
System.out.println("absolutely sure, qwerty is above");

Don't use threading when you don't need it.

不需要时不要使用线程。

But if you know what you're doing with the thread, then @DoubleMalt's solution is the way to go (t.join()).

但是如果你知道你在用线程做什么,那么@DoubleMalt 的解决方案就是要走的路 ( t.join())。

You'd also probably find it worth reading http://download.oracle.com/javase/tutorial/essential/concurrency/

您可能还会发现它值得一读http://download.oracle.com/javase/tutorial/essential/concurrency/