package ConcurrentSource;
import java.util.LinkedList;
import java.util.Queue;
public class CustomerAndProducer01 {
private Queue<Integer> queue;
private int maxSize;
CustomerAndProducer01(int maxSize){
this.queue = new LinkedList<Integer>();
this.maxSize = maxSize;
}
class Customer implements Runnable{
public void use(){
synchronized (queue){
while(queue.size()==0){
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.poll();
System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
queue.notifyAll();
}
}
@Override
public void run() {
for(int i=0;i<5;i++){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
use();
}
}
}
class Producer implements Runnable{
public void produce(){
synchronized (queue){
while(queue.size()==maxSize){
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.offer(1);
System.out.println(Thread.currentThread().getName() +"生产后还有"+queue.size());
queue.notifyAll();
}
}
@Override
public void run() {
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
produce();
}
}
}
public static void main(String[] args) {
CustomerAndProducer01 cp = new CustomerAndProducer01(10);
for(int i=0;i<10;i++){
new Thread(cp.new Customer()).start();
}
for(int i=0;i<5;i++){
new Thread(cp.new Producer()).start();
}
}
}
package ConcurrentSource;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class CustomerAndProducer02 {
private Queue<Integer> queue;
private int maxSize;
private Lock lock;
private Condition isFull;
private Condition isEmpty;
CustomerAndProducer02(int maxSize){
this.queue = new LinkedList<Integer>();
this.maxSize = maxSize;
this.lock = new ReentrantLock();
isEmpty = lock.newCondition();
isFull = lock.newCondition();
}
class Customer implements Runnable{
public void use(){
lock.lock();
try{
while(queue.size()==0){
try {
isEmpty.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.poll();
System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
isFull.signalAll();
}finally {
lock.unlock();
}
}
@Override
public void run() {
for(int i=0;i<5;i++){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
use();
}
}
}
class Producer implements Runnable{
public void produce(){
lock.lock();
try{
while(queue.size()==maxSize){
try {
isFull.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
queue.offer(1);
System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
isEmpty.signalAll();
}finally {
lock.unlock();
}
}
@Override
public void run() {
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
produce();
}
}
}
public static void main(String[] args) {
CustomerAndProducer02 cp = new CustomerAndProducer02(10);
for(int i=0;i<10;i++){
new Thread(cp.new Customer()).start();
}
for(int i=0;i<5;i++){
new Thread(cp.new Producer()).start();
}
}
}
package ConcurrentSource;
import java.util.concurrent.LinkedBlockingQueue;
public class CustomerAndProducer03 {
private LinkedBlockingQueue queue;
private int maxSize;
CustomerAndProducer03(int maxSize){
this.queue = new LinkedBlockingQueue(maxSize);
this.maxSize = maxSize;
}
class Customer implements Runnable{
@Override
public void run() {
for(int i=0;i<5;i++){
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
queue.take();
System.out.println(Thread.currentThread().getName() + "消费后还有"+queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Producer implements Runnable{
@Override
public void run() {
for(int i=0;i<5;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
queue.put(1);
System.out.println(Thread.currentThread().getName() + "生产后还有"+queue.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
CustomerAndProducer03 cp = new CustomerAndProducer03(10);
for(int i=0;i<10;i++){
new Thread(cp.new Customer()).start();
}
for(int i=0;i<5;i++){
new Thread(cp.new Producer()).start();
}
}
}