java 编写多线程应用程序,模拟多个人通过独木桥的模拟。 线程问题

xiaoxiao2021-02-28  94

线程 编写多线程应用程序,模拟多个人通过独木桥的模拟。 这个独木桥每次只能通过一个人,每个人通过独木桥的时间为3秒,随机生成10个人, 同时准备过此独木桥,显示一下每次通过独木桥人的姓名。*/ public class Demo6 { public static void main(String[] args) { person pp=new person(); pp.thread();      }   } class   person implements Runnable{ public void run() { synchronized(this) { try { Thread.sleep(3000);//设置时间为3秒 System.out.println(Thread.currentThread().getName()+"--->过完独木桥"); } catch (InterruptedException e) { e.printStackTrace();  } }     } public void thread() {   person pp=new person();    for (int i = 0; i < 10; i++) {     Thread thread=new Thread(pp);  thread.start(); }     } }
转载请注明原文地址: https://www.6miu.com/read-46125.html

最新回复(0)