任务要求:账号里边有1000元创业基金。规定:其中的两位同学有取钱权限,用于购置物资。另外两位同学有存钱权限,用于将营利的资金存回到账号中。模拟银行系统对用户存款和取款的处理过程。
源码:
class Resource{
private String name;
private int money = 1000;
private boolean flag = false;
public synchronized void set(String name){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name = name+money;
money++;
System.out.println(Thread.currentThread().getName()+"..已存入.."+this.name);
flag = true;
notifyAll();
}
public synchronized void out(){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"..已提取.."+this.name);
flag = true;
notifyAll();
}
}
class depositors implements Runnable{
private Resource r;
public depositors(Resource r){
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
r.set("存钱");
}
}
}
class Draw implements Runnable{
private Resource r;
public Draw(Resource r){
this.r = r;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
r.out();
}
}
}
public class SetAndOutMoney {
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource r = new Resource();
Draw dr =new Draw(r);
depositors de = new depositors(r);
Thread t1 = new Thread(dr);
Thread t2 = new Thread(dr);
Thread t3 = new Thread(de);
Thread t4 = new Thread(de);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
没有报错,为什么编译时候没有结果?