?? syndemo.java.txt
字號:
package ch7.section7_5;
public class SynDemo {
public static void main(String[] args) {
MyThread2 m1 = new MyThread2();
Thread t1 = new Thread(m1,"t1"); //根據m1創建兩個子線程
Thread t2 = new Thread(m1, "t2");
t1.start(); //啟動兩個線程
t2.start();
}
}
class MyThread2 implements Runnable {
private int shareData = 0; //共享變量
public synchronized void run() {
Thread t = Thread.currentThread();
for(int i=0; i<5; i++) {
int copy = shareData; //將共享變量復制一份到copy
try {
//休眠若干時間
Thread.sleep((int)(Math.random() * 1000));
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread-" + t.getName() + ": copy = " + copy
+ "\tshareData = " + shareData);
shareData ++;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -