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

发布于 2024-11-29 02:06:51 字数 524 浏览 0 评论 0原文

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

    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. 这是正确的解决方案吗?
  2. 还有更短的方法可以做到同样的事情吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叹沉浮 2024-12-06 02:06:51

notify()wait() 这样的东西实际上是低级同步原语。

您应该尽可能使用更高级别的抽象,例如在本例中,CountDownLatch

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

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

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

此外,我建议阅读一本精彩的书:Java Concurrency In Practice

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

You should use higher-level abstractions whenever possible, like in this case, say, a 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 wait and notify are really just low-level Java idiosynchrasies that you shouldn't be concerned with (unless you're writing concurrency APIs).

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

缱倦旧时光 2024-12-06 02:06:51

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

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


使用 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);
}

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

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


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

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);
}
亽野灬性zι浪 2024-12-06 02:06:51
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");
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");
一指流沙 2024-12-06 02:06:51

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

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

不需要时不要使用线程。

但是,如果您知道自己在使用线程做什么,那么@DoubleMalt 的解决方案就是正确的选择(t.join())。

您可能还会发现值得一读 http://download.oracle.com/javase /教程/基本/并发/

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文