synchronized 锁和Lock锁不同版本的生产者和消费者,以及Lock锁指定线程运行
synchronized 版本
package com.luoKing.PC;
//synchronized版消费者和生产者
public class A {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
//数据源
class Data{
private int num = 0;
public synchronized void increment() throws InterruptedException {
while(num!=0){
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName()+"->"+num);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while(num==0){
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName()+"->"+num);
this.notifyAll();
}
}
Lock 版本(指定线程运行)
线程运行顺序A->B->c->D->A
package com.luoKing.PC;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class B {
public static void main(String[] args) {
Data2 data2 = new Data2();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data2.decrementA();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data2.incrementB();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data2.decrementC();
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data2.incrementD();
}
},"D").start();
}
}
class Data2{
private int num = 0;
Lock lock = new ReentrantLock();
Condition conditionA = lock.newCondition();//condition可以判断休眠还是唤醒
Condition conditionB = lock.newCondition();//condition可以判断休眠还是唤醒
Condition conditionC = lock.newCondition();//condition可以判断休眠还是唤醒
Condition conditionD = lock.newCondition();//condition可以判断休眠还是唤醒
public void decrementA(){
lock.lock();
try {
while(num!=0){
conditionA.await();//线程等待
}
System.out.println(Thread.currentThread().getName()+"->"+num++);
conditionB.signal();//唤醒指定线程
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void decrementC(){
lock.lock();
try {
while(num!=0){
conditionC.await();//线程等待
}
System.out.println(Thread.currentThread().getName()+"->"+num++);
conditionD.signal();//唤醒所有线程
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void incrementB(){
lock.lock();
try {
while(num!=1){
conditionB.await();
}
System.out.println(Thread.currentThread().getName()+"->"+num--);
conditionC.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void incrementD(){
lock.lock();
try {
while(num!=1){
conditionD.await();
}
System.out.println(Thread.currentThread().getName()+"->"+num--);
conditionA.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}