在一个多线程的程序里面 有一个 Collection list = new LinkedList();在一个方法里面会调用 public void methodOne{Iterator it = list.iterator();while(it.hasNext()){
  XX x = (XX)it.next();
  //do something
 }
}另外的方法会使用public void methodTwo{synchronized(list){
     list.add(sth);
}}我想问一下 除了把methodOne 函数或者函数内容加上 synchronized eg: 
1.public synchronized void methodOne(){
...
}2.public void methodOne(){Iterator it = list.iterator();synchronzied(list){
while(it.hasNext()){
... ...
}}这两种方法来同步list 还有没有其他的方法来同步iterator了? 
Colletions.synchronizedColleciton(xxx) 方法返回的Object 也不能同步iterator 必须要自己来保证.我认为
synchronzied(list){
while(it.hasNext()){
... ...
}
会太影响性能了 因为有很多连接都在不停的查询请问大家有没有好一点的 同步iterator的方法 Thx.  :)

解决方案 »

  1.   

    记得好象是ArrayList它本身就是多线程安全的
    那是不是就不用烤炉同步的问题了
      

  2.   

    to ermao0709ArrayList不是线程安全的阿 在API里写了是 Note that this implementation is not synchronized.它的源码是这样的 
    eg:
    public Object getFirst() {
    if (size==0)
        throw new NoSuchElementException(); return header.next.element;
    }public Object removeLast() {
    Object last = header.previous.element;
    remove(header.previous);
    return last;
    }不安全,你说的线程安全是不是再说Vector阿 我用的是1.4.08的SDK 还不是安全的 :(
      

  3.   

    有人能解释一下synchronized吗,知道是同步,不过没什么概念
      

  4.   

    对于同步得实现也就只有这两种方法能够实现了,应该不会再有别得方法了至于你说得这会很影响性能那是必然得,因为你想要实现线程得安全那就必须得付出这样得代价了对于有位仁兄所说得ArrayList本身就是线程安全得这是不对得,因为Vector才是线程安全得它本身才被实现了同步
      

  5.   

    还是想知道synchronize的具体含义,没什么概念
      

  6.   

    synchronized 是同步的意思 没概念去看书阿