2014-01-29 44 views
1

我有我的線程的啓動方法有問題,我不明白了一切......主題的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)); 
      } 
     } 
    } 

} 

如果您對爲什麼沒有任何反應有任何想法...我認爲這是因爲我現在不使用啓動方法?

+1

它似乎並不像你正在使用你的'ExecutorService'可言。 –

回答

1

如果要遷移到ExecutorService必須更改至少兩件事情:

  1. 變化ThreadAction來實現的,而不是延長Thread
  2. 提交threadActionExecutorService情況下,一旦動作被初始化Runnable

    executor.submit(threadAction); 
    
+0

謝謝!我改變了我的班級來實現Runnable,並且我冒然進行,但沒有任何反應...... –

+0

你爲什麼認爲什麼都沒有發生?你如何檢查你的動作是否運行? – hoaz

+0

正常情況下,文件已發送 –

2

提交threadAction任務後,您需要使用executor.shutdown()關閉ExecutorService。這是爲了確保線程不會繼續運行。

您創建了一個大小爲8的線程池,但您只提交了一個任務。要麼將ExecutorService更改爲Executors.newSingleThreadExecutor(),要麼將循環中的更多的threadAction實例提交給ExecutorService。

0

首先,您必須瞭解ExecutorService的作用。 ExecutorService實際上運行你的線程,你不需要在你創建的線程上調用start方法。在上面的代碼中,您已經創建了線程,但從未將其提交給ExecutorService。下面的例子將幫助您瞭解:

**

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

**