【发布时间】:2012-02-12 21:58:06
【问题描述】:
我想并行执行一些不同的任务,但有一个概念,如果一个任务已经排队或正在处理,它不会重新排队。我已经阅读了一些关于 Java API 的内容,并提出了下面的代码,这似乎可行。 任何人都可以阐明我使用的方法是否是最好的方法。任何危险(线程安全?)或更好的方法来做到这一点? 代码如下:
import java.util.HashMap;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestExecution implements Runnable {
String key1;
String key2;
static HashMap<TestExecution, Future<?>> executions = new HashMap<TestExecution, Future<?>>();
static LinkedBlockingQueue<Runnable> q = new LinkedBlockingQueue<Runnable>();
static ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 5, 1, TimeUnit.MINUTES, q);
public static void main(String[] args) {
try {
execute(new TestExecution("A", "A"));
execute(new TestExecution("A", "A"));
execute(new TestExecution("B", "B"));
Thread.sleep(8000);
execute(new TestExecution("B", "B"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static boolean execute(TestExecution e) {
System.out.println("Handling "+e.key1+":"+e.key2);
if (executions.containsKey(e)) {
Future<?> f = (Future<?>) executions.get(e);
if (f.isDone()) {
System.out.println("Previous execution has completed");
executions.remove(e);
} else {
System.out.println("Previous execution still running");
return false;
}
}
else {
System.out.println("No previous execution");
}
Future<?> f = tpe.submit(e);
executions.put(e, f);
return true;
}
public TestExecution(String key1, String key2) {
this.key1 = key1;
this.key2 = key2;
}
public boolean equals(Object obj)
{
if (obj instanceof TestExecution)
{
TestExecution t = (TestExecution) obj;
return (key1.equals(t.key1) && key2.equals(t.key2));
}
return false;
}
public int hashCode ()
{
return key1.hashCode()+key2.hashCode();
}
public void run() {
try {
System.out.println("Start processing "+key1+":"+key2);
Thread.sleep(4000);
System.out.println("Finish processing "+key1+":"+key2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
关注下方评论:
计划是触发要执行的任务将由 cron 调用 RESTful Web 服务来处理。例如,下面是每天 9:30 触发的一项任务的设置,以及每两分钟安排的另一项任务。
0/2 * * * * restclient.pl key11 key12
30 09 * * * restclient.pl key21 key22
在这种情况下,如果任务 key11:key12 正在运行,或者已经排队运行,我不想排队另一个实例。我知道我们还有其他调度选项,但是我们倾向于将 cron 用于其他任务,所以我想尽量保留这个。
第二次更新。为了响应 cmets 到目前为止我已经重写了代码,您能否评论以下更新解决方案的任何问题?
import java.util.concurrent.LinkedBlockingQueue;
public class TestExecution implements Runnable {
String key1;
String key2;
static TestThreadPoolExecutor tpe = new TestThreadPoolExecutor(new LinkedBlockingQueue<Runnable>());
public static void main(String[] args) {
try {
tpe.execute(new TestExecution("A", "A"));
tpe.execute(new TestExecution("A", "A"));
tpe.execute(new TestExecution("B", "B"));
Thread.sleep(8000);
tpe.execute(new TestExecution("B", "B"));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public TestExecution(String key1, String key2) {
this.key1 = key1;
this.key2 = key2;
}
public boolean equals(Object obj)
{
if (obj instanceof TestExecution)
{
TestExecution t = (TestExecution) obj;
return (key1.equals(t.key1) && key2.equals(t.key2));
}
return false;
}
public int hashCode ()
{
return key1.hashCode()+key2.hashCode();
}
public void run() {
try {
System.out.println("Start processing "+key1+":"+key2);
Thread.sleep(4000);
System.out.println("Finish processing "+key1+":"+key2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestThreadPoolExecutor extends ThreadPoolExecutor {
Set<Runnable> executions = Collections.synchronizedSet(new HashSet<Runnable>());
public TestThreadPoolExecutor(LinkedBlockingQueue<Runnable> q) {
super(2, 5, 1, TimeUnit.MINUTES, q);
}
public void execute(Runnable command) {
if (executions.contains(command)) {
System.out.println("Previous execution still running");
return;
}
else {
System.out.println("No previous execution");
}
super.execute(command);
executions.add(command);
}
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
executions.remove(r);
}
}
【问题讨论】:
-
为什么不在 TestExecution 中使用 hashset 而不是 HashMap??
标签: java multithreading threadpool