實驗文件 synchronized

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();
}
}
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章