这段代码线程安全吗?

Is this piece of code thread safe?

private static final Word2Vec word2vectors = getWordVector();

    private static Word2Vec getWordVector() {
        String PATH;
        try {
            PATH = new ClassPathResource("models/word2vec_model").getFile().getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        log.warn("Loading model...");
        return WordVectorSerializer.readWord2VecModel(new File(PATH));
    }

        ExecutorService pools = Executors.newFixedThreadPool(4);
        long startTime = System.currentTimeMillis();
        List<Future<?>> runnables = new ArrayList<>();
        if (word2vectors != null) {
            for (int i = 0; i < 3000; i++) {
                MyRunnable runnable = new MyRunnable("beautiful", i);
                runnables.add(pools.submit(runnable));
            }
        }
        for(Future<?> task: runnables){
            try {
                task.get();
            }catch(InterruptedException ie){
                ie.printStackTrace();
            }catch(ExecutionException ee){
                ee.printStackTrace();
            }
        }
        pools.shutdown();

static class MyRunnable implements Runnable{
        private String word;
        private int count;
        public MyRunnable(String word, int i){
            this.word = word;
            this.count = i;
        }

        @Override
        public void run() {
                Collection<String> words = word2vectors.wordsNearest(word, 5);
                log.info("Top 5 cloest words: " + words);
                log.info(String.valueOf(count));
        }
    }

word2vectors.wordsNearest() 是 public 库中的一种方法。我打算让4个线程并发执行该方法来加快进程。这个线程安全吗?

如果满足以下条件,您的代码片段将是线程安全的:

  1. word2vectors.wordsNearest(...) 调用是线程安全的
  2. word2vectors数据结构由当前线程创建并初始化。
  3. 在 pools.execute 调用和计算完成之间没有任何数据结构改变。

如果wordsNearest不看其他数据结构,不改变word2vectors数据结构,那么是合理的假设 它是线程安全的。然而,唯一可以确定的方法是分析它

但同样值得注意的是,您只向执行者提交了一个任务。由于每个任务都是由单个线程 运行 完成的,因此您的代码实际上只使用一个线程。要利用多线程,您需要将单个大任务拆分为多个独立的小任务;例如将 10,000 次重复循环放在执行调用之外...