多线程Java-线程冲突/覆盖问题

时间:2016-02-21 11:32:29

标签: java multithreading dictionary arraylist threadpool

我正在使用一个简单的线程池来读取一个大字典,我已经编写了一个for循环遍历整个字典(10000字),我试图得到它,所以它将每500个存储到一个线程中分配该子字节arraylist到单个线程来处理。

当arraylist中有500个单词时,它会将其存储在“Words”类的实例中。这只是存储并允许访问arrayList(分配给该线程的arrayList)。

这似乎不起作用,因为有重复,大多数时候字典中的最后500个单词是所有线程最终使用的,我觉得很奇怪。另外我还注意到,当我在下面显示的for循环内部结尾添加一个简单的超时3秒时,它可以工作,但这似乎是一个可怕的修复,我希望这个程序尽可能高效和快速。

// Executor Program
ExecutorService executor = Executors.newFixedThreadPool(cores);
    ArrayList<String> words123 = new ArrayList<String>();
    for (int i = 0; i < dictionary.size(); i++) {
        words123.add(dictionary.get(i));
        if(words123.size() == 1000) {
            Words wordsList = new Words(words123);
            Runnable worker = new WorkerThread(wordsList, passwords, stp);
            executor.execute(worker);
            words123 = new ArrayList<String>();
        }
    }
    executor.shutdown();
    //wait for all jobs to complete
    while (!executor.isTerminated()) {
    }
    System.out.println("Finished all threads");




// WORD OBJECT ------------------
public class Words {
public static ArrayList<String> words = new ArrayList<String>();

public Words(ArrayList words) {
    this.words = words;
}

public int getSize() {
    return words.size();
}

public String getWord(int i) {
    return words.get(i);
}

}

//WORKER THREAD ----------------

    public static Words wordList;
public static int cmd;
public static HashMap<String, String> passwords = new HashMap<String, String>();
public static SimpleThreadPool stp;
/**
 * Constructor
 * @param s
 */
public WorkerThread(Words word, HashMap passwords, SimpleThreadPool stp, int cmd){
    this.wordList = word;
    //System.out.println("in  " + words);
    //Read in hashes using readFromFile method
    this.passwords = passwords;
    this.stp = stp;
    this.cmd = cmd;
}

/**
 * For a thread pool to function, ensure that the run() method terminates
 * This method prints out the command, calls a function, then prints end and terminates
 */
@Override
public void run() {
    //System.out.println(Thread.currentThread().getName()+" Start.");
    //System.out.println("WOOOO  " + wordList.getWords() + cmd);

    for(int i = 0; i < wordList.getSize(); i++){
        Password pass = new Password(wordList.getWord(i), hashPassword(wordList.getWord(i)));
        //System.out.println(pass.getOriginalPass());
        //checkHash(pass);

        // Check password with letter-number edits (e.g. a-->@)
        letterSymbolEdit(pass);

        // Check password with capital letter edits
        //capitalsEdit(pass);

        // Reverse password
        reverseEdit(pass);

        // Concatenate all words in dictionary
        //concatEdit(pass);

        printPermutations(pass);

        // All possible numbers generated and appended to string
        for(int j = 0; j < 4; j++){
            numberBuilder("", 0, j, pass);
        }
    }
    //System.out.println(Thread.currentThread().getName()+" End.");
}

1 个答案:

答案 0 :(得分:2)

问题是Words类中的'words'变量是静态的,这意味着该类的每个实例都使用相同的列表。

另外,既然你想提高效率,我会采用不同的方法。而不是

ExecutorService executor = Executors.newFixedThreadPool(cores);

使用

LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(cores, cores, 0L, TimeUnit.MILLISECONDS, workQueue);
executor.prestartAllCoreThreads();

然后将Runnable实例直接添加到workQueue。通过这种方式,您不必等待自己在线程之间划分单词:线程将在完成任务后立即获取它们。