Java - 使用 ReentrantLock 异步运行作业?
下面的代码允许我们运行一个作业,同时使用ReentrantLock
确保一次只能运行一个作业。
有没有办法修改此代码以异步运行 job.call()
并在启动线程之前将 MyConcurrentJobException
返回给客户端?
我们尝试将 try/catch/finally 块包装在一个新的 Thread
中,但是 unlock
和 lock
必须发生在同一个线程中,所以我们得到一个IllegalMonitorException
??
final static Lock lock = new ReentrantLock();
public Object runJob(String desc, Callable job, boolean wait) {
logger.info("Acquiring lock");
if (!lock.tryLock()) {
throw new MyConcurrentJobException();
}
activeJob = new JobStatus(desc);
logger.info("Lock acquired");
try {
return job.call();
} catch (MarginServiceAssertionException e) {
throw e;
} catch (MarginServiceSystemException e) {
throw e;
} catch (Exception e) {
throw new MarginServiceSystemException(e);
} finally {
activeJob = null;
logger.info("Releasing lock");
lock.unlock();
logger.info("Lock released");
}
}
The code below allows us to run a job
while ensuring that only one job at a time can run by using ReentrantLock
.
Is there any way to modify this code to run job.call()
asynchronously and to return the MyConcurrentJobException
to the client prior to starting the thread?
We tried wrapping the try/catch/finally block in a new Thread
but the unlock
and lock
have to happen in the same thread so we get an IllegalMonitorException
??
final static Lock lock = new ReentrantLock();
public Object runJob(String desc, Callable job, boolean wait) {
logger.info("Acquiring lock");
if (!lock.tryLock()) {
throw new MyConcurrentJobException();
}
activeJob = new JobStatus(desc);
logger.info("Lock acquired");
try {
return job.call();
} catch (MarginServiceAssertionException e) {
throw e;
} catch (MarginServiceSystemException e) {
throw e;
} catch (Exception e) {
throw new MarginServiceSystemException(e);
} finally {
activeJob = null;
logger.info("Releasing lock");
lock.unlock();
logger.info("Lock released");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Semaphore
代替ReentrantLock
,它的许可不与线程绑定。像这样的事情(不确定在异步情况下您想如何处理 job.call() 的结果):
You can use
Semaphore
instead ofReentrantLock
, its permits are not bound to thread.Something like this (not sure what you want to do with the result of
job.call()
in the asynchronous case):我认为我完全误解了,因为异步执行某些操作时阻塞和等待对我来说没有太大意义,除非可以在调用线程上取得一些进展。
你能做这样的事情吗:
I think I am misunderstanding completely because to block and wait while doing something asynchronously doesn't make too much sense to me unless some progress can be made on the invoking thread.
Could you do something like this: