首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java线程通信

Java线程通信
EN

Stack Overflow用户
提问于 2015-12-10 19:05:04
回答 2查看 939关注 0票数 1

在java Thread中,我可以使用wait()notify()轻松地在两个线程之间进行通信。

但是假设我有10个线程在运行,比如T1T10,并且我想让线程T2与线程T7通信。

我怎么能做到这一点?请举出一些例子以示感谢。

EN

回答 2

Stack Overflow用户

发布于 2015-12-10 19:11:48

线程之间的通信可以通过等待/通知进行,但非常复杂(要正确处理),特别是当涉及两个以上的线程时。

更现代的Java解决方案更适合于BlockingQueue中的线程间通信。

要使用它们,请在创建线程之前创建一个在两个线程之间共享的队列。然后,在创建队列时将队列传递给每个线程。然后它们都持有队列,一个写入队列,另一个读取队列。

代码语言:javascript
运行
复制
public class TwoThreads {

    public static void main(String args[]) throws InterruptedException {
        System.out.println("TwoThreads:Test");
        new TwoThreads().test();
    }

    // The end of the list.
    private static final Integer End = -1;

    static class Producer implements Runnable {

        final BlockingQueue<Integer> queue;

        public Producer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            try {
                for (int i = 0; i < 1000; i++) {
                    queue.add(i);
                    Thread.sleep(1);
                }
                // Finish the queue.
                queue.add(End);
            } catch (InterruptedException ex) {
                // Just exit.
            }
        }

    }

    static class Consumer implements Runnable {

        final BlockingQueue<Integer> queue;

        public Consumer(BlockingQueue<Integer> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            boolean ended = false;
            while (!ended) {
                try {
                    Integer i = queue.take();
                    ended = i == End;
                    System.out.println(i);
                } catch (InterruptedException ex) {
                    ended = true;
                }
            }
        }

    }

    public void test() throws InterruptedException {
        BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();
        Thread pt = new Thread(new Producer(queue));
        Thread ct = new Thread(new Consumer(queue));
        // Start it all going.
        pt.start();
        ct.start();
        // Wait for it to finish.
        pt.join();
        ct.join();
    }

}
票数 5
EN

Stack Overflow用户

发布于 2015-12-10 19:14:28

下面是一个例子

代码语言:javascript
运行
复制
public static void main(String[] args) throws Exception {
    final Object[] monitors = new Object[10];
    final Thread[] threads = new Thread[10];
    for (int i = 0; i < 10; i++) {
        final int num = i;
        Thread t = new Thread() {
            @Override
            public void run() {
                final Object mon = monitors[num];
                try {
                    synchronized (mon) {
                        mon.wait();
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                }
                System.out.println("Hello, world from thread " + num);
            }
        };
        Object o = new Object();
        threads[i] = t;
        monitors[i] = o;
        t.start();
    }

    final Object mon = monitors[5];
    synchronized (mon) {
        mon.notify();
    }
    Thread.sleep(10000);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34200294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档