zoukankan      html  css  js  c++  java
  • 过山洞 Java

    题目描述

    编写多线程应用程序,模拟三个人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++;
    			}
    		}
    			
    	}
    }
    
  • 相关阅读:
    PHP分页类
    phpexcel 控制导出数据内容和行数
    phpexcel 导入 和 导出
    PHP无限极分类
    php 对查询结果集进行排序
    php 删除文件夹及文件夹内文件函数
    php 字符串截取函数
    php 获取用户登录的ip
    js layer页面层加载新网站
    分享到qq
  • 原文地址:https://www.cnblogs.com/Romantic-Chopin/p/12451249.html
Copyright © 2011-2022 走看看