start() 和 system.out 的优先级别

xuhui54 2012-03-22 05:16:22
结果怎么是
Totle Money: 100.0
100.0
100.0
7
1
100.0
withdraw 100 success
0.0
withdraw 100 failed!
附带代码
package two;

public class Account {

double balance;

public Account(double money){
// System.out.println(6);
balance = money;
System.out.println("Totle Money: "+balance);
// System.out.println(3);
}
public void ba() {

System.out.println(balance);
}
public void withdraw(double money){
// System.out.println(2);
synchronized (this) {
if (balance >= money) {
balance = balance - money;
// System.out.println(4);
System.out.println("withdraw 100 success");
}
else
System.out.println("withdraw 100 failed!");
}
}
}


package two;

public class AccountThread extends Thread {

Account acount;
public AccountThread(Account acount) {
// System.out.println(5);
this.acount = acount;
acount.ba();

}
public void run(){
acount.ba();
acount.withdraw(100);
}

}



package two;

public class Tst {

/**
* @param args
*/
public static void main(String[] args) {

Account acount = new Account(100);

AccountThread acountThread31 = new AccountThread(acount);
AccountThread acountThread32 = new AccountThread(acount);
System.out.println(7);
acountThread31.start();
System.out.println(1);
acountThread32.start();
}

}

...全文
104 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Eleven_git 2012-03-23
  • 打赏
  • 举报
回复
public Account(double money)
public void withdraw(double money)

艳沐石 2012-03-22
  • 打赏
  • 举报
回复
多线程的执行结果是不确定的,因为线程的调度是系统完成的。
已删 2012-03-22
  • 打赏
  • 举报
回复
倒数第二行的00是怎么回事
宏Lee 2012-03-22
  • 打赏
  • 举报
回复
如果你想设定优先级,就用setPriority(整数);
冰思雨 2012-03-22
  • 打赏
  • 举报
回复
单个线程来讲,代码的执行还是有先后顺序的。
我说的是,多个线程来看,多个线程的某个语句,和其他线程的某个语句,其先后顺序,没有什么可比性。
冰思雨 2012-03-22
  • 打赏
  • 举报
回复
多线程并发,操作是异步的,无法确定哪个操作在前面执行,
哪行代码先执行,有操作系统调度策略来决定。
当然,如果楼主引入线程同步技术,先后顺序可以得到改善。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧