Java?殻??殻議揖化-揖化圭隈
扮寂:2011-02-20 51cto鴬人 leizhimin
?殻議揖化頁Java謹?殻園殻議佃泣?吏吏蝕窟宀吾音賠萱焚担頁昇尸彿坿、焚担扮昨俶勣深打揖化?奕担揖化吉吉諒籾?輝隼?宸乂諒籾短嗤載苧鳩議基宛?徽嗤乂圻夸諒籾俶勣深打?頁倦嗤昇尸彿坿瓜揖扮個強議諒籾?
壓云猟岻念?萩歌堋ゞJava?殻??殻議揖化嚥迄〃?云猟頁壓緩児粥貧侭亟議。
斤噐揖化?壓醤悶議Java旗鷹嶄俶勣頼撹匯和曾倖荷恬?
委昇尸恵諒議彿坿炎紛葎private?
揖化陳乂俐個延楚議旗鷹?聞喘synchronized購囚忖揖化圭隈賜旗鷹。
輝隼宸音頁率匯陣崙旺窟芦畠議余抄。
synchronized購囚忖聞喘傍苧
synchronized峪嬬炎芝掲渇?議圭隈?音嬬炎紛撹埀延楚。
葎阻處幣揖化圭隈議聞喘?更秀阻匯倖佚喘触嬲薩?軟兜佚喘駆葎100w?隼朔庁亭邑屶、贋錘吉謹倖荷恬。?隼咢佩嬲薩User斤?頁倖昇尸彿坿?遇謹倖旺窟荷恬議頁嬲薩圭隈oper(int x)?輝隼哘乎壓緩圭隈貧紗貧揖化?旺繍嬲薩議噫駆譜葎暴嗤延楚?鋤峭岷俊恵諒。
/**
* Java?殻??殻議揖化
*
* @author leizhimin
*/
public class Test {
public static void main(String[] args) {
User u = new User("嫖眉", 100);
MyThread t1 = new MyThread("?殻A", u, 20);
MyThread t2 = new MyThread("?殻B", u, -60);
MyThread t3 = new MyThread("?殻C", u, -80);
MyThread t4 = new MyThread("?殻D", u, -30);
MyThread t5 = new MyThread("?殻E", u, 32);
MyThread t6 = new MyThread("?殻F", u, 21);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
}
}
class MyThread extends Thread {
private User u;
private int y = 0;
MyThread(String name, User u, int y) {
super(name);
this.u = u;
this.y = y;
}
public void run() {
u.oper(y);
}
}
class User {
private String code;
private int cash;
User(String code, int cash) {
this.code = code;
this.cash = cash;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
/**
* 匍暦圭隈
* @param x 耶紗x嵐圷
*/
public synchronized void oper(int x)
|