java1.7 linkedlist 线程安全问题

本文详细探讨了Java中LinkedList的线程安全性问题,指出直接使用add()方法进行多线程操作时不会抛出ConcurrentModificationException异常,但在使用ListIterator时则会触发该异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天看了一下网上说linkedlist是线程不安全的 ,多线程下会抛出java.util.ConcurrentModificationException异常

其实不对 ,linkedlist线程不安全不假 ,但是并不会抛出异常

源码: add()方法

/**
 * Appends the specified element to the end of this list.
 *
 * <p>This method is equivalent to {@link #addLast}.
 *
 * @param e element to be appended to this list
 * @return {@code true} (as specified by {@link Collection#add})
 */
public boolean add(E e) {
    linkLast(e);
    return true;
}
/**
 * Links e as last element.
 */
void linkLast(E e) {
    final Node<E> l = last;
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
    if (l == null)
        first = newNode;
    else
        l.next = newNode;
    size++;
    modCount++;
}

其实是没有任何抛出异常的方法


而在其内部类ListIterator中

public void add(E e) {
    checkForComodification();
    lastReturned = null;
    if (next == null)
        linkLast(e);
    else
        linkBefore(e, next);
    nextIndex++;
    expectedModCount++;
}
才有校验
final void checkForComodification() {
    if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

所以多线程下正常的add()操作并不会抛出异常, 而使用ListIterator才会抛出异常


测试代码 : 使用ListIterator

public class Test1 {

    private final LinkedList<Integer> list = new LinkedList<Integer>();

    public Test1() {
        Thread add1 = new Thread(new Runnable() {

            @Override
            public void run() {
                ListIterator<Integer> integerListIterator = list.listIterator();
                int number = 0;
                while (true) {
                    if (number == 10)
                        number = 0;
                    // System.out.println("Writing " + number);
                    integerListIterator.add(number);
                    number++;
                    System.out.flush();
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread add2 = new Thread(new Runnable() {

            @Override
            public void run() {
                ListIterator<Integer> integerListIterator = list.listIterator();
                int number = 100;
                while (true) {
                    if (number > 119) {
                        number = 100;
                    }
                    integerListIterator.add(number);
                    number++;
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

        Executors.newCachedThreadPool().execute(add1);
        Executors.newCachedThreadPool().execute(add2);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Test1 test = new Test1();
    }
}
Exception in thread "pool-1-thread-1" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953)
at java.util.LinkedList$ListItr.add(LinkedList.java:941)
at main.Test1$1.run(Test1.java:29)
at java.lang.Thread.run(Thread.java:745)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:745)

正常add():

public class Test1 {

    private final LinkedList<Integer> list = new LinkedList<Integer>();

    public Test1() {
        Thread add1 = new Thread(new Runnable() {

            @Override
            public void run() {
                int number = 0;
                while (true) {
                    if (number == 10)
                        number = 0;
                    // System.out.println("Writing " + number);
                    list.add(number);
                    number++;
                    System.out.flush();
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread add2 = new Thread(new Runnable() {

            @Override
            public void run() {
                int number = 100;
                while (true) {
                    if (number > 119) {
                        number = 100;
                    }
                    list.add(number);
                    number++;
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

        Executors.newCachedThreadPool().execute(add1);
        Executors.newCachedThreadPool().execute(add2);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Test1 test = new Test1();
    }
}
没有抛出异常


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值