Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java线程通信_Java_Multithreading_Jakarta Ee - Fatal编程技术网

Java线程通信

Java线程通信,java,multithreading,jakarta-ee,Java,Multithreading,Jakarta Ee,在java线程中,我可以使用wait和notify在两个线程之间轻松通信 但是假设我有10个线程在运行,比如T1到T10,我希望线程T2与线程T7通信 我怎么能做到?可以举一些例子。使用wait/notify可以在线程之间进行通信,但要正确地进行通信非常复杂,特别是当涉及两个以上的线程时 更现代的s解决方案更适合Java中的线程间通信 要使用它们,请在创建线程之前,创建一个要在两个线程之间共享的队列。然后在创建队列时将其传递给每个线程。然后它们都保持队列,一个写入,另一个读取 public cl

在java线程中,我可以使用wait和notify在两个线程之间轻松通信

但是假设我有10个线程在运行,比如T1到T10,我希望线程T2与线程T7通信


我怎么能做到?可以举一些例子。

使用wait/notify可以在线程之间进行通信,但要正确地进行通信非常复杂,特别是当涉及两个以上的线程时

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

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

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

}

使用wait/notify可以在线程之间进行通信,但要正确进行通信非常复杂,特别是当涉及两个以上的线程时

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

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

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

}
下面是一个例子

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);
}
下面是一个例子

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

你到底想达到什么目的?我建议不要在JavaEE特别是EJB容器中处理线程——如果您不熟悉它们的工作方式,就更应该如此。可能还有更多类似JavaEE的解决方案,所以请详细说明。简短的回答是,您不能。使用notifyAll,您事先不知道下一个线程将执行哪个线程。您到底想实现什么?我建议不要在JavaEE特别是EJB容器中处理线程——如果您不熟悉它们的工作方式,就更应该如此。可能还有更多类似JavaEE的解决方案,所以请详细说明。简短的回答是,您不能。通过使用notifyAll,您事先不知道下一个将执行哪个线程