java多线程的问题,没有报错,无法编译。

任务要求:账号里边有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();

}


}

没有报错,为什么编译时候没有结果?

http://img.mukewang.com/5843d191000173fa04200281.jpg

慕粉3505864
浏览 1754回答 1
1回答

肥冰

wait(); ->   wait(1000);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java