?? machine.java
字號:
package suspend;
public class Machine extends Thread{
private int a; //共享數據
public void run(){
for(int i=0;i<1000;i++){
synchronized(this){
a+=i;
yield(); //給其他線程運行的機會
a-=i;
}
}
}
public synchronized void reset(){ a=0;}
public static void main(String args[]) throws InterruptedException{
Machine machine=new Machine();
machine.start();
yield(); //給machine線程運行的機會
machine.suspend(); //讓machine線程暫停
machine.reset(); //調用machine對象的同步代碼塊
machine.resume(); //使machine線程恢復運行
}
}
/****************************************************
* 作者:孫衛琴 *
* 來源:<<Java面向對象編程>> *
* 技術支持網址:www.javathinker.org *
***************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -