1

Recently I have delved into the dark arts of Threads, I get how to create them and when to use them and when not to use them. But when I tried to learn how to communicate between them; I Found that Pipes are what you use to do it. I have a Object that is a Instance of one of my Class' that I created, but Pipes seem to be only able to send Byte Arrays or Integers. I wont to be able to use something like a Object Stream to send my object to the other Thread, but my surfing of the internet has gone terribly bad and I am lost. so I Guess the only thing to do is turn to Stack Overflow and see if any one can help. Thank you for the help in advance.

  • Check out [similar](http://stackoverflow.com/questions/2170520/inter-thread-communication-in-java) [questions](http://stackoverflow.com/questions/9242204/inter-threads-communication) for alternative options – Mr Moose Oct 07 '15 at 07:19
  • There are similar questions but I couldn't find one that had my answer or asked the question that i asked. – NicholasLupo Oct 07 '15 at 07:25
  • Where do you get pipes from? That seems way out of left field. Are you referring to different processes? – matt Oct 07 '15 at 07:34
  • pipoutputstreams and pipeinputstreams – NicholasLupo Oct 07 '15 at 07:48
  • there used to communicate with threads using bytes[]. you must be thinking of unix pipes? – NicholasLupo Oct 07 '15 at 07:49
  • Part of the definition of threads, is that they all run in the same address space. Literally _any_ object can be shared between two (or more) threads. The most versatile shared objects for inter-thread communication are the _queues_. `java.util.concurrent.ArrayBlockingQueue` or `java.util.concurrent.LinkedBlockingQueue` would be a good place to start. Also, consider working through the Java concurrency tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/ – Solomon Slow Oct 07 '15 at 13:05

1 Answers1

6

You should use one of the implementations of BlockingQueue.

I most commonly use ArrayBlockingQueue as it allows me to limit the memory footprint of the solution. A LinkedBlockingDeque can be used for an unlimited size but be certain you cannot overload memory.

Here are two threads communicating between themselves using an ArrayBlockingQueue.

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

}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213