試圖編寫一個多線程的Java程序,但遇到一些問題,我的主要多線程類可以正常工作,但如果我從main調用它啓動所有線程並移動到下一個函數,我需要它不動,直到完成所有線程。讀被傳遞文件路徑讀取(字符串,字符串)Java多線程等待線程完成
Thread one = new Thread(new Runnable() {
public void run()
{
System.out.println("Starting thread 1");
read(expiredOneYear, master1);
System.out.println("Finished thread 1");
}
});
Thread two = new Thread(new Runnable() {
public void run()
{
System.out.println("Starting thread 2");
read(expiredOneAndQuarterYear, master2);
System.out.println("Finished thread 2");
}
});
Thread three = new Thread(new Runnable() {
public void run()
{
System.out.println("Starting thread 3");
read(expiredOneAndHalfYear , master3);
System.out.println("Finished thread 3");
}
});
Thread four = new Thread(new Runnable() {
public void run()
{
System.out.println("Starting thread 4");
read(expiredOneAnd3QuarterYear , master4);
System.out.println("Finished thread 4");
}
});
// start threads
one.start();
two.start();
three.start();
four.start();
下面
是主要
CSVcompare.run(threadCount, mode, fileLocation);
CSVpattern.run(fileLocation);
發生什麼事,我不想CSVpattern.run()開始在CSVcompare.run直到所有線程()已完成,否則將不會有某些數據準備好用於CSVpattern.run()
你真的需要** ** CountDownLatch這裏。您的方案完全符合coundownlatch的使用。 – vijayraj34