2016-12-30 103 views
1

目标是创建搜索方法,该方法返回首先在所有搜索线程中找到的针的索引。当其中一个完成时,我需要停止所有线程。当其中一个完成时停止所有线程

逻辑是:有4个线程。第一个线程首先检查干草堆的%25,第二个线程检查干草堆的%25-%50等等。

只要其中一人打印文字,我就应该停下来,但我总是得到4个输出,因为他们中的4人都发现了大海捞针。但是,我只需要一个输出。

实施例输出:(下面索引)

I found, it is: 622 
I found, it is: 4072 
I found, it is: 7519 
I found, it is: 7264 

这里是SearcherThreat类的扩展Thread

public class SearcherThread extends Thread { 

// PROPERTIES 
private int needle; 
private int[] haystack; 
private int start, end; 

// CONSTRUCTOR 
public SearcherThread(int needle, int[] haystack, int start, int end) { 
    this.needle = needle; 
    this.haystack = haystack; 
    this.start = start; 
    this.end = end; 
} 

@Override 
public void run() { 
    for (int i = start; i < end && !isInterrupted(); ++i) { 
     if (haystack[i] == needle) { 
      System.out.println("I found, it is: " + i); 
      for (SearcherThread searcher : InterruptTest.searchers) { 
       searcher.interrupt(); 
      } 

     } 
    } 
} 
} 

这是包含主类和线程

import java.util.ArrayList; 
public class InterruptTest { 

public static ArrayList<SearcherThread> searchers = new ArrayList<SearcherThread>(); 

public static void main(String[] args) throws InterruptedException { 

    int itemCount = 10000; 
    int[] haystack = new int[itemCount]; 
    int domainSize = 1000; 
    for (int i = 0; i < itemCount; ++i) 
     haystack[i] = (int) (Math.random() * domainSize); 
    int needle = 10; 

    int numThreads = 4; 
    int numItemsPerThread = haystack.length/numThreads; 
    int extraItems = haystack.length - numItemsPerThread * numThreads; 
    for (int i = 0, start = 0; i < numThreads; ++i) { 
     int numItems = (i < extraItems) ? (numItemsPerThread + 1) : numItemsPerThread; 
     searchers.add(new SearcherThread(needle, haystack, start, start + numItems)); 
     start += numItems; 
    } 

    for (SearcherThread searcher : searchers) 
     searcher.start(); 
} 
} 
+0

我这样做,但为什么我得到4输出 –

+0

因为您的工作线程不检查,看他们是否中断。 –

+0

我可能在这里找到了答案http://stackoverflow.com/questions/41389714/threading-search-for-a-value-and-stop-all-threads –

回答

4

我得到了这个输出:

[[email protected] tmp]$ java InterruptTest 
I found, it is: 855 
I found, it is: 3051 
[[email protected] tmp]$ java InterruptTest 
I found, it is: 2875 
I found, it is: 5008 
I found, it is: 1081 
I found, it is: 8527 
[[email protected] tmp]$ java InterruptTest 
I found, it is: 2653 
I found, it is: 5377 
I found, it is: 1092 
[[email protected] tmp]$ java InterruptTest 
I found, it is: 255 
I found, it is: 9095 
I found, it is: 6983 
I found, it is: 3777 

正如您所看到的,完成的线程数从一次运行到下一次不等。

我们这里有一场比赛。可能发生的情况是,一个线程在启动之前完成并中断其他线程。所以他们看不到中断。 javadoc说:

“中断不活动的线程不需要任何效果。”

另一种可能性是中断传播速度不够快。请注意,javadoc没有说中断的线程立即可见interrupt()

我想不出一个解决方案,这并不否定多线程的好处。另一方面,在现实世界的用例中:

  • 您应该使用线程池......因为线程创建相对较贵。
  • 线程应该做更多的工作。

如果您测量了您在当前测试中获得的实际加速比,那么可能是否定


综上所述,在更真实的测试,你应该看到中断工作大部分时间。这应该足够好。 (应该没有关系,偶尔线程不会中断得足以阻止他们发现二级结果。)

+0

+1此外,如果在发送中断后打印消息并在线程中断时打印消息,则会看到多次线程由于种族条件,其他时间会中断,要么他们发现针速过快,要么中断不是立即发生。可能是 – Gray

+0

。但是,添加跟踪打印时需要小心一点。他们可以改变多线程代码的行为 –

相关问题