package com.agentz.test.filescanner.core;
import com.agentz.test.filescanner.util.FileNameValidator;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class ScannerThread implements Callable<ArrayList><File>> {
private ThreadCountController controller;
private ArrayList<File> fileList;
private String path;
private String mask;
public ScannerThread(ThreadCountController controller, String path, String mask) {
this.fileList = new ArrayList<File>();
this.controller = controller;
this.path = path;
this.mask = mask;
}
private void scan() {
ArrayList<Future><ArrayList><File>>> futureArrayList = new ArrayList<Future><ArrayList><File>>>();
File[] files = new File(path).listFiles();
if (files != null) {
for (File file : files) {
if (!Thread.currentThread().isInterrupted()) {
if (file.isDirectory()) {
path = file.getAbsolutePath();
if (!controller.addScannerThread(path, futureArrayList)) {
scan();
}
} else {
fileList.add(file);
}
}
}
for (Future<ArrayList><File>> future : futureArrayList) {
try {
fileList.addAll(future.get());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
Thread.currentThread().interrupt();
}
}
}
}
public ArrayList<File> call() {
scan();
return fileList;
}
}
колличество потоков в пуле.
Класс сканер:
Код:
Для контроля за количеством потоков написан класс-контроллер:
Код:
package com.agentz.test.filescanner.core;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadCountController {
private ExecutorService threadPool;
private int threadCount;
private String mask;
public ThreadCountController(ExecutorService threadPool, int threadCount, String mask) {
this.threadPool = threadPool;
this.threadCount = threadCount;
this.mask = mask;
}
public ExecutorService getThreadPool() {
return threadPool;
}
public void setThreadPool(ExecutorService threadPool) {
this.threadPool = threadPool;
}
public int getThreadCount() {
return threadCount;
}
public void setThreadCount(int threadCount) {
this.threadCount = threadCount;
}
synchronized public boolean addScannerThread(String path, ArrayList<Future><ArrayList><File>>> futureList) {
System.out.println(((ThreadPoolExecutor) threadPool).getCorePoolSize());
if (((ThreadPoolExecutor) threadPool).getActiveCount() < threadCount) {
ScannerThread thread = new ScannerThread(this, path, mask);
Future<ArrayList><File>> future = threadPool.submit(thread);
futureList.add(future);
return true;
} else {
return false;
}
}
}
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
public class ThreadCountController {
private ExecutorService threadPool;
private int threadCount;
private String mask;
public ThreadCountController(ExecutorService threadPool, int threadCount, String mask) {
this.threadPool = threadPool;
this.threadCount = threadCount;
this.mask = mask;
}
public ExecutorService getThreadPool() {
return threadPool;
}
public void setThreadPool(ExecutorService threadPool) {
this.threadPool = threadPool;
}
public int getThreadCount() {
return threadCount;
}
public void setThreadCount(int threadCount) {
this.threadCount = threadCount;
}
synchronized public boolean addScannerThread(String path, ArrayList<Future><ArrayList><File>>> futureList) {
System.out.println(((ThreadPoolExecutor) threadPool).getCorePoolSize());
if (((ThreadPoolExecutor) threadPool).getActiveCount() < threadCount) {
ScannerThread thread = new ScannerThread(this, path, mask);
Future<ArrayList><File>> future = threadPool.submit(thread);
futureList.add(future);
return true;
} else {
return false;
}
}
}
Но вот точно определить количество работающих потоков не знаю как(вернее наложить работающее ограничение на количество).
Подскажите пожалуйста, что изменить.
Заранее спасибо.
В чем собственно проблема?
Не хочется копировать, запускать и тестировать ваш код чтобы найти в нем потенциальные баги, может выразитесь точней?
Цитата: Zorkus
А в чем проблема? Что именно не получается? Счетчик количества потоков есть, проверка есть при попытке добавить еще один поток сканирования.
В чем собственно проблема?
Не хочется копировать, запускать и тестировать ваш код чтобы найти в нем потенциальные баги, может выразитесь точней?
В чем собственно проблема?
Не хочется копировать, запускать и тестировать ваш код чтобы найти в нем потенциальные баги, может выразитесь точней?
Когда дописыаваю строку
Код:
System.out.println(Thread.currentThread().getName());
в метод scan() в классе ScannerThread, то в консоли получаю
Код:
pool-1-thread-1
pool-1-thread-2
pool-1-thread-3
pool-1-thread-4
pool-1-thread-5
pool-1-thread-6
pool-1-thread-7
pool-1-thread-8
pool-1-thread-9
pool-1-thread-10
pool-1-thread-9
pool-1-thread-11
pool-1-thread-2
pool-1-thread-3
pool-1-thread-11
pool-1-thread-6
pool-1-thread-4
pool-1-thread-4
pool-1-thread-7
pool-1-thread-4
pool-1-thread-2
pool-1-thread-7
pool-1-thread-8
pool-1-thread-11
pool-1-thread-4
pool-1-thread-7
pool-1-thread-9
pool-1-thread-7
pool-1-thread-8
pool-1-thread-11
pool-1-thread-2
pool-1-thread-10
pool-1-thread-5
pool-1-thread-9
pool-1-thread-9
pool-1-thread-7
pool-1-thread-10
pool-1-thread-9
pool-1-thread-7
pool-1-thread-10
pool-1-thread-10
pool-1-thread-6
pool-1-thread-4
pool-1-thread-2
pool-1-thread-2
pool-1-thread-5
pool-1-thread-5
pool-1-thread-11
pool-1-thread-8
pool-1-thread-3
pool-1-thread-11
pool-1-thread-11
pool-1-thread-11
pool-1-thread-11
pool-1-thread-4
pool-1-thread-6
pool-1-thread-11
pool-1-thread-9
pool-1-thread-4
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-7
pool-1-thread-4
pool-1-thread-2
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-9
pool-1-thread-4
pool-1-thread-6
pool-1-thread-9
pool-1-thread-9
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-12
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-10
pool-1-thread-11
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-11
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-5
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-11
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-9
pool-1-thread-10
pool-1-thread-9
pool-1-thread-10
pool-1-thread-6
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-6
pool-1-thread-6
pool-1-thread-9
pool-1-thread-3
pool-1-thread-5
pool-1-thread-9
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-9
pool-1-thread-9
pool-1-thread-4
pool-1-thread-9
pool-1-thread-4
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-9
pool-1-thread-9
pool-1-thread-10
pool-1-thread-9
pool-1-thread-3
pool-1-thread-6
pool-1-thread-3
pool-1-thread-10
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-3
pool-1-thread-9
pool-1-thread-3
pool-1-thread-9
pool-1-thread-4
pool-1-thread-4
pool-1-thread-9
pool-1-thread-3
pool-1-thread-4
pool-1-thread-4
pool-1-thread-4
pool-1-thread-4
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-2
pool-1-thread-4
pool-1-thread-2
pool-1-thread-9
pool-1-thread-2
pool-1-thread-4
pool-1-thread-9
pool-1-thread-3
pool-1-thread-3
pool-1-thread-4
pool-1-thread-9
pool-1-thread-6
pool-1-thread-9
pool-1-thread-3
pool-1-thread-4
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-2
pool-1-thread-9
pool-1-thread-10
pool-1-thread-2
pool-1-thread-9
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-3
pool-1-thread-9
pool-1-thread-10
pool-1-thread-3
pool-1-thread-4
pool-1-thread-9
pool-1-thread-2
pool-1-thread-10
pool-1-thread-10
pool-1-thread-10
pool-1-thread-10
pool-1-thread-10
pool-1-thread-6
pool-1-thread-2
pool-1-thread-2
pool-1-thread-3
pool-1-thread-10
pool-1-thread-4
pool-1-thread-10
pool-1-thread-9
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-3
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-4
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-2
pool-1-thread-10
pool-1-thread-4
pool-1-thread-11
pool-1-thread-12
pool-1-thread-3
pool-1-thread-13
pool-1-thread-4
pool-1-thread-3
pool-1-thread-2
pool-1-thread-4
pool-1-thread-10
pool-1-thread-9
pool-1-thread-9
pool-1-thread-2
pool-1-thread-9
pool-1-thread-10
pool-1-thread-2
pool-1-thread-3
pool-1-thread-4
pool-1-thread-5
pool-1-thread-6
pool-1-thread-7
pool-1-thread-8
pool-1-thread-9
pool-1-thread-10
pool-1-thread-9
pool-1-thread-11
pool-1-thread-2
pool-1-thread-3
pool-1-thread-11
pool-1-thread-6
pool-1-thread-4
pool-1-thread-4
pool-1-thread-7
pool-1-thread-4
pool-1-thread-2
pool-1-thread-7
pool-1-thread-8
pool-1-thread-11
pool-1-thread-4
pool-1-thread-7
pool-1-thread-9
pool-1-thread-7
pool-1-thread-8
pool-1-thread-11
pool-1-thread-2
pool-1-thread-10
pool-1-thread-5
pool-1-thread-9
pool-1-thread-9
pool-1-thread-7
pool-1-thread-10
pool-1-thread-9
pool-1-thread-7
pool-1-thread-10
pool-1-thread-10
pool-1-thread-6
pool-1-thread-4
pool-1-thread-2
pool-1-thread-2
pool-1-thread-5
pool-1-thread-5
pool-1-thread-11
pool-1-thread-8
pool-1-thread-3
pool-1-thread-11
pool-1-thread-11
pool-1-thread-11
pool-1-thread-11
pool-1-thread-4
pool-1-thread-6
pool-1-thread-11
pool-1-thread-9
pool-1-thread-4
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-7
pool-1-thread-4
pool-1-thread-2
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-9
pool-1-thread-4
pool-1-thread-6
pool-1-thread-9
pool-1-thread-9
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-12
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-10
pool-1-thread-11
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-11
pool-1-thread-11
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-11
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-5
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-11
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-6
pool-1-thread-9
pool-1-thread-10
pool-1-thread-9
pool-1-thread-10
pool-1-thread-6
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-5
pool-1-thread-6
pool-1-thread-6
pool-1-thread-9
pool-1-thread-3
pool-1-thread-5
pool-1-thread-9
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-9
pool-1-thread-9
pool-1-thread-4
pool-1-thread-9
pool-1-thread-4
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-9
pool-1-thread-9
pool-1-thread-10
pool-1-thread-9
pool-1-thread-3
pool-1-thread-6
pool-1-thread-3
pool-1-thread-10
pool-1-thread-9
pool-1-thread-10
pool-1-thread-10
pool-1-thread-3
pool-1-thread-9
pool-1-thread-3
pool-1-thread-9
pool-1-thread-4
pool-1-thread-4
pool-1-thread-9
pool-1-thread-3
pool-1-thread-4
pool-1-thread-4
pool-1-thread-4
pool-1-thread-4
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-2
pool-1-thread-4
pool-1-thread-2
pool-1-thread-9
pool-1-thread-2
pool-1-thread-4
pool-1-thread-9
pool-1-thread-3
pool-1-thread-3
pool-1-thread-4
pool-1-thread-9
pool-1-thread-6
pool-1-thread-9
pool-1-thread-3
pool-1-thread-4
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-9
pool-1-thread-2
pool-1-thread-9
pool-1-thread-10
pool-1-thread-2
pool-1-thread-9
pool-1-thread-2
pool-1-thread-9
pool-1-thread-9
pool-1-thread-3
pool-1-thread-9
pool-1-thread-10
pool-1-thread-3
pool-1-thread-4
pool-1-thread-9
pool-1-thread-2
pool-1-thread-10
pool-1-thread-10
pool-1-thread-10
pool-1-thread-10
pool-1-thread-10
pool-1-thread-6
pool-1-thread-2
pool-1-thread-2
pool-1-thread-3
pool-1-thread-10
pool-1-thread-4
pool-1-thread-10
pool-1-thread-9
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-3
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-4
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-2
pool-1-thread-9
pool-1-thread-2
pool-1-thread-10
pool-1-thread-4
pool-1-thread-11
pool-1-thread-12
pool-1-thread-3
pool-1-thread-13
pool-1-thread-4
pool-1-thread-3
pool-1-thread-2
pool-1-thread-4
pool-1-thread-10
pool-1-thread-9
pool-1-thread-9
pool-1-thread-2
pool-1-thread-9
pool-1-thread-10
Есть потоки 12 и 13 при ограничении на 10(считается с 1 походу), ну и т.д. Колличество может быть больше.
Интерестно почему?
Цитата:
Класс сканер...
Для контроля за количеством потоков написан класс-контроллер:
Для контроля за количеством потоков написан класс-контроллер:
Повторю вопросс Zorkus'а вторично.... Ну и в чем проблема???
Вы не желаете анализировать код? Простите, но лично мне не хочеться выполнять все эти действия за вас. Может быть это сделает Zorkus, хотя я лично сомневаюсь что ему это нужно.
IDE, Debuger, Profiler и бог Вам в помощь. Задача довольно таки простая.