1.编写多线程应用程序,模拟多个人通过一个山洞的模拟。这个山洞每次只能通过一个人,每个人通过山洞的时间为5秒,随机生成10个人,同时准备过此山洞,显示一下每次通过山洞人的姓名。

package com.hanqi.xc;

import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;

public class GuoShanDong implements Runnable {
    
    private static int deng=0;
    @Override
    public void run() {
        
                deng= deng+5000;
                
                try
                {
                    Thread.sleep(deng);
                    
                } catch (InterruptedException e)
                {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
           
                System.out.println(Thread.currentThread().getName()
                    +" 过山洞");
                
               
           
    
        
    }
    public static void main(String[] args) {
        
         
    
        String ary[] ={"赵","钱","孙","李","周","吴","郑","王","冯","陈"};
    
        
        GuoShanDong gsd = new GuoShanDong();
             
        Set<Integer> set=new LinkedHashSet<Integer>();
        while(true){
        if(set.size() == 10){
        break;
        }
        
        //乱序排列(随机)
        int a=(int) (Math.random()*10);
        
        set.add(a);
        }
        for(int b:set){
        
        Thread th = new Thread(gsd, ary[b]);
         
         
         th.start();
        }            
                     
             
       }
        

posted @ 2016-08-05 11:17  凌零聆  阅读(7363)  评论(0编辑  收藏  举报