实验文件 synchronized
来源:互联网 发布:喜马拉雅电台mac版 编辑:程序博客网 时间:2024/05/29 02:35
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();
}
}
}
}
}
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
- Android同步锁:Synchronized的小实验
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- synchronized
- 【OpenCV】图像代数运算:平均值去噪,减去背景
- 大型web构架设计--学习笔记
- C语言 堆形式存储的字符串操作
- string c++ 详解3 at
- SQL Server请求订阅:查看订阅状态提示 无法连接到distributor 'xxx'
- 实验文件 synchronized
- 网络爬虫-URL去重
- java多线程基础
- Struts1.2中文过滤
- 写一个从/proc/devices 文件中获取设备号的脚本
- 结构性模式之Composite模式
- java 内部类
- U-Boot and MMC/SDIO
- XAF之创建高级Information Panel