How do determine if an object is locked (synchronized) so not to block in Java?(如何确定一个对象是否被锁定(同步)以免在 Java 中阻塞?)
问题描述
我有一个进程 A,它在内存中包含一个带有一组记录(recordA、recordB 等)的表
I have a process A that contains a table in memory with a set of records (recordA, recordB, etc...)
现在,这个进程可以启动许多影响记录的线程,有时我们可以有 2 个线程尝试访问同一个记录 - 这种情况必须被拒绝.特别是如果一条记录被一个线程锁定,我希望另一个线程中止(我不想阻塞或等待).
Now, this process can launch many threads that affect the records, and sometimes we can have 2 threads trying to access the same record - this situation must be denied. Specifically if a record is LOCKED by one thread I want the other thread to abort (I do not want to BLOCK or WAIT).
目前我做这样的事情:
synchronized(record)
{
performOperation(record);
}
但这给我带来了问题......因为当 Process1 正在执行操作时,如果 Process2 进入它会阻塞/等待同步语句,并且当 Process1 完成时它会执行操作.相反,我想要这样的东西:
But this is causing me problems ... because while Process1 is performing the operation, if Process2 comes in it blocks/waits on the synchronized statement and when Process1 is finished it performs the operation. Instead I want something like this:
if (record is locked)
return;
synchronized(record)
{
performOperation(record);
}
关于如何实现这一点的任何线索?任何帮助将非常感激.谢谢,
Any clues on how this can be accomplished? Any help would be much appreciated. Thanks,
推荐答案
需要注意的一点是,您收到此类信息的瞬间,它已经过时了.换句话说,您可能会被告知没有人拥有锁,但是当您尝试获取它时,您会阻塞,因为另一个线程在检查和您尝试获取它之间取出了锁.
One thing to note is that the instant you receive such information, it's stale. In other words, you could be told that no-one has the lock, but then when you try to acquire it, you block because another thread took out the lock between the check and you trying to acquire it.
Brian 指出 Lock 是对的,但我认为您真正想要的是它的 tryLock 方法:
Brian is right to point at Lock, but I think what you really want is its tryLock method:
Lock lock = new ReentrantLock();
......
if (lock.tryLock())
{
// Got the lock
try
{
// Process record
}
finally
{
// Make sure to unlock so that we don't cause a deadlock
lock.unlock();
}
}
else
{
// Someone else had the lock, abort
}
您也可以调用 tryLock 并等待一段时间 - 因此您可以尝试在十分之一秒内获取它,然后如果无法获取它则中止(例如).
You can also call tryLock with an amount of time to wait - so you could try to acquire it for a tenth of a second, then abort if you can't get it (for example).
(我认为很遗憾 Java API 没有 - 据我所知 - 为内置"锁定提供与 Monitor 类相同的功能在 .NET 中.再说一次,在线程方面,我不喜欢这两个平台中的许多其他东西——例如,每个对象都可能有一个监视器!)
(I think it's a pity that the Java API doesn't - as far as I'm aware - provide the same functionality for the "built-in" locking, as the Monitor class does in .NET. Then again, there are plenty of other things I dislike in both platforms when it comes to threading - every object potentially having a monitor, for example!)
这篇关于如何确定一个对象是否被锁定(同步)以免在 Java 中阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何确定一个对象是否被锁定(同步)以免在 Java 中阻塞?
基础教程推荐
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
