博客
关于我
多线程---模拟多人通过山洞
阅读量:340 次
发布时间:2019-03-04

本文共 1029 字,大约阅读时间需要 3 分钟。

* 编写多线程应用程序,模拟多个人通过山洞。这个山洞每次只能通过一个人,每个人 * 通过山洞的时间为5秒,随机生成10个人,名字分别是"ONE"~"TEN",同时准备过此山洞, * 显示一下每次通过山洞人的姓名。 * 提示:1.获取当前线程名字可用Thread.currentThread().getName()方法 *     2.输出结果应该是乱序的*/public class Pass implements Runnable{      private Object lock=new Object();      public void run(){          synchronized(lock){              System.out.println(Thread.currentThread().getName());              try{                  Thread.sleep(1000);              }catch(InterruptedException e){                  e.printStackTrace();              }          }      }            public static void main(String[] args){          Pass p=new Pass();          new Thread(p,"ONE").start();          new Thread(p,"TWO").start();          new Thread(p,"THREE").start();          new Thread(p,"FOUR").start();          new Thread(p,"FIVE").start();          new Thread(p,"SIX").start();          new Thread(p,"SEVEN").start();          new Thread(p,"EIGHT").start();        new Thread(p,"NINE").start();        new Thread(p,"TEN").start();            }  }  

转载地址:http://wzne.baihongyu.com/

你可能感兴趣的文章
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>
vue3+Ts 项目打包时报错 ‘reactive‘is declared but its value is never read.及解决方法
查看>>
Node-RED中Switch开关和Dropdown选择组件的使用
查看>>