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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 胡帅冰 中级黑马   /  2013-5-31 18:05  /  1551 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 胡帅冰 于 2013-6-2 10:06 编辑

  1. import java.util.*;

  2. class TrafficTest
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 String [] direction = {"S2N"};

  7.                 for(int i=0;i<direction.length;i++)
  8.                 {
  9.                         new Thread(new Road(direction[i])).start();
  10.                         new Thread(new Road_2()).start();
  11.                 }
  12.         }
  13. }

  14. class Road implements Runnable
  15. {
  16.         private String name;
  17.         private        ArrayList<String> car;
  18.         private boolean flg=true;

  19.         Road(String name)
  20.         {
  21.                 this.name=name;

  22.         }
  23.         Road()
  24.         {
  25.         }

  26.         public ArrayList<String> getCar()
  27.         {
  28.                 return car;
  29.         }
  30.         public boolean getFlg()
  31.         {
  32.                 return flg;
  33.         }

  34.         private void comeCar()
  35.         {
  36.                 car=new ArrayList<String>();

  37.                 for(int i=0;i<5;i++)
  38.                 {
  39.                         try
  40.                         {
  41.                         //Thread.sleep(new Random().nextInt(10)*1000+1);
  42.                                 synchronized(TrafficTest.class)
  43.                                 {
  44.                                         car.add(this.name+"_"+i);
  45.                                 }
  46.                         }
  47.                         catch (Exception e)
  48.                         {
  49.                                 System.out.println(e.getMessage());
  50.                         }
  51.                         System.out.println("方向为:: "+this.name+"...的第"+i+"辆车来了。。。");
  52.                 }
  53.                 for(Iterator<String> it=car.iterator();it.hasNext();)
  54.                 {
  55.                         System.out.println(it.next());
  56.                 }
  57.         }
  58.         public void run()
  59.         {
  60.                 comeCar();
  61.         }
  62. }
  63. class Road_2 extends Road  implements Runnable
  64. {
  65.         public void run()
  66.         {
  67.                 ArrayList<String> car=getCar();
  68.                 boolean flg=getFlg();
  69.                         while(true)
  70.                         {
  71.                                         synchronized(TrafficTest.class)
  72.                                         {
  73.                                                 if(flg)
  74.                                                 {
  75.                                                         System.out.println(car.remove(0)+"is go");
  76.                                                 }
  77.                                         }
  78.                         }
  79.         }
  80. }
复制代码
求解怎么解决这个问题。问题一: 为什么这个空指针异常会出现在运行的时候??
问题二: 怎么解决,怎么实现同步??
说明:这个是在交通灯视频看过之后,我写的一段测试Road类的,使用的是实现Runnable类,没有使用视频中的Executor框架的Executors工具类。
谢谢各位了。。。。

评分

参与人数 1技术分 +1 收起 理由
殇_心。 + 1

查看全部评分

6 个回复

倒序浏览

回帖奖励 +3

其实  我只是   来拿金币的
回复 使用道具 举报
李阳阳 发表于 2013-5-31 18:29
其实  我只是   来拿金币的

哇嘎,来人,拖出去乱棍打死。。。:lol
回复 使用道具 举报
其实,我不是来拿金币的
回复 使用道具 举报
有吗有吗
回复 使用道具 举报
还有吗?(*^__^*) 嘻嘻……
回复 使用道具 举报
奖励在哪里
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马