summaryrefslogtreecommitdiffstats
path: root/simple/simple-common/src/main/java/org/simpleframework/common/thread/ConcurrentScheduler.java
blob: bb4a117432ac25cdc092edea5b926ed9da3ab14a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * ConcurrentScheduler.java February 2007
 *
 * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.common.thread;

import java.util.concurrent.TimeUnit;

/**
 * The <code>ConcurrentScheduler</code> object is used to schedule tasks 
 * for execution. This queues the task for the requested period of 
 * time before it is executed. It ensures that the delay is adhered
 * to such that tasks can be timed for execution in an accurate way.
 * 
 * @author Niall Gallagher
 */
public class ConcurrentScheduler implements Scheduler {
   
   /**
    * This is the scheduler queue used to enque tasks to execute.
    */
   private final SchedulerQueue queue;
   
   /**
    * Constructor for the <code>ConcurrentScheduler</code> object. 
    * This will create a scheduler with a fixed number of threads to 
    * use before execution. Depending on the types of task that are
    * to be executed this should be increased for accuracy.
    * 
    * @param type this is the type of the worker threads
    */
   public ConcurrentScheduler(Class type) {
      this(type, 10);
   }
   
   /**
    * Constructor for the <code>ConcurrentScheduler</code> object. 
    * This will create a scheduler with a fixed number of threads to 
    * use before execution. Depending on the types of task that are
    * to be executed this should be increased for accuracy.
    * 
    * @param type this is the type of the worker threads
    * @param size this is the number of threads for the scheduler
    */
   public ConcurrentScheduler(Class type, int size) {
      this.queue = new SchedulerQueue(type, size);
   }

   /**
    * This will execute the task within the executor immediately 
    * as it uses a delay duration of zero milliseconds. This can
    * be used if the scheduler is to be used as a thread pool.
    * 
    * @param task this is the task to schedule for execution
    */
   public void execute(Runnable task) {
      queue.execute(task);           
   }
   
   /**
    * This will execute the task within the executor after the time
    * specified has expired. If the time specified is zero then it
    * will be executed immediately. Once the scheduler has been
    * stopped then this method will no longer accept runnable tasks.
    * 
    * @param task this is the task to schedule for execution
    * @param delay the time in milliseconds to wait for execution
    */   
   public void execute(Runnable task, long delay) {
      execute(task, delay, TimeUnit.MILLISECONDS);
   }
   
   /**
    * This will execute the task within the executor after the time
    * specified has expired. If the time specified is zero then it
    * will be executed immediately. Once the scheduler has been
    * stopped then this method will no longer accept runnable tasks.
    * 
    * @param task this is the task to schedule for execution
    * @param delay this is the delay to wait before execution
    * @param unit this is the duration time unit to wait for
    */   
   public void execute(Runnable task, long delay, TimeUnit unit) {
      queue.execute(task, delay, unit);
   }

   /**
    * This is used to stop the scheduler by interrupting all running
    * tasks and shutting down the threads within the pool. This will
    * return immediately once it has been stopped, and not further
    * tasks will be accepted by this pool for execution.
    */   
   public void stop() {
      stop(60000);          
   }
   
   /**
    * This is used to stop the scheduler by interrupting all running
    * tasks and shutting down the threads within the pool. This will
    * return once it has been stopped, and no further tasks will be 
    * accepted by this pool for execution.
    *
    * @param wait the number of milliseconds to wait for it to stop 
    */   
   public void stop(long wait) {
      queue.stop(wait);
   }
}