package lTime;
public class SwitchStatus1 extends Thread {
private static String currentThread = "A";
private static byte[] lock = new byte[0];
private String name = "";
private static int count = 1000;
static int e = 0;
public SwitchStatus1(String name) {
this.name = name;
}
public void run() {
while (count > 0) {
SwitchStatus.r( this.name);
}
System.out.println(name + e);
}
/**
* @param args
*/
public static void main(String[] args) {
(new SwitchStatus1("C")).start();
(new SwitchStatus1("C")).start();
(new SwitchStatus1("A")).start();
(new SwitchStatus1("B")).start();
(new SwitchStatus1("C")).start();
}
static synchronized void r( String name) {
synchronized (SwitchStatus.class) {
e++;
if (currentThread.equals(name)) {
System.out.print(name + count);
count--;
if (currentThread.equals("A")) {
currentThread = "B";
} else if (currentThread.equals("B")) {
currentThread = "C";
} else if (currentThread.equals("C")) {
currentThread = "A";
System.out.println();
}
}
}
}
}
實驗文件 synchronized
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
之前的java測試代碼 性能測試
zqfddqr
2018-08-24 18:44:39
將整數num轉化爲32位的二進制數
zqfddqr
2018-08-24 18:44:38
java 畫sin曲線 java 畫函數曲線
zqfddqr
2018-08-24 18:44:38
netbeans替換掉程序的空行
zqfddqr
2018-08-24 18:44:35