Java 线程执行器服务

Java 线程执行器服务,java,threadpool,executorservice,Java,Threadpool,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.ge

我的线程的开始方法有问题,我不明白所有的

我向您展示代码:

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();
            }
        }
    }
}

}
public类ThreadAction扩展线程{
@凌驾
公开募捐{
ActionFactory=新的ActionFactory();
镇痛作用;
对于(int i=0;i
我在这里使用的线程是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));
            }
        }
    }

}
void init(){
for(目录目录目录:configuration.directoriesList){
名单(dir);
}
}
无效列表(目录目录目录){
文件目录=新文件(dir.path);
File[]fList=directory.listFiles();
ExecutorService executor=Executors.newFixedThreadPool(8);
如果(fList!=null){
for(文件f:fList){
if(f.isFile()){
ArrayList configList=getActions(f,“条目创建”、getDirectoriesList(f)、getMatchList(f、getDirectoriesList(f));
//使用良好的参数实现新线程
threadAction=新的threadAction();
设置列表(配置列表);
setEvent(“ENTRY_CREATE”);
threadAction.setFile(f);
setamp(temp+“/”+f.getName());
threadAction.start();
}else if(f.isDirectory()){
列表(新目录(f.getAbsolutePath(),true));
}
}
}
}

如果你知道为什么什么都没发生。。。我想这是因为我现在不使用start方法?

如果您想迁移到
ExecutorService
,您必须至少更改两件事:

  • 更改
    ThreadAction
    以实现
    Runnable
    而不是扩展
    Thread
  • 初始化操作后,将
    threadAction
    提交到
    ExecutorService
    实例:

    executor.submit(threadAction);
    

  • 提交threadAction任务后,需要使用executor.shutdown()关闭executor服务。这是为了确保线程不会保持运行


    您创建了一个大小为8的线程池,但只提交了一个任务。要么将ExecutorService更改为Executors.newSingleThreadExecutor(),要么在循环中向ExecutorService提交更多threadAction实例。

    首先,您必须了解ExecutorService的角色。ExecutorService实际上运行您的线程,您不必对已创建的线程调用start方法。在上面的代码中,您已经创建了线程,但从未将其提交给ExecutorService。以下示例将帮助您理解:

    **

    导入java.util.concurrent.ExecutorService;
    导入java.util.concurrent.Executors;
    公共类执行器服务检查{
    公共静态整数和(int j){
    int结果=0;
    
    对于(int i=0;iIt看起来你根本没有使用你的
    ExecutorService
    。谢谢!我更改了我的类以实现Runnable,并且我提交了,但是什么都没有发生……为什么你认为什么都没有发生?你如何检查你的操作是否运行?通常已经发送了一个文件。你可以向
    run
    方法添加记录器调用以确保e没有被处决?
    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));
            }
        }