シーヶンスキューインプリメンテーションタスクこの実行-タスクスケジューリングシリーズ2
3724 ワード
シーケンスキューは、タスクを実行するためのアイデアを実現します.
public class TaskManage {
//
private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(10);
private boolean running = false;
public void start() {
running = true;
Thread t = new Thread(new OrderedJob());
t.start();
}
public void stop() {
running = false;
}
public void submit(Runnable task){
try {
if(queue.offer(task, 5000L, TimeUnit.MILLISECONDS) == false){
System.out.println();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
class OrderedJob implements Runnable {
@Override
public void run() {
while (running) {
try {
Runnable job = queue.poll(5000L, TimeUnit.MILLISECONDS);
try {
if (job != null)
job.run();
} catch (RuntimeException e) {
// TODO: handle exception
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}