Java 如何使用多线向Vector集合里添加不重复的数据

范例1:

package com.contoso;

import java.util.Enumeration;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class VectorExample1 {

    public static void insertIfAbsent(Vector<Long> list, Long value) {
        synchronized (list) {
            boolean contains = list.contains(value);
            if (!contains) {
                list.add(value);
                System.out.println(Thread.currentThread().getName() + " insertIfAbsent Value added: " + value);
            }
        }
    }

    /**
     * 你可能会看到重复的值,尝试多运行几次并查看结果中的差异
     * 
     */
    public static void insertIfAbsentUnsafe(Vector<Long> list, Long value) {
        boolean contains = list.contains(value);
        if (!contains) {
            list.add(value);
            System.out.println(Thread.currentThread().getName() + " insertIfAbsentUnsafe Value added: " + value);
        }
    }

    public static void printVector(String name, Vector<Long> v) {
        Enumeration<Long> elements = v.elements();
        while (elements.hasMoreElements()) {
            System.out.println(name + " " + elements.nextElement());
        }
    }

    public static void insertUnsafe() {
        ExecutorService executor = Executors.newCachedThreadPool();
        // Synchronized - Vector
        Vector vector = new Vector<Long>();

        Runnable insertIfAbsentUnsafe = () -> {
            long millis = System.currentTimeMillis() / 1000;
            insertIfAbsentUnsafe(vector, millis);
        };
        for (int i = 0; i < 1000000; i++) {
            executor.execute(insertIfAbsentUnsafe);
        }
        executor.shutdown();
        printVector("insertIfAbsentUnsafe", vector);
    }

    public static void insertSafe() {
        ExecutorService executor = Executors.newCachedThreadPool();
        // Synchronized - Vector
        Vector vector = new Vector<Long>();

        Runnable insertIfAbsent = () -> {
            long millis = System.currentTimeMillis() / 1000;
            insertIfAbsent(vector, millis);
        };
        for (int i = 0; i < 1000000; i++) {
            executor.execute(insertIfAbsent);
        }
        executor.shutdown();
        printVector("insertIfAbsent", vector);
    }

    public static void main(String[] args) throws InterruptedException {
        insertSafe();
        insertUnsafe();
    }
}
run:
pool-1-thread-1 insertIfAbsent Value added: 1571847216
pool-1-thread-209 insertIfAbsent Value added: 1571847217
pool-1-thread-1535 insertIfAbsent Value added: 1571847218
insertIfAbsent 1571847216
insertIfAbsent 1571847217
insertIfAbsent 1571847218
pool-2-thread-1 insertIfAbsentUnsafe Value added: 1571847218
pool-2-thread-656 insertIfAbsentUnsafe Value added: 1571847219
pool-2-thread-657 insertIfAbsentUnsafe Value added: 1571847219
pool-2-thread-654 insertIfAbsentUnsafe Value added: 1571847219
pool-2-thread-782 insertIfAbsentUnsafe Value added: 1571847220
pool-2-thread-682 insertIfAbsentUnsafe Value added: 1571847220
pool-2-thread-778 insertIfAbsentUnsafe Value added: 1571847220
pool-2-thread-781 insertIfAbsentUnsafe Value added: 1571847220
pool-2-thread-680 insertIfAbsentUnsafe Value added: 1571847220
insertIfAbsentUnsafe 1571847218
insertIfAbsentUnsafe 1571847219
insertIfAbsentUnsafe 1571847219
insertIfAbsentUnsafe 1571847219
insertIfAbsentUnsafe 1571847220
insertIfAbsentUnsafe 1571847220
insertIfAbsentUnsafe 1571847220
insertIfAbsentUnsafe 1571847220
insertIfAbsentUnsafe 1571847220
BUILD SUCCESSFUL (total time: 4 seconds)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值