- 创建资源类,在资源类创建属性和操作方法
- 在资源类操作方法
1判断
2.干活
3.通知
3. 创建多个线程,调用资源类的操作方法
4. 防止虚假唤醒问题 (wait()方法要放在循环里)
package lock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Shareess {
private int number = 0;
private Lock locks = new ReentrantLock();
private Condition condition = locks.newCondition();
//+1
public void incr() {
locks.lock();
try {
//判断
while (number != 0) {
condition.await();
}
//干活
number++;
System.out.println(Thread.currentThread().getName() + "::" + number);
//通知
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
locks.unlock();
}
}
//-1
public void decr() throws InterruptedException {
locks.lock();
try {
while (number!=1){
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"::"+number);
condition.signalAll();
}finally {
locks.unlock();
}
}
}
public class ThreadDemo2 {
public static void main(String[] args) {
Shareess shareess = new Shareess();
new Thread(()->{
for (int i =1;i<10;i++){
shareess.incr();
}
},"aa").start();
new Thread(()->{
for (int i =1;i<10;i++){
try {
shareess.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"bb").start();
new Thread(()->{
for (int i =1;i<10;i++){
shareess.incr();
}
},"ccc").start();
new Thread(()->{
for (int i =1;i<10;i++){
try {
shareess.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"ddd").start();
}
}
线程间的定制化通信
package lock;
//第一步 创建资源类
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ShareResource{
//定义标志位
private int flag =1; //1 AA 2 BB 3 CC
private Lock lock = new ReentrantLock();
//创建三个condition
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();
//打印5次,参数几轮
public void print5(int loop) throws InterruptedException {
lock.lock();
try {
//判断
while (flag !=1){
//等待
c1.await();
}
//干活
for (int i =1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+"::"+i+":轮数:"+loop);
}
//通知
flag=2;
c2.signal();
}finally {
lock.unlock();
}
}
//打印10次,参数几轮
public void print10(int loop) throws InterruptedException {
lock.lock();
try {
//判断
while (flag !=2){
//等待
c2.await();
}
//干活
for (int i =1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+"::"+i+":轮数:"+loop);
}
//通知
flag=3;
c3.signal();
}finally {
lock.unlock();
}
}
//打印15次,参数几轮
public void print15(int loop) throws InterruptedException {
lock.lock();
try {
//判断
while (flag !=3){
//等待
c3.await();
}
//干活
for (int i =1;i<=5;i++){
System.out.println(Thread.currentThread().getName()+"::"+i+":轮数:"+loop);
}
//通知
flag=1;
c1.signal();
}finally {
lock.unlock();
}
}
}
public class ThreadDemo3 {
public static void main(String[] args) {
ShareResource shareResource = new ShareResource();
new Thread(()->{
for (int i =1;i<=10;i++){
try {
shareResource.print5(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"aa").start();
new Thread(()->{
for (int i =1;i<=10;i++){
try {
shareResource.print10(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"bb").start();
new Thread(()->{
for (int i =1;i<=10;i++){
try {
shareResource.print15(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"cc").start();
}
}
运行结果: