我试图理解是什么让并发锁如此重要,如果可以使用synchronized (this).在下面的虚拟代码中,我可以做到:
码:
private final ReentrantLock lock = new ReentrantLock();
private static List<Integer> ints;
public Integer getResult(String name) {
.
.
.
lock.lock();
try {
if (ints.size()==3) {
ints=null;
return -9;
}
for (int x=0; x<ints.size(); x++) {
System.out.println("["+name+"] "+x+"/"+ints.size()+". values >>>>"+ints.get(x));
}
} finally {
lock.unlock();
}
return random;
}
Run Code Online (Sandbox Code Playgroud) 我有一个LockManager来管理几个线程的锁.有时线程是坏男孩,我必须杀死它们并要求LockManager释放所有锁.但是,由于我在java中使用ReentrantLock这是不可能的,我无法解锁另一个线程所拥有的锁.
我被迫使用Locks(不能使用信号量,这是功课的重点).是否有任何Java Lock实现允许我解锁其他线程拥有的锁?
到目前为止,我考虑的选项是:
您可能会觉得有用的额外资源:
我们在Weblogic 10 clustured环境中使用Spring Web Flow(2.0.9).在生产中我们得到了很多LockTimeoutException:30秒后无法获得对话锁定.
我一直试图弄清楚为什么只有一次点击或我们访问网站本身的主页时,在某些情况下会出现上述异常.
请在SWF中找到试图锁定FlowController的代码.我无法弄清楚的是锁定在正在访问的servlet上还是其他什么东西?
当发生此锁定时,请帮助理解Web应用程序中哪些资源实际上已锁定在SWF中?
要了解ReentrantLock的概念,请参阅以下链接.
提前致谢.
异常堆栈跟踪
org.springframework.webflow.conversation.impl.LockTimeoutException: Unable to acquire conversation lock after 30 seconds
at org.springframework.webflow.conversation.impl.JdkConcurrentConversationLock.lock(JdkConcurrentConversationLock.java:44)
at org.springframework.webflow.conversation.impl.ContainedConversation.lock(ContainedConversation.java:69)
at org.springframework.webflow.execution.repository.support.ConversationBackedFlowExecutionLock.lock(ConversationBackedFlowExecutionLock.java:51)
at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:166)
at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:183)
at org.springframework.webflow.mvc.servlet.FlowController.handleRequest(FlowController.java:174)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
Run Code Online (Sandbox Code Playgroud)
在SWF中锁定实现
package org.springframework.webflow.conversation.impl;
import java.io.Serializable;
import java.util.concurrent.locks.ReentrantLock;
/**
* A conversation lock that relies on a …Run Code Online (Sandbox Code Playgroud) 在Brian Goetz的书"Java Concurrency in Practice"中,他的Reentrant锁的例子编程如下:
Lock lock = new ReentrantLock();
Run Code Online (Sandbox Code Playgroud)
但是,我很想知道是否将上述代码更改为:
private static final Lock lock = new ReentrantLock();
Run Code Online (Sandbox Code Playgroud)
导致锁现在充当互斥锁,或者如果它是不必要和冗余的.
因此,如果将锁定为私有,静态和最终,此代码的功能是否会发生变化?
lock.lock();
try {
//method stuff
} finally {
lock.unlock();
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家.马特
你对这种方法实际使用了lockInterruptibly什么?我已阅读API,但对我来说不是很清楚.换句话说,有人可以表达吗?
ReadWriteLockReentrantReadWriteLock实现允许降级(tryLock()从下面的示例总是返回true):
void downgrade(final ReadWriteLock readWriteLock) {
boolean downgraded = false;
readWriteLock.writeLock().lock();
try {
// Always true, as we already hold a W lock.
final boolean readLockAcquired = readWriteLock.readLock().tryLock();
if (readLockAcquired) {
// Now holding both a R and a W lock.
assert ((ReentrantReadWriteLock) readWriteLock).getReadHoldCount() == 1;
assert ((ReentrantReadWriteLock) readWriteLock).getWriteHoldCount() == 1;
readWriteLock.writeLock().unlock();
downgraded = true;
try {
// Now do some work with only a R lock held
} finally {
readWriteLock.readLock().unlock();
assert ((ReentrantReadWriteLock) …Run Code Online (Sandbox Code Playgroud) 当我在http://javarevisited.blogspot.in/2013/03/reentrantlock-example-in-java-synchronized-difference-vs-lock.html上运行示例类时,我看到了与之相同的行为synchronized.
我有一个带有一些方法的objective-c类,它使用GCD队列来确保对资源的并发访问是串行进行的(标准方法是这样做).
其中一些方法需要调用同一类的其他方法.因此锁定机制需要重新进入.有没有标准的方法来做到这一点?
起初,我使用了这些方法
dispatch_sync(my_queue, ^{
// Critical section
});
Run Code Online (Sandbox Code Playgroud)
同步访问.如您所知,当其中一个方法调用另一个此类方法时,会发生死锁,因为dispatch_sync调用会停止当前执行,直到执行其他块,这也无法执行,因为队列上的执行已停止.为了解决这个问题,我然后使用了这种方法:
- (void) executeOnQueueSync:(dispatch_queue_t)queue : (void (^)(void))theBlock {
if (dispatch_get_current_queue() == queue) {
theBlock();
} else {
dispatch_sync(queue, theBlock);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的每个方法中,我都使用
[self executeOnQueueSync:my_queue : ^{
// Critical section
}];
Run Code Online (Sandbox Code Playgroud)
我不喜欢这个解决方案,因为对于每个具有不同返回类型的块,我需要编写另一个方法.此外,这个问题对我来说非常普遍,我认为应该存在一个更好的标准解决方案.
从这个链接,我理解"由于lock()和unlock()方法调用是显式的,我们可以将它们移动到任何地方,建立任何锁定范围,从单行代码到跨越多个方法的范围"
所以我从上面的陈述中理解的是
public class Test {
Lock l = new ReentrantLock();
void myMethod1() {
l.lock();
// Do my stuff here
}
void myMethod2() {
// Do more stuff here
l.unlock();
}
}
Run Code Online (Sandbox Code Playgroud)
所以基本上1可以按顺序调用method1和method2,并假设调用是线程安全的.
我不确定如上所述是否属实.
如果有人在我已经执行method1/method2对时调用method2怎么办?这不是事情的复杂化.
我认为在从函数返回控件之前,应该在函数本身中获取并释放锁.我的理解是否正确?
我正在尝试编写一个测试,在其中我想演示公平和不公平可重入锁之间的区别。该测试使用ThreadPoolExecutor并由多个迭代组成,每个迭代都有以下步骤:
AtomicInteger状态。因此,对于公平锁,共享状态的最终值必须等于最后一个任务的索引。但测试在所有执行中约有 50% 失败。
我的代码如下所示:
@Test
void should_be_fair() throws InterruptedException, ExecutionException {
int iterationsCount = 100;
int waitingThreadsCount = 5;
ReentrantLock lock = new ReentrantLock(true);
Semaphore unlockingSemaphore = new Semaphore(1);
boolean wasAnyThreadUnfair = false;
for (int i = 0; i < iterationsCount; i++) {
unlockingSemaphore.acquire();
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(waitingThreadsCount + 1);
Future<?> lockingFuture = executor.submit(() -> {
try {
lock.lock();
unlockingSemaphore.acquire();
} catch (InterruptedException e) {
throw new …Run Code Online (Sandbox Code Playgroud) reentrantlock ×10
java ×8
concurrency ×4
locking ×3
locks ×1
mutex ×1
objective-c ×1
synchronize ×1