解决方案 »

  1.   

    用LinkedBlockingQueue 来做容器就好了,自带锁。
      

  2.   

    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;public class Demo5 {
    private static List<People> peoples = new ArrayList<People>(); public static void main(String[] args) {
    for (int i = 0; i < 18; i++) {
    peoples.add(new People("#" + i + "people"));
    } Demo5 demo = new Demo5();
    ExecutorService exec = Executors.newCachedThreadPool();
    exec.execute(demo.new ViewPeople());
    exec.execute(demo.new ViewPeople());
    exec.shutdown();
    } class ViewPeople implements Runnable { public void run() {
    try {
    while (!Thread.interrupted()) {
    synchronized (peoples) {
    if (peoples.size() > 0) {
    People people = peoples.get(0);
    System.out.println(people);
    peoples.remove(people);
    } else {
    break;
    }
    }
    }
    } catch (Exception e) {
    }
    } }
    }class People {
    private static int count;
    private final int id = count++;
    private String name; public People(String name) {
    this.name = name;
    } public int getId() {
    return id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } @Override
    public String toString() {
    return id + ": " + name;
    }}
      

  3.   

    1、哪个方法里访问list就加锁,访问完移除对象,线程跳出run,线程消亡。
    2、不要用arraylist,改用线程安全的队列。
      

  4.   


    这样每次只有一个线程在访问List,我需要2个线程同时可以访问List
      

  5.   


    这样每次只有一个线程在访问List,我需要2个线程同时可以访问List 
      

  6.   


    这样每次只有一个线程在访问List,我需要2个线程同时可以访问List 
    一个list对象只有一个对象锁, 怎么同时访问, 除非不同步控制
      

  7.   

    孩子,同步的概念就是宏观同步,微观不同步,用Victor 就可以了
      

  8.   

    你是想让B知道A是否在使用P吗?
    那就用lock。
    A使用P时尝试获取lock,成功继续操作。
    B尝试获取lock,失败则P正在被使用。