繁体   English   中英

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

[英]Multithreading Java- Threads clashing/overwriting issue

我使用一个简单的线程池读取大型词典,我编写了一个for循环,遍历整个词典(10000个单词),我试图将其存储,因此它将每500个存储到一个线程中,将该子数组列表分配给一个单线程处理。

当数组列表中有500个单词时,它将存储在“单词”类的实例中。 这只是存储并允许访问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.");
}

问题在于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中。 这样,您不必等待自己在线程之间划分单词:线程将在完成任务后立即获取它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM