我有我的線程的啓動方法有問題,我不明白了一切......主題的ExecutorService
我告訴你的代碼:
public class ThreadAction extends Thread{
@Override
public void run() {
ActionFactory factory = new ActionFactory();
IAction action;
for (int i = 0; i < list.size(); i++) {
action = factory.getIAction(list.get(i));
action.setFile(file);
try {
// Creates a random access file stream to read from, and
// optionally to write to
channel = new RandomAccessFile(file, "r").getChannel();
// We put a lock on the file
lock = channel.tryLock(0, file.length(), true);
// after the file has been locked, we can send it
action.send();
// after the file has been sent, we move it in a temporary
// repository specified in the configuration file
lock.release();
channel.close();
Path location = Paths.get(file.getPath());
Path destination = Paths.get(temp);
Files.move(location, destination);
} catch (IOException e) {
logger.error("message", e);
// e.printStackTrace();
} catch (OverlappingFileLockException e) {
logger.error("message", e);
e.printStackTrace();
} catch (SendException e) {
try {
lock.release();
channel.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
logger.error("message", e1);
// e1.printStackTrace();
}
}
}
}
}
而且我用我的主題這裏有一個thread.start()但我想使用executorService來限制我的線程數,但是當我嘗試使用它時什麼也沒有發生!
void init() {
for (Directory dir : configuration.directoriesList) {
list(dir);
}
}
void list(Directory dir) {
File directory = new File(dir.path);
File[] fList = directory.listFiles();
ExecutorService executor = Executors.newFixedThreadPool(8);
if (fList != null) {
for (File f : fList) {
if (f.isFile()) {
ArrayList<IConfig> configList = getActions(f, "ENTRY_CREATE", getDirectoriesList(f), getMatchList(f, getDirectoriesList(f)));
// implement new thread with the good parameters
threadAction = new ThreadAction();
threadAction.setList(configList);
threadAction.setEvent("ENTRY_CREATE");
threadAction.setFile(f);
threadAction.setTemp(temp + "//" + f.getName());
threadAction.start();
} else if (f.isDirectory()) {
list(new Directory(f.getAbsolutePath(), true));
}
}
}
}
如果您對爲什麼沒有任何反應有任何想法...我認爲這是因爲我現在不使用啓動方法?
它似乎並不像你正在使用你的'ExecutorService'可言。 –