范例一:
package cn.mldn.cn;
public class TeacherDemo {
public static void main(String[] args){
Resource res = new Resource() ;
TeacherSubThread st = new TeacherSubThread(res) ;
TeacherAddThread at = new TeacherAddThread(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) { // 现在需要执行的是减法操作,加法操作要等待
super.wait();
}
else {
Thread.sleep(100);
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) { // 减法操作需要等待
super.wait();
}else{
Thread.sleep(100);
this.num--;
System.out.println("【减法操作 - " + Thread.currentThread().getName() + "】num = " + this.num);
this.flag = true ;
super.notifyAll();
}
}
}
class TeacherAddThread implements Runnable {
private final Resource resource ;
public TeacherAddThread(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 TeacherSubThread implements Runnable {
private final Resource resource ;
public TeacherSubThread(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();
}
}
}
}
这个主要是在if之后再加else进行判断
范例二:
package cn.mldn.cn;
public class CaseDemo {
public static void main(String[] args) {
Resources res = new Resources();
Thread thread1 = new Thread(new AddThread(res), "加法线程- A");
Thread thread2 = new Thread(new AddThread(res),"加法线程- B");
Thread thread3 = new Thread(new SubThread(res),"减法线程- C");
Thread thread4 = new Thread(new SubThread(res),"减法线程- D");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Resources{
private int num = 0 ;
private boolean flag = true;
public synchronized void add() throws Exception{
while (!this.flag) {
super.wait();
}
Thread.sleep(100);
this.num++;
System.out.println("【加法操作 - " + Thread.currentThread().getName() +"】num" + this.num);
this.flag = false;
super.notifyAll();
}
public synchronized void sub() throws Exception{
while (this.flag) {
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 final Resources resources;
public AddThread ( Resources resources){
this.resources = resources;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int x = 0 ; x < 50 ; x ++){
try {
this.resources.add();
} catch (Exception e) {
e.printStackTrace();
//TODO: handle exception
}
}
}
}
class SubThread implements Runnable{
private final Resources resources;
public SubThread(Resources resources){
this.resources = resources;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int x = 0 ; x < 50 ; x ++){
try {
this.resources.sub();
} catch (Exception e) {
e.printStackTrace();
//TODO: handle exception
}
}
}
}
这是将if改成while,执行前先进行判断
|