首页  >  问答  >  正文

java中System.arraycopy是线程安全的吗?

请问一下 在java中System.arraycopy是不是线程安全的

巴扎黑巴扎黑2712 天前764

全部回复(3)我来回复

  • 高洛峰

    高洛峰2017-04-18 10:25:09

    System.arraycopy 是一个 native 方法:

    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

    native 方法和线程安全之间又没有什么必然联系, 并且我看 System.arraycopy 的文档中也没有提到它是线程安全的, 因此就可以知道它是 线程不安全的.

    不过为了验证 System.arraycopy 是否真的是线程不安全的, 我写了一个小例子:

    public class ArrayCopyThreadSafe {
        private static int[] arrayOriginal = new int[1024 * 1024 * 10];
        private static int[] arraySrc = new int[1024 * 1024 * 10];
        private static int[] arrayDist = new int[1024 * 1024 * 10];
        private static ReentrantLock lock = new ReentrantLock();
    
        private static void modify() {
            for (int i = 0; i < arraySrc.length; i++) {
                arraySrc[i] = i + 1;
            }
        }
    
        private static void copy() {
            System.arraycopy(arraySrc, 0, arrayDist, 0, arraySrc.length);
        }
    
        private static void init() {
            for (int i = 0; i < arraySrc.length; i++) {
                arrayOriginal[i] = i;
                arraySrc[i] = i;
                arrayDist[i] = 0;
            }
        }
    
        private static void doThreadSafeCheck() throws Exception {
            for (int i = 0; i < 100; i++) {
                System.out.println("run count: " + (i + 1));
                init();
                Condition condition = lock.newCondition();
    
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        lock.lock();
                        condition.signalAll();
                        lock.unlock();
                        copy();
                    }
                }).start();
    
    
                lock.lock();
                // 这里使用 Condition 来保证拷贝线程先已经运行了.
                condition.await();
                lock.unlock();
    
                Thread.sleep(2); // 休眠2毫秒, 确保拷贝操作已经执行了, 才执行修改操作.
                modify();
    
                if (!Arrays.equals(arrayOriginal, arrayDist)) {
                    throw new RuntimeException("System.arraycopy is not thread safe");
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
            doThreadSafeCheck();
        }
    }

    这个例子的具体操作是:

    1. arrayOriginal 和 arraySrc 初始化时是相同的, 而 arrayDist 是全为零的.

    2. 启动一个线程运行 copy() 方法来拷贝 arraySrc 到 arrayDist 中.

    3. 在主线程执行 modify() 操作, 修改 arraySrc 的内容. 为了确保 modify() 操作, 修改 arraySrc 的内容. 为了确保 copy() 操作先于 modify() 操作先于 modify() 操作, 我使用 Condition, 并且延时了两毫秒, 以此来保证执行拷贝操作(即System.arraycopy) 先于修改操作.

    4. 根据第三点, 如果 System.arraycopy 是线程安全的, 那么先执行拷贝操作, 再执行修改操作时, 不会影响复制结果, 因此 arrayOriginal 必然等于 arrayDist; 而如果 System.arraycopy线程不安全的 是线程安全的, 那么先执行拷贝操作, 再执行修改操作时, 不会影响复制结果, 因此 arrayOriginal 必然等于 arrayDist; 而如果

      线程不安全的, 那么 arrayOriginal 不等于 arrayDist.

    根据上面的推理, 运行一下程序, 有如下输出:

    run count: 1
    run count: 2
    Exception in thread "main" java.lang.RuntimeException: System.arraycopy is not thread safe
        at com.test.ArrayCopyThreadSafe.doThreadSafeCheck(ArrayCopyThreadSafe.java:62)
        at com.test.ArrayCopyThreadSafe.main(ArrayCopyThreadSafe.java:68)

    可以看到, 前两次运行的好好的, 但是第三次就不行了.🎜

    回复
    0
  • ringa_lee

    ringa_lee2017-04-18 10:25:09

    native方法 直接copy堆中内存

    回复
    0
  • 怪我咯

    怪我咯2017-04-18 10:25:09

    当然不是啦。要加锁或互斥的

    回复
    0
  • 取消回复