siema,
W mojej aplikacji jest przykladowo 500 uruchomionych watkow i one zliczaja ilosc wystapien slowa w roznych plikach. W pewnym momencie ktorys z nich sie zawiesza i nie wiem jak go zabic, albo cos, bo przez to nie moge dokonczyc obliczen. Czy mozna gdzies ustawic, ze jesli watek nie zakonczy sie w jakims czasie, to zeby sam sie skonczyl?
Wkleje kod, zeby mozna rzucic okiem:
public class BlockingQueue2 {
public static void main(String[] args) throws InterruptedException
{
Scanner in = new Scanner(System.in);
System.out.print("Podaj adres strony: ");
String directory = in.nextLine();
System.out.print("Podaj słowo kluczowe: ");
String keyword = in.nextLine();
System.out.print("Podaj głębokość szukania: ");
String depthString = in.nextLine();
int depth = Integer.parseInt(depthString);
final int FILE_QUEUE_SIZE = 10000;
final int SEARCH_THREADS = 500;
BlockingQueue<URL> queue = new ArrayBlockingQueue<URL>(FILE_QUEUE_SIZE);
AtomicInteger sum = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(queue.size());
for (int i = 1; i <= SEARCH_THREADS; i++)
new Thread(new SearchTask(queue, keyword, sum, latch)).start();
try {
// Important: waits for all workers to finish.
latch.await();
} catch (InterruptedException e) {
}
System.out.println(" Sum: " + sum.get());
}
}
class SearchTask implements Runnable
{
public SearchTask(BlockingQueue<URL> queue, String keyword, AtomicInteger sum, CountDownLatch latch) throws InterruptedException
{
this.queue = queue;
this.keyword = keyword;
this.sum = sum;
this.latch = latch;
}
public void run()
{
while (!queue.isEmpty())
{
System.out.println(queue.size());
URL url = queue.poll();
search(url);
}
}
public void search(URL url)
{
try {
out = new BufferedWriter(new FileWriter("content" + Thread.currentThread().getId() + ".xml"));
buffReader = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = buffReader.readLine()) != null) {
out.write(inputLine + '\n');
}
out.close();
charArray = keyword.toCharArray();
fstream = new FileInputStream("content" + Thread.currentThread().getId() + ".xml");
in = new DataInputStream(fstream);
while ((n = in.read()) != -1) {
c = (char)n;
if (c == charArray[place]) {
place++;
if (place == charArray.length) {
place = 0;
countWords++;
}
}
else place = 0;
}
sum.addAndGet(countWords);
// This runnable is finished, so count down
latch.countDown();
in.close();
file = new File("content" + Thread.currentThread().getId() + ".xml");
file.delete();
} catch (IOException e) {
}
}
private final AtomicInteger sum;
private final CountDownLatch latch;
private FileInputStream fstream;
private int n = 0;
private char c;
private DataInputStream in;
private int place = 0;
private BufferedWriter out;
private BufferedReader buffReader;
private String inputLine;
private char[] charArray;
private BlockingQueue<URL> queue;
private String keyword;
private File file;
private int countWords = 0;
}
czesc kodu usunalem, bo dzialala dobrze, a nie byla potrzebna