for - 程式設計【多執行緒同步處理】

1 view
Skip to first unread message

q738...@yahoo.com.tw

unread,
Dec 29, 2010, 2:38:48 AM12/29/10
to EnJoyJava
class bank extends Thread {

private static int sum = 0;

public bank (String name) {
this.setName(name);
}

public void run () {

for (int i=1; i <= 5; i++)
try {
Thread.sleep(1000);
sum += 50;
System.out.printf("「%s」累加匯款「%d」\n",getName(),sum);
} catch (Exception e) {
System.out.println("程式發生錯誤");
}
}
}

public class example {

public static void main (String[] ymc) throws Exception{

bank bank1 = new bank("王小明");
bank bank2 = new bank("金大蔥");

bank1.start();
bank2.start();

}
}
Reply all
Reply to author
Forward
0 new messages