Reputation: 147
I have a problem with the start method of my thread, I don't understand everything...
I show you the code:
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();
}
}
}
}
}
And I'm using my Thread here with a thread.start() but i would like to use executorService to limit my number of thread but when i try to use it nothing happens!
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));
}
}
}
}
If you have any idea about why nothing happens... I think it's because i don't use start method now ?
Upvotes: 1
Views: 274
Reputation: 3
First thing, you have to understand the role of ExecutorService. ExecutorService actually runs your thread and you need not have to call start method on the thread that you have created. In above code you have created thread but never submitted it to ExecutorService. Following example will help you to understand:
**
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ExecutorServiceCheck {
public static Integer sum(int j) {
int result=0;
for(int i =0; i<10; i++) {
result= result+i;
}
return result;
}
public static void main(String[] args) {
final ExecutorServiceCheck obj = new ExecutorServiceCheck();
Thread t1 = new Thread(new AddHelper());
ExecutorService service = Executors.newFixedThreadPool(8);
service.submit(t1);
}
}
class AddHelper implements Runnable {
public void run() {
System.out.println(ExecutorServiceCheck.sum(13));
}
}
**
Upvotes: 0
Reputation: 21
After submitting the threadAction task you need to shutdown the ExecutorService using executor.shutdown(). This is to ensure that threads do not keep running.
You created a threadpool of size 8 but you are only submitting one task. Either you change the ExecutorService to Executors.newSingleThreadExecutor() or you submit more instances of the threadAction to the ExecutorService in a loop.
Upvotes: 2
Reputation: 10143
If you want to migrate to ExecutorService
you must change at least two things:
ThreadAction
to implement Runnable
instead of extending Thread
Submit threadAction
to ExecutorService
instance once action is initialized:
executor.submit(threadAction);
Upvotes: 1