生产者与消费者
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Created by Panda on 2018/6/6.
*/
class Meal{
private final int orderNum;
public Meal (int orderNum) {this .orderNum = orderNum;}
public String toString (){return "Meal " +orderNum;}
}
class WaitPerson implements Runnable{
private Restaurant restaurant;
public WaitPerson (Restaurant restaurant) {this .restaurant = restaurant;}
@Override
public void run () {
try {
while (!Thread.interrupted()){
synchronized (this ){
while (restaurant.meal==null ) wait();
}
System.out.println("Waitperson got; " +restaurant.meal);
synchronized (restaurant.chef){
restaurant.meal=null ;
restaurant.chef.notifyAll();
}
}
}catch (InterruptedException e){
System.out.println("Waitperson interrupted" );
}
}
}
class Chef implements Runnable{
private Restaurant restaurant;
private int cout=0 ;
public Chef (Restaurant restaurant) {this .restaurant = restaurant;}
@Override
public void run () {
try {
while (!Thread.interrupted()){
synchronized (this ){
while (restaurant.meal!=null ){
wait();
}
}
if (++cout==10 ){
System.out.println("Out of food,closing" );
restaurant.executorService.shutdownNow();
}
System.out.println("Order up!" );
synchronized (restaurant.waitPerson){
restaurant.meal=new Meal(cout);
restaurant.waitPerson.notifyAll();
}
TimeUnit.MILLISECONDS.sleep(100 );
}
}catch (InterruptedException e){
System.out.println("chef interrupted" );
}
}
}
public class Restaurant {
Meal meal;
ExecutorService executorService = Executors.newCachedThreadPool();
WaitPerson waitPerson = new WaitPerson(this );
Chef chef =new Chef(this );
public Restaurant () {
executorService.execute(chef);
executorService.execute(waitPerson);
}
public static void main (String[] args) {
new Restaurant();
}
}
生产者-消费者与队列
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.SynchronousQueue;
/**
* Created by Panda on 2018/6/6.
*/
class LiftOffRunner implements Runnable{
private BlockingQueue<LiftOff> rockets;
public LiftOffRunner (BlockingQueue<LiftOff> rockets) {
this .rockets = rockets;
}
public void add (LiftOff liftOff){
try {
rockets.put(liftOff);
}catch (InterruptedException e){
System.out.println("Interrupted during put()" );
}
}
@Override
public void run () {
try {
while (!Thread.interrupted()){
LiftOff liftOff = rockets.take();
liftOff.run();
}
}catch (InterruptedException e){
System.out.println("Waking from take()" );
}
System.out.println("Exiting LiftOffRunner" );
}
}
public class TestBlockingQueues {
static void getKey(){
try {
new BufferedReader(new InputStreamReader(System.in)).readLine();
}catch (IOException e){
throw new RuntimeException(e);
}
}
static void getKey(String message){
System.out.println(message);
getKey();
}
static void test(String msg,BlockingQueue<LiftOff> queue){
System.out.println(msg);
LiftOffRunner liftOffRunner =new LiftOffRunner(queue);
Thread thread = new Thread(liftOffRunner);
thread.start();
for (int i = 0 ; i < 5 ; i++) {
liftOffRunner.add(new LiftOff(5 ));
}
getKey("Press 'Enter' (" +msg+")" );
thread.interrupt();
System.out.println("Finished " +msg+" test" );
}
public static void main (String[] args) {
test("LinkedBlockingQueue" ,new LinkedBlockingDeque<LiftOff>());
test("ArrayBlockingQueue" ,new ArrayBlockingQueue<LiftOff>(3 ));
test("SynchronousQueue" ,new SynchronousQueue<LiftOff>());
}
}