我正在編寫一個生成1到1,000之間的隨機數的程序。然後它使用三個線程的線程池搜索範圍更廣的1到1,000範圍內的某些數字範圍。線程檢查其範圍內的每個數字,並將它與隨機目標數字進行比較,如果匹配,則消息傳遞給控制檯。如果號碼不匹配,這也會反映到控制檯的消息中。我想知道如何在達到目標數字後結束程序,並且即使目標已經找到,也不要繼續分析數字。謝謝。如何在Java中停止線程
這是FindIt類:
/** fill in later */
public class FindIt extends Thread
{
private int numToFind;
private int numToStart;
private int numToEnd;
public FindIt(int nF, int nS, int nE)
{
numToFind = nF;
numToStart = nS;
numToEnd = nE;
}
public void run()
{
int counter = 0;
int numAt = numToStart;
for (int i = 0; i < (numToEnd - numToStart) + 1; i++)
{
counter++;
if (counter == 10)
{
counter = 0;
Thread.yield();
}
if (numAt++ == numToFind)
{
System.out.println("The target number, " + numToFind + ", has been found by " + Thread.currentThread().getName() + ".");
}
else
{
System.out.println(Thread.currentThread().getName() + " has analyzed the number " + (numAt - 1) + " - not the target number.");
}
}
}
}
這是主要的方法的程序:
import java.util.Random; //imports Java's Random class
import java.util.concurrent.*;
/** fill in later */
public class NumberSearch
{
public static void main(String [] args)
{
Random r = new Random(); //creates an instance of the Random class
int randomNum = r.nextInt(1001); //declares the integer randomNum and initilizes it to be a random interger in the range 0 inclusive to 1001 exclusive
ExecutorService executor = Executors.newFixedThreadPool(3);
FindIt find1 = new FindIt(randomNum, 0, 349);
FindIt find2 = new FindIt(randomNum, 350, 699);
FindIt find3 = new FindIt(randomNum, 700, 1000);
executor.execute(find1);
executor.execute(find2);
executor.execute(find3);
executor.shutdown();
}
}
的可能的複製[?如何正確地停止在Java線程(http://stackoverflow.com/questions/10961714/how-to-properly-stop -ja-thread-in-java) –
@Ravindrababu:請注意,在該問題上接受的答案不能處理任務在線程池中運行的情況,當使用Executors時,整個手動滾動易失性標誌方法失敗。 –
好的。我撤回了我的投票。 –