三种写法
public class Thread5 {
private static int num = 0;
private static int MAX_NUM = 20;
private static Object lock = new Object();
public static void subNum() {
synchronized (lock) {
while (num == 0) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num--;
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.notifyAll();
System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",消费: " + 1);
}
}
public static void addNum() {
synchronized (lock) {
while (num == MAX_NUM) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num++;
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.notifyAll();
System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",生产: " + 1);
}
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
subNum();
}
});
thread.setName("消费者" + i);
thread.start();
}
for (int j = 0; j < 10; j++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
addNum();
}
});
t.setName("生产者" + j);
t.start();
}
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Thread6 {
private static int num = 0;
private static int MAX_NUM = 20;
private static Lock lock = new ReentrantLock();
private static Condition full = lock.newCondition();
private static Condition empty = lock.newCondition();
public static void subNum() {
lock.lock();
while (num == 0) {
try {
empty.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num--;
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
full.signalAll();
System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",消费: " + 1);
lock.unlock();
}
public static void addNum() {
lock.lock();
while (num == MAX_NUM) {
try {
full.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
num++;
try {
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
empty.signalAll();
System.out.println(Thread.currentThread().getName() + ",当前num: " + num + ",生产: " + 1);
lock.unlock();
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
subNum();
}
});
thread.setName("消费者" + i);
thread.start();
}
for (int j = 0; j < 10; j++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
addNum();
}
});
t.setName("生产者" + j);
t.start();
}
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Thread7 {
private static int MAX_NUM = 2;
private static BlockingQueue<String> queue = new ArrayBlockingQueue<String>(MAX_NUM);
public static void subNum() {
try {
String item = queue.take();
System.out.println(Thread.currentThread().getName() + ",当前num: " + queue.size() + ",消费: " + 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void addNum() {
try {
queue.put("1");
System.out.println(Thread.currentThread().getName() + ",当前num: " + queue.size() + ",生产: " + 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
subNum();
}
});
thread.setName("消费者" + i);
thread.start();
}
for (int j = 0; j < 10; j++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
addNum();
}
});
t.setName("生产者" + j);
t.start();
}
}
}