题目描述
编写多线程应用程序,模拟三个人Tom,Peter,Bob过山洞:
1、这个山洞每次只能通过一个人,每个人通过山洞的时间为1秒
2、过山洞次序为:Tom,Peter,Bob
将下列代码补充完整:
public class Main{
public static void main(String[] args) {
Tunnel tul = new Tunnel();
Thread tom = new Thread(tul,\"Tom\");
// 你的代码将嵌入这里
输出描述
Tom have Crossed the tunnel!This is 1th
Peter have Crossed the tunnel!This is 2th
Bob have Crossed the tunnel!This is 3th
Thread peter = new Thread(tul,\"Peter\");
Thread bob = new Thread(tul,\"Bob\");
tom.start();
peter.start();
bob.start();
}
}
class Tunnel implements Runnable
{
public void run()
{
for(int i = 1;i<=3;i++)
{
if(i == 1&&Thread.currentThread().getName().equals(\"Tom\"))
{
System.out.println(\"Tom\"+\" have Crossed the tunnel!This is \"+i+\"th\");
i++;
}
if(i == 2&&Thread.currentThread().getName().equals(\"Peter\"))
{
System.out.println(\"Peter\"+\" have Crossed the tunnel!This is \"+i+\"th\");
i++;
}
if(i == 3&&Thread.currentThread().getName().equals(\"Bob\"))
{
System.out.println(\"Bob have Crossed the tunnel!This is \"+i+\"th\");
i++;
}
}
}
}
继续阅读与本文标签相同的文章
下一篇 :
深度学习人脸检测和识别系统 DFace
-
阿里架构总监一次讲透中台架构,13页PPT精华详解,建议收藏!
2026-05-19栏目: 教程
-
好程序员web前端学习路线解答前后端对接问题
2026-05-19栏目: 教程
-
五大典型场景中的API自动化测试实践
2026-05-19栏目: 教程
-
历时五天用 SwiftUI 做了一款 APP,阿里工程师如何做的? | 9月5号栖夜读
2026-05-19栏目: 教程
-
CAD如何批量导出PDF文件?别说PDF了!GIF我都能给你导出来
2026-05-19栏目: 教程
