mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
scheduler service as singleton
This commit is contained in:
parent
32497dc988
commit
1ca3d65a4e
1 changed files with 21 additions and 6 deletions
|
|
@ -25,21 +25,36 @@ import java.util.concurrent.TimeUnit;
|
|||
*
|
||||
*/
|
||||
public class SchedulerService {
|
||||
|
||||
|
||||
private final ScheduledExecutorService pool;
|
||||
|
||||
private static SchedulerService instance = null;
|
||||
|
||||
private SchedulerService(int numberOfThread) {
|
||||
pool = Executors.newScheduledThreadPool(numberOfThread);
|
||||
}
|
||||
|
||||
|
||||
public static SchedulerService create(int numberOfThread) {
|
||||
return new SchedulerService(numberOfThread);
|
||||
if (instance == null) {
|
||||
instance = new SchedulerService(numberOfThread);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
public static SchedulerService getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new SchedulerService(2);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void add(Runnable service, int firstExecution, int period) {
|
||||
pool.scheduleAtFixedRate(service, firstExecution, period, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
|
||||
public void addOnce(Runnable service, int firstExecution) {
|
||||
pool.schedule(service, firstExecution, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
pool.shutdown();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue