您的位置:首页 > 其它

生产消费者1

2016-07-12 16:14 555 查看
package com.packtpub.java7.concurrency.chapter2.recipe2.core;

import com.packtpub.java7.concurrency.chapter2.recipe2.task.Consumer;
import com.packtpub.java7.concurrency.chapter2.recipe2.task.EventStorage;
import com.packtpub.java7.concurrency.chapter2.recipe2.task.Producer;

/**
* Main class of the example
*/
public class Main {

/**
* Main method of the example
*/
public static void main(String[] args) {

// Creates an event storage
EventStorage storage=new EventStorage();

// Creates a Producer and a Thread to run it
Producer producer=new Producer(storage);
Thread thread1=new Thread(producer);

// Creates a Consumer and a Thread to run it
Consumer consumer=new Consumer(storage);
Thread thread2=new Thread(consumer);

// Starts the thread
thread2.start();
thread1.start();
}

}


package com.packtpub.java7.concurrency.chapter2.recipe2.task;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
* This class implements an Event storage. Producers will storage
* events in it and Consumers will process them. An event will
* be a java.util.Date object
*
*/
public class EventStorage {

/**
* Maximum size of the storage
*/
private int maxSize;
/**
* Storage of events
*/
private List<Date> storage;

/**
* Constructor of the class. Initializes the attributes.
*/
public EventStorage(){
maxSize=10;
storage=new LinkedList<>();
}

/**
* This method creates and storage an event.
*/
public synchronized void set(){
while (storage.size()==maxSize){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storage.add(new Date());
System.out.printf("Set: %d\n",storage.size());
notify();
}

/**
* This method delete the first event of the storage.
*/
public synchronized void get(){
while (storage.size()==0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("Get: %d: %s",storage.size(),((LinkedList<?>)storage).poll());
notify();
}

}


package com.packtpub.java7.concurrency.chapter2.recipe2.task;

/**
* This class implements a producer of events.
*
*/
public class Producer implements Runnable {

/**
* Store to work with
*/
private EventStorage storage;

/**
* Constructor of the class. Initialize the storage.
* @param storage The store to work with
*/
public Producer(EventStorage storage){
this.storage=storage;
}

/**
* Core method of the producer. Generates 100 events.
*/
@Override
public void run() {
for (int i=0; i<100; i++){
storage.set();
}
}
}


package com.packtpub.java7.concurrency.chapter2.recipe2.task;

/**
* This class implements a consumer of events.
*
*/
public class Consumer implements Runnable {

/**
* Store to work with
*/
private EventStorage storage;

/**
* Constructor of the class. Initialize the storage
* @param storage The store to work with
*/
public Consumer(EventStorage storage){
this.storage=storage;
}

/**
* Core method for the consumer. Consume 100 events
*/
@Override
public void run() {
for (int i=0; i<100; i++){
storage.get();
}
}

}


结果:

Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:51 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1

Set: 2

Set: 3

Set: 4

Set: 5

Set: 6

Set: 7

Set: 8

Set: 9

Set: 10

Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016

以上代码来源于Java7并发实践手册
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  生产消费者