路人过河
package victors;
import java.util.Random;
public class Test { public static void main(String[] args) {
// TODO 自动生成的方法存根Guoddong guoddong = new Guoddong();
// Thread thread = new Thread();
Random random = new Random();
String str = "abcdefghijklmnopqrstuvwxyz";
for (int i = 1;
i <= 10;
i++) {
Thread thread = new Thread(guoddong);
String itemStr = "" + str.charAt(random.nextInt(str.length())) + str.charAt(random.nextInt(str.length()))
+ str.charAt(random.nextInt(str.length())) + str.charAt(random.nextInt(str.length())) + i;
// 随机生成四个
thread.setName("线程" + i + ":路人" + itemStr);
thread.start();
}
}}
package victors;
public class Guoddong implements Runnable {
private Object lock = new Object();
@Override
public void run() {
// TODO 自动生成的方法存根
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + "正在通过山洞");
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "通过山洞");
}
}
}
文章图片
推荐阅读
- 六步搭建ES6语法环境
- ExcelPackage读取写入
- 藏族开发的修路人——记致富援乡的斯定那珠
- Hi|Hi,路人甲
- Sql2oUtil
- 数据库|效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】)...
- 基础3-API-值传递
- 代言被撤,路人缘败尽,肖战被骂滚出娱乐圈(被粉丝害惨了)
- 268
- 让路人粉期待续集的《游侠索罗(星球大战外传》)