单一生产,单一消费:
信号量方式:
package test;
public class Main {
boolean flag = true;
int food = 0;
class T1 extends Thread{
@Override
public void run() {
while(true){
synchronized (this) {
while(!flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (t2) {
food++;
System.out.println("make "+food);
flag=!flag;
t2.notify();
}
}
}
}
class T2 extends Thread{
@Override
public void run() {
while(true){
synchronized (this) {
while(flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (t1) {
food--;
System.out.println("eat "+food);
flag=!flag;
t1.notify();
}
}
}
}
T1 t1 = new T1();
T2 t2 = new T2();
public static void main(String[] args) {
Main m = new Main();
m.t1.start();
m.t2.start();
}
}
信号集
package test;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Test{
ReentrantLock lock1 = new ReentrantLock();//满锁
ReentrantLock lock2 = new ReentrantLock();//空锁
Condition c1 = lock1.newCondition();
Condition c2 = lock2.newCondition();
int food = 0;
class T1 extends Thread{//生产者
@Override
public void run() {
while(true){
lock1.lock();
while(food==1){
try {
c1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock1.unlock();
lock2.lock();
System.out.println("生产"+(++food));
c2.signalAll();
lock2.unlock();
}
}
}
class T2 extends Thread{//消费者
@Override
public void run() {
while(true){
lock2.lock();
while(food==0){
try {
c2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock2.unlock();
lock1.lock();
System.out.println("消费"+(--food));
c1.signalAll();
lock1.unlock();
}
}
}
T1 t1 = new T1();
T2 t2 = new T2();
public static void main(String[] args) {
Test m = new Test();
m.t1.start();
m.t2.start();
}
}
生产者消费者队列:
信号量方式:
package test;
import java.util.Arrays;
public class Main {
class Food{//食物类
int num = 0;
Food(int num){
this.num = num;
}
@Override
public String toString() {
return "Food"+this.num;
}
}
int max = 10;
int count = 0;//当前队列里面的Food数目
Food[] foods = new Food[max];
class T1 extends Thread{//生产者
@Override
public void run() {
while(true){
synchronized (this) {
while(count==max){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (t2) {
foods[count] = new Food(count);
count++;
System.out.println("生产"+Arrays.toString(foods));
t2.notify();
}
}
}
}
class T2 extends Thread{//消费者
@Override
public void run() {
while(true){
synchronized (this) {
while(count==0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
synchronized (t1) {
foods[--count] = null;
System.out.println("消费"+Arrays.toString(foods));
t1.notify();
}
}
}
}
T1 t1 = new T1();
T2 t2 = new T2();
public static void main(String[] args) {
Main m = new Main();
m.t1.start();
m.t2.start();
}
}
信号集package test;
import java.util.Arrays;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
class Food{//食物类
int num = 0;
Food(int num){
this.num = num;
}
@Override
public String toString() {
return "Food"+this.num;
}
}
ReentrantLock lock1 = new ReentrantLock();//满锁
ReentrantLock lock2 = new ReentrantLock();//空锁
Condition c1 = lock1.newCondition();
Condition c2 = lock2.newCondition();
int max = 10;
int count = 0;//当前队列里面的Food数目
Food[] foods = new Food[max];
class T1 extends Thread{//生产者
@Override
public void run() {
while(true){
lock1.lock();
while(count==max){
try {
c1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock1.unlock();
lock2.lock();
foods[count] = new Food(count);
count++;
System.out.println("生产"+Arrays.toString(foods));
c2.signalAll();
lock2.unlock();
}
}
}
class T2 extends Thread{//消费者
@Override
public void run() {
while(true){
lock2.lock();
while(count==0){
try {
c2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock2.unlock();
lock1.lock();
foods[--count] = null;
System.out.println("消费"+Arrays.toString(foods));
c1.signalAll();
lock1.unlock();
}
}
}
T1 t1 = new T1();
T2 t2 = new T2();
public static void main(String[] args) {
Main m = new Main();
m.t1.start();
m.t2.start();
}
}
管程方式
package test;
import java.util.Arrays;
public class Main {
class Food{//食物类
int num = 0;
Food(int num){
this.num = num;
}
@Override
public String toString() {
return "Food"+this.num;
}
}
int max = 10;
int count = 0;//当前队列里面的Food数目
static Main m = new Main();
Food[] foods = new Food[max];
synchronized void produce(){
while(count==max){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
foods[count] = new Food(count);
count++;
System.out.println("生产"+Arrays.toString(foods));
notifyAll();
}
synchronized void consume(){
while(count==0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
foods[--count] = null;
System.out.println("消费"+Arrays.toString(foods));
notifyAll();
}
class T1 extends Thread{//生产者
@Override
public void run() {
while(true){
m.produce();
}
}
}
class T2 extends Thread{//消费者
@Override
public void run() {
while(true){
m.consume();
}
}
}
T1 t1 = new T1();
T2 t2 = new T2();
public static void main(String[] args) {
Main m = new Main();
m.t1.start();
m.t2.start();
}
}