public class ThreadRunnable{
public static void main(String[] args){
Person.t1.start();
Person.t2.start();
}
}
class Person implements Runnable{
public int i;
static Thread t1 = new Thread(new Person(1));
static Thread t2 = new Thread(new Person(2));
public Person(int i){
this.i = i;
}
ATM atm = new ATM();
public void run() {
synchronized(atm){
if(i==1){
try {
ATM.withDraw(20,i);
atm.wait();
ATM.dispose(20, i);
atm.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
try {
ATM.withDraw(20, i);
atm.notify();
atm.wait();
ATM.dispose(20, i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class ATM{
static double total=1000; public static void dispose(double x,int i){
total = total +x;
System.out.println("person"+i+" dispose");
System.out.println(ATM.getTotals());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void withDraw(double x,int i){
total = total -x;
System.out.println("person"+i+" withDraw");
System.out.println("the rest of money"+ATM.getTotals());
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static double getTotals(){
return total;
}
}这是程序,我想让它的结果是:
person1 withDraw
the rest money 980
person2 withDraw
the rest money 960
person1 dispose
the rest money 980
person2 dispose
the rest money 1000但是它现在有问题.不是期望的结果.5555555555请大家帮帮我.
解决方案 »
- 如何将数据库中的信息写入到16进制文件以减小其占用空间
- 请问为什么我在写入文件数据的时候会出现非法字母啊
- 急求解决办法:文本很大,怎样一边读一边写 用filereader 和filewriter会抛出java.outofememory.exception 有没有别的办法 谢谢
- 紧急问题,望大家帮忙解答
- java里怎样使得中文字符串按拼音排序?
- 学jsp的请进?100分送上
- 如何对某个日期加一天,或加几秒,或加几分钟,或加几小时
- 请教各位关于 new
- java的 3d开发怎么搞啊,是用OpenGL还是什么其他的?up有分
- 一个小问题,让你笑掉大牙
- 有关JAVA程序执行问题
- eclipse导出问题
public int i; static Thread t1 = new Thread(new Person(1)); static Thread t2 = new Thread(new Person(2)); public Person(int i) {
this.i = i;
} ATM atm = new ATM(); public void run() {
synchronized (atm) {
try {
if (i == 1) {
ATM.withDraw(20, i);
// atm.wait();
ATM.dispose(20, i);
// atm.notify();
} else {
ATM.withDraw(20, i);
// atm.notify();
// atm.wait();
ATM.dispose(20, i);
}
Thread.sleep(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
加一行Thread.sleep(0);
结果:
person1 withDraw
the rest of money980.0
person2 withDraw
the rest of money960.0
person1 dispose
980.0
person2 dispose
1000.0
ATM应该是只有一个吧.你做了两个线程,有了两个ATM了.
public class t {
public static void main(String args[]){
B b=new B();
Person person1=new Person(b);
Person person2=new Person(b);
person1.j=200;
person1.k=500;
person2.j=300;
person2.k=600;
new Thread(person1).start();
new Thread(person2).start();
}}
class Person implements Runnable{
B b;
public Person(B b){
this.b=b;
}
int j=0;
int k=0;
public void run(){
b.get(j);
b.put(k);
}
}
class B{
int i=1000;
boolean b=true;
public synchronized void get(int j){
if(b){
i-=j;
try {
System.out.println(Thread.currentThread().getName()+" comes,gets money"+j+"元");
System.out.println("the rest money is"+i);
notify();
wait();
b=false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void put(int j){
if(!b){
i+=j;
System.out.println(Thread.currentThread().getName()+"puts money"+j+"元");
System.out.println("the rest money is"+i);
b=true;
notify();
}
else{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出结果
Thread-0 comes,gets money200元
the rest money is800
Thread-1 comes,gets money300元
the rest money is500
Thread-0puts money500元
the rest money is1000
Thread-1puts money600元
the rest money is1600
我初步看了下你的代码,发现你这里有个问题
ATM atm = new ATM(); //这里,你每开启一个线程,他都新建一个ATM对象
public void run() {
synchronized(atm){ //这里,同步块中的锁只是这个线程中的atm对象锁
当另一个线程到来时,两个线程所用的锁是不同的,
else{ try {
ATM.withDraw(20, i);
atm.notify(); //你在这里调用notify()方法并不能把第一个线程中同步快中的锁给解锁了
atm.wait();
ATM.dispose(20, i); 所以呢,你这个程序最后输出的结果肯定是
person1 withDraw
the rest of money980.0
person2 withDraw
the rest of money960.0
而不是你理想中的输出,想要达到理想中的输出,就用我的代码吧,8楼的,呵呵,或者把你自己的代码改下,统一用一把对象锁就可以了
如果是成员变量,那输出肯定是
person1 withDraw
the rest of money980.0
person2 withDraw
the rest of money980.0
不信你试试