cave

来源:互联网 发布:网络连接怎么设置 编辑:程序博客网 时间:2024/04/28 18:10
  1. import java.util.Random;  
  2.   
  3.  public class AcrossCave {  
  4.     public static void main(String[] args) {  
  5.           
  6.         for(int i=0;i<10;i++){  
  7.             People peo=new People(getRandomString(6));  
  8.             new Thread(peo).run();  
  9.         }  
  10.     }  
  11.     public static String getRandomString(int length) {   
  12.         String base = "abcdefghijklmnopqrstuvwxyz";     
  13.         Random random = new Random();     
  14.         StringBuffer sb = new StringBuffer();     
  15.         for (int i = 0; i < length; i++) {     
  16.             int number = random.nextInt(base.length());     
  17.             sb.append(base.charAt(number));     
  18.         }     
  19.         return sb.toString();     
  20.      }   
  21. }  
  22. class People implements Runnable{  
  23.     String name;  
  24.       
  25.     public People(String name) {  
  26.         super();  
  27.         this.name = name;  
  28.     }  
  29.   
  30.     @Override  
  31.     public synchronized void run() {  
  32.         try {  
  33.             Thread.sleep(2000);  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         }finally {  
  37.             System.out.println(name+"通过了山洞。");  
  38.         }             
  39.     }  
  40. }
  41.   
原创粉丝点击