黑马程序员技术交流社区

标题: 若将ArarryList<E> 同步,线程启动后执行无效...帮忙改程序 [打印本页]

作者: jonn    时间: 2013-1-21 13:38
标题: 若将ArarryList<E> 同步,线程启动后执行无效...帮忙改程序
本帖最后由 张向辉 于 2013-1-22 12:42 编辑
  1. package com.jonn.demo;
  2. import java.util.*;
  3. import java.util.concurrent.locks.*;
  4. class Person implements Runnable
  5. {
  6.         private List<Person> person=ArrayListDemo.getIntance();
  7.     private Lock lock=new ReentrantLock();
  8.         private Condition condi_addEl=lock.newCondition();
  9.         private Condition condi_delEl=lock.newCondition();
  10.         private static boolean flag=false;
  11.         private String name;
  12.         private int age;
  13.         Person(String name,int age){
  14.                 this.setPersonInfo(name,age);
  15.         }
  16.         public void setPersonInfo(String name,int age){
  17.                 this.name=name;
  18.                 this.age=age;
  19.         }
  20.         public String  getPersonInfo(){
  21.                 return this.name+","+this.age;
  22.         }
  23.         public String toString(){
  24.                 return this.getPersonInfo();
  25.         }
  26.   public void addElements(Person p) throws InterruptedException{
  27.        lock.lock();
  28.            try{
  29.                   if(flag)
  30.                   condi_addEl.await();
  31.                   person.add(p);
  32.                   System.out.println(Thread.currentThread().getName()+"---添加元素---"+p);
  33.                   ArrayListDemo.printList(person);
  34.                   flag=true;
  35.                   condi_delEl.signal();
  36.            }
  37.            finally{
  38.                    lock.unlock();
  39.            }
  40.   }
  41.   public void delElements(Person p) throws InterruptedException{
  42.     lock.lock();
  43.         try{
  44.                 if(!flag)
  45.                 condi_delEl.await();
  46.                 person.remove(p);
  47.                 System.out.println(Thread.currentThread().getName()+"---删除元素---"+p);
  48.                 ArrayListDemo.printList(person);
  49.                 flag=false;
  50.                 condi_addEl.signal();
  51.         }
  52.         finally{
  53.         lock.unlock();
  54.         }
  55.   }
  56.   public void run(){
  57.           int i=0;
  58.           while(flag){
  59.                       if(i==0)
  60.                                   try{
  61.                                           addElements(new Person("gao",24));
  62.                                   }catch(InterruptedException e){
  63.                                           changeFlag();
  64.                                   }
  65.                           else
  66.                                   try{
  67.                                           delElements(null);
  68.                                   }catch(InterruptedException e)
  69.                                   {
  70.                                           changeFlag();
  71.                                   }
  72.           i=(i+1)%2;
  73.           }
  74.   }

  75. public static boolean changeFlag(){
  76.         return flag=false;
  77.   }
  78. };

  79. class ArrayListDemo
  80. {
  81.         private static List<Person> person=null;
  82.         private ArrayListDemo(){
  83.    
  84.         }
  85.     public static List<Person> getIntance(){
  86.                 if(person==null){
  87.                         if(person==null)
  88.                                 person=Collections.synchronizedList(new ArrayList<Person>());
  89.                 }
  90.                         return person;
  91.         }
  92.         protected static void printList(List<?> e){
  93.          Iterator<?> iter=e.iterator();
  94.      while(iter.hasNext()){
  95.                  System.out.println(iter.next());
  96.            }
  97.          }

  98.          private static void printInfo(List<Person> e){
  99.            for(Iterator iter=e.iterator();iter.hasNext();){
  100.                    Person p=(Person)iter.next();
  101.                    System.out.println(p);
  102.            }
  103.          }
  104.         public static void main(String[] args)
  105.         {
  106.                  
  107.         Person[] p={
  108.                                 new Person("张三",22),
  109.                                         new Person("李四",23),
  110.                                     new Person("王五",22)
  111.                                         };

  112.              person.addAll(Arrays.asList(p));
  113.          person.add(1,new Person("六路",24));
  114.                  Thread t1=new Thread(new Person("lisi",25));
  115.                  Thread t2=new Thread(new Person("kk",23));
  116.                  t1.setDaemon(true);                       
  117.                  t2.setDaemon(true);
  118.                  t1.start();                                // 线程启动后,执行run()方法无效,无结果,求改代码....
  119.                  t2.start();
  120.                
  121.          int x=0;
  122.                  boolean flag=true;
  123.                  while(flag){
  124.             if(x==50){
  125.                                 flag=false;
  126.                         }
  127.                         x++;
  128.          System.out.println(Thread.currentThread().getName()+"---"+x);
  129.                 }
  130.         
  131.         }
  132. };
复制代码

作者: 黑马张英涛    时间: 2013-1-21 13:48
好长。。。头疼。。
作者: jonn    时间: 2013-1-21 13:49
黑马张英涛 发表于 2013-1-21 13:48
好长。。。头疼。。

这长吗?niya,赶快适应日阅读万行代码的状态....:lol
作者: txl    时间: 2013-1-21 14:15
  1. public void run(){
  2.           int i=0;
  3.           while(true){                                 //这里应该为true,修改后就可以运行了,但你的逻辑有问题,运行后有异常出现,慢慢梳理下思路吧
  4.                       if(i==0)
复制代码

作者: 郭嘉    时间: 2013-1-21 14:20
flag标记量开始就是false了,两个线程的run都没跑起来,所以当然不显示了~~~ps:以后这么大段的代码最好写上注释之类的,这样也好debug~~
作者: jonn    时间: 2013-1-21 14:22
黑马唐贤来 发表于 2013-1-21 14:15

这小玩意bug被唐哥看出来了,感谢嗯.....:lol
作者: jonn    时间: 2013-1-21 14:24
郭嘉 发表于 2013-1-21 14:20
flag标记量开始就是false了,两个线程的run都没跑起来,所以当然不显示了~~~ps:以后这么大段的代码最好写 ...

buddy,Thanks...




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2