http://blog.csdn.net/a352193394/article/details/39503857
Java Multi-thread ~ ~ ~ Using exchanger to exchange data between threads [this combination of multi-threaded parallel will solve a lot of problems] specifically see http://www.cnblogs.com/donaldlee2008/p/5290169.html java Thread pool Parallel Execution http://www.cnblogs.com/donaldlee2008/p/5290169.htmlJava Multi-thread ~ ~ ~ Exchange data between threads using exchanger2014-09-23 20:48 1205 people read comments (0) favorite reports Classification:Multithreading (+)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
In multi-threading, exchanging data between two threads is very common, and we can use a common data structure, and Java also provides a good
Class for us to use, that is the exchanger class, this class can help us to synchronize data structure between two threads, and then we will take this class to real
Now a producer consumer model, it seems that the model has been written rotten.
[Java]View PlainCopyprint?
- Package com.bird.concursey.charpet5;
- Import java.util.List;
- Import Java.util.concurrent.Exchanger;
- Public class Producer implements Runnable {
- //this'll is the data structure that the producer would interchange with the consumer.
- private list<string> buffer;
- private exchanger<list<string>> Exchanger;
- Public Producer (list<string> buffer, exchanger<list<string>> Exchanger) {
- super ();
- this.buffer = buffer;
- This.exchanger = exchanger;
- }
- @Override
- public Void Run () {
- int cycle = 1;
- For (int i = 0; i < i++) {
- System.out.printf ("Producer:cycle%d\n", Cycle);
- For (int j=0; j<; j + +) {
- String message="Event" + ((i*) +j);
- System.out.printf ("Producer:%s\n", message);
- Buffer.add (message);
- }
- try {
- Buffer = exchanger.exchange (buffer);
- } catch (Interruptedexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- System.out.println ("Producer:" +buffer.size ());
- cycle++;
- }
- }
- }
[Java]View PlainCopyprint?
- Package com.bird.concursey.charpet5;
- Import java.util.ArrayList;
- Import java.util.List;
- Import Java.util.concurrent.Exchanger;
- Public class Consumer implements Runnable {
- private list<string> buffer;
- private exchanger<list<string>> exchange;
- Public Consumer (list<string> buffer, exchanger<list<string>> Exchange) {
- super ();
- this.buffer = buffer;
- this.exchange = Exchange;
- }
- @Override
- public Void Run () {
- int cycle = 1;
- For (int i = 0; i < i++) {
- System.out.printf ("Consumer:cycle%d\n", Cycle);
- try {
- Buffer = exchange.exchange (buffer);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- System.out.println ("Consumer:" +buffer.size ());
- For (int j=0; j<; j + +) {
- String Message=buffer.get (0);
- System.out.println ("Consumer:" +message);
- Buffer.remove (0);
- }
- cycle++;
- }
- }
- public static void Main (string[] args) {
- list<string> buffer1 = new arraylist<string> ();
- list<string> buffer2 = new arraylist<string> ();
- Exchanger<list<string>> Exchange = new exchanger<list<string>> ();
- Producer Producer = new Producer (Buffer1, Exchange);
- Consumer Consumer = new Consumer (buffer2, Exchange);
- Thread threadproducer=new Thread (producer);
- Thread threadconsumer=new Thread (consumer);
- Threadproducer.start ();
- Threadconsumer.start ();
- }
- }
[HTML]View PlainCopyprint?
- The consumer begins with a empty buffer and calls Exchanger to synchronize with the
- Producer. It needs data to consume. The producer begins its execution with an empty buffer.
- It creates strings, stores it in the buffer, and uses the exchanger-synchronize with
- The consumer.
[HTML]View PlainCopyprint?
- At this point, both threads (producer and consumer) is in Exchanger and it changes the
- Data structures, if the consumer returns from the Exchange () method, it would have a
- Buffer with ten strings. When the producer returns from the Exchange () method, it would have
- An empty buffer to fill again. This operation is to be repeated ten times.
[HTML]View PlainCopy print?
- If You execute the example, you'll see how producer and consumer do their jobs
- Concurrently and how the other objects interchange their buffers in every step. As it occurs with
- Other synchronization utilities, the first thread that calls the Exchange () method is put to
- Sleep until the other threads arrived.
Java Multi-thread ~ ~ ~ Using exchanger to exchange data between threads [this combination of multi-threaded parallelism will solve a lot of problems]