设计4个线程对象,两个线程执行减操作,两个线程执行加操作。
package multithreading;
public class Demo1 {
public static void main(String[] args) {
Resource res= new Resource();
AddThread at = new AddThread(res);
SubThread st = new SubThread(res);
new Thread(at,"加法线程-A").start();
new Thread(at,"加法线程-B").start();
new Thread(st,"减法线程-X").start();
new Thread(st,"减法线程-Y").start();
}
}
class Resource{ //定义一个操作的资源
private int num = 0; //这个要进行加减操作的数据
private boolean flag = true; //加减的切换
//flag = true表示可以进行加法操作,无法进行减法操作
//flag = false表示可以进行减法操作,无法进行加法操作
public synchronized void add() throws Exception { //执行加法操作
if(this.flag == false) { //现在需要执行的是减法操作,加法操作要等待
super.wait();//Object的等待方法
}
Thread.sleep(200);
this.num++;
System.out.println("【加法操作-"+Thread.currentThread().getName() +"】num = "+this.num);
this.flag = false;//执行完以后执行减法
super.notifyAll();//唤醒全部等待线程
}
public synchronized void sub() throws Exception { //执行减法操作
if(this.flag == true) {//现在执行的是加法操作,减法需要等待
super.wait();
}
Thread.sleep(200);
this.num--;
System.out.println("【减法操作-"+Thread.currentThread().getName()+"】num = "+this.num);
this.flag = true;
super.notifyAll();
}
}
class AddThread implements Runnable{
private Resource resource;
public AddThread(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
for(int x = 0;x<50;x++) {
try {
this.resource.add();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class SubThread implements Runnable{
private Resource resource;
public SubThread(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
for(int x = 0;x<50;x++) {
try {
this.resource.sub();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这一题是一个经典的多线程的开发操作,这一程序里面一定要考虑的核心本质在于:加一个、减一个,整体的计算结果应该只在0、-1、1之间循环出现才是合理的做法