A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 你为谁归来 中级黑马   /  2014-5-8 00:26  /  815 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

代码可以运行,也达到了我想要的结果,但是偶尔会出现角标越界异常,找了好久都没发现问题。一来分享下自己写的代码,二来还可以找高手给看下,那里出的问题。不是每次都会出现异常的,只是偶尔,可能是同步的问题,但是不知道问题出在了那里?
  1. import java.util.*;

  2. public class Bank
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 new Thread(new Number(1000)).start();//开启调用随机生成人员的线程
  7.                 for(int i = 1 ;i<=10;i++)//开启10个窗口的线程
  8.                 {
  9.                         new Thread(new Monitor(i)).start();
  10.                 }
  11.         }

  12. }

  13. class Monitor implements Runnable
  14. {
  15.         public int y1;
  16.         public Monitor(int y1)
  17.         {
  18.                 this.y1=y1;
  19.         }
  20.         public synchronized void sop()
  21.         {
  22.                 if (Number.al1.size() > 0)
  23.                 {
  24.                         Number.al1.remove(0);//移除集合的第一个元素,表示处理好一个客户
  25.                         System.out.println("第" + y1 + "窗口处理了一个普通客户");
  26.                         try
  27.                         {
  28.                                 Thread.sleep((new Random().nextInt(10)+1)*100);//处理一个客户用的时间随机取值
  29.                         } catch (Exception e)
  30.                         {
  31.                                 e.printStackTrace();
  32.                         }
  33.                 }
  34.         }
  35.         public void run()
  36.         {
  37.                 while (true)
  38.                 {
  39.                         if (y1 >= 1 && y1 <= 6)
  40.                         {
  41.                                 sop();
  42.                                
  43.                         } else if (y1 == 7)
  44.                         {
  45.                                 if (Number.al2.size() > 0)
  46.                                 {
  47.                                         Number.al2.remove(0);
  48.                                         System.out.println("第" + y1 + "窗口处理了一个VIP客户");
  49.                                         try
  50.                                         {
  51.                                                 Thread.sleep((new Random().nextInt(10)+1)*100);
  52.                                                 //处理一个客户用的时间随机取值
  53.                                         } catch (Exception e)
  54.                                         {
  55.                                                 e.printStackTrace();
  56.                                         }
  57.                                 } else
  58.                                 {
  59.                                         sop();
  60.                                 }
  61.                         } else
  62.                         {
  63.                                 if (Number.al3.size() > 0)
  64.                                 {
  65.                                         Number.al3.remove(0);
  66.                                         System.out.println("第" + y1 + "窗口处理了一个快速客户");
  67.                                         try
  68.                                         {
  69.                                                 Thread.sleep((new Random().nextInt(10)+1)*100);
  70.                                                 //处理一个客户用的时间随机取值
  71.                                         } catch (Exception e)
  72.                                         {
  73.                                                 e.printStackTrace();
  74.                                         }
  75.                                 } else
  76.                                 {
  77.                                         sop();
  78.                                 }
  79.                         }
  80.                         try
  81.                         {
  82.                                 Thread.sleep((new Random().nextInt(10)+1)*100);
  83.                         } catch (Exception e)
  84.                         {
  85.                                 e.printStackTrace();
  86.                         }
  87.                 }
  88.         }

  89. }



  90. class Number implements Runnable
  91. {
  92.         public int x;
  93.         public int o1;
  94.         public int o2=1;
  95.         public static int uuu=1;
  96.         public static ArrayList<String> al1,al2,al3;
  97.         public Number(int o1)//构造函数用于生成3个集合保存3种客户
  98.         {
  99.                 this.o1=o1;
  100.                 al1 = new ArrayList<String>();
  101.                 al2 = new ArrayList<String>();
  102.                 al3 = new ArrayList<String>();
  103.         }
  104.        
  105.         public void run()
  106.         {
  107.                 Randomgeneration();
  108.         }
  109.         public void Randomgeneration()
  110.         {
  111.                 while(o2<=o1)
  112.                 {
  113.                         x = (int) (Math.random() * 10 + 1);//随机生成1-10用于判断来的人员是什么客户
  114.                         Judge(x);
  115.                         try
  116.                         {
  117.                                 Thread.sleep((new Random().nextInt(10)+1)*100);
  118.                                 //这个时间要注意下,随机生成人员来的时间
  119.                         } catch (Exception e)
  120.                         {
  121.                                 e.printStackTrace();
  122.                         }
  123.                         o2++;//人员截至条件
  124.                 }
  125.         }
  126.         public void Judge(int y)
  127.         {
  128.                 if(y>=1&&y<=6)
  129.                 {
  130.                         al1.add("people");
  131.                         System.out.println("第"+uuu+++"号来了一个普通客户");
  132.                         //在普通客户的数组进行添加1个人员
  133.                 }else if(y==7)
  134.                 {
  135.                         al2.add("people");
  136.                         System.out.println("第"+uuu+++"号来了一个VIP客户");
  137.                         //在VIP客户的数组进行添加1个人员
  138.                 }else
  139.                 {
  140.                         al3.add("people");
  141.                         System.out.println("第"+uuu+++"号来了一个快速客户");
  142.                         //在快速客户的数组进行添加1个人员
  143.                 }
  144.         }
  145. }
复制代码

1 个回复

倒序浏览
mark一下,占个楼
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马