2010-06-08 21 views
5

La classe SimpleThreadPool fornita con Quartz Scheduler non ha un comportamento FIFO. Voglio essere sicuro che se continuo ad aggiungere lavori allo scheduler, sono indirizzati in base al principio "First in I - 1". C'è qualche ThreadPool disponibile per questo? O esiste un altro modo per raggiungere questo obiettivo?Quartz scheduler theadpool

risposta

5

Si potrebbe raggiungere questo obiettivo, delegando ad un ThreadPoolExecutor con una coda FIFO, come segue:

public class DelegatingThreadPool implements ThreadPool { 

private int size = 5; //Fix this up if you like 
private final ThreadPoolExecutor executor = new ThreadPoolExecutor(size, size, 
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); 

public boolean runInThread(Runnable runnable) { 
    synchronized (executor) { 
     if (executor.getActiveCount() == size) { 
      return false; 
     } 
     executor.submit(runnable); 
     return true; 
    } 
} 

public int blockForAvailableThreads() { 
    synchronized (executor) { 
     return executor.getActiveCount(); 
    } 
} 

public void initialize() throws SchedulerConfigException { 
    //noop 
} 

public void shutdown(boolean waitForJobsToComplete) { 
    //No impl provided for wait, write one if you like 
    executor.shutdownNow(); 
} 

public int getPoolSize() { 
    return size; 
} 

public void setInstanceId(String schedInstId) { 
    //Do what you like here 
} 

public void setInstanceName(String schedName) { 
    //Do what you like here 
} 

E 'possibile che il conteggio attiva di eseguibili non corrispondere esattamente al numero esatto di attività che sono in esecuzione. Dovresti aggiungere un latch e utilizzare beforeExecute per garantire che l'attività sia iniziata in esecuzione se necessario.

+0

Questo è un ottimo esempio, ci proverò. – Shamik