?? app14_7.java
字號(hào):
// app14_7, 沒有同步處理的線程
class CBank
{
private static int sum=0;
public static void add(int n){
int tmp=sum;
tmp=tmp+n; // 累加匯款總額
try{
Thread.sleep((int)(1000*Math.random())); // 小睡0~1秒鐘
}
catch(InterruptedException e){}
sum=tmp;
System.out.println("sum= "+sum);
}
}
class CCustomer extends Thread // CCustomer類,繼承自Thread類
{
public void run(){ // run() method
for(int i=1;i<=3;i++)
CBank.add(100); // 將100元分三次匯入
}
}
public class app14_7
{
public static void main(String args[])
{
CCustomer c1=new CCustomer();
CCustomer c2=new CCustomer();
c1.start();
c2.start();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -