package com.qing.java8;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author liuchangqing
* @time 2016年4月9日下午2:18:02
* @function 4個生產者線程,1個消費者線程
* A生產者 1
* B 2
* C 3
* D 4
* 且只能順序生產,12341234,消費者順序消費
* 數據存放到數組products[13]中
* 每個生產200次
*/
public class CTest {
private ReentrantLock lock = new ReentrantLock(true);
private Condition condition = lock.newCondition();
private int products[] = new int[13];
private int serverCount = 4; // 生產者個數
private int eachProSum = 200; // 每個生產者生產總數
public static void main(String[] args) throws Exception {
CTest main = new CTest();
main.run();
}
void run() throws Exception {
Executor executor = Executors.newFixedThreadPool(5);
executor.execute(new Client());
executor.execute(new Server(1));
executor.execute(new Server(2));
executor.execute(new Server(3));
executor.execute(new Server(4));
}
private int getPos = 0; // 用於記錄目前讀取的位置
class Server implements Runnable {
private int count = 0;
private int putPos;
private int n;
public Server(int n) {
this.n = n;
this.putPos = n-1;
}
@Override
public void run() {
while(true) {
try {
lock.lock();
while(products[putPos % products.length] != 0 || putPos - getPos >= products.length) {
condition.await();
}
products[putPos % products.length] = n;
putPos +=serverCount;
count ++;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
lock.unlock();
if(count >= eachProSum) {
System.out.println("server "+n+" is done");
return;
}
}
}
}
}
class Client implements Runnable {
@Override
public void run() {
while(true) {
if(products[getPos % products.length] == 0)
continue;
try {
lock.lock();
System.out.print(products[getPos % products.length]+", ");
products[getPos % products.length] =0;
getPos ++;
condition.signalAll();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
lock.unlock();
if(getPos >= eachProSum*serverCount) {
System.out.println("client is done");
return;
}
}
}
}
}
}
借鑑disruptor的思想,序號遞增。