锁定从Singleton EJB到无状态会话Bean的传播

时间:2016-11-29 18:03:11

标签: java multithreading java-ee threadpool ejb-3.1

我有这个EJB Singleton(EJB 3.1):

@Singleton
@Startup
@Lock(LockType.READ)
public class SingletonExample {

@EJB
private StatelessSBExample stlsb;
...
    @Schedule(..........., persistent = false)
    @AccessTimeout(0)
    @Lock(LockType.READ)
    public void call1SB() {
         stlsb.doSomething();
    }

    @Schedule(..........., persistent = false)
    @AccessTimeout(0)
    @Lock(LockType.READ)
    public void call2SB() {
        stlsb.doSomething();
    }
}

我的bean是一个传统的EJB无状态会话Bean:

@Stateless
public class StatelessSBExample {
    public void domSomething() {
    ...
    }
}

使用visualvm进行监控,我意识到有些线程正在积累。应用程序以Thread Live Peak = 92开始,现在是102.它正在增加。在VisualVM线程中,我有几个状态为“Park”和“Wait”的线程。 在我的线程转储中,我有很多:

"Thread-42" - Thread t@190
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <71bfce05> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1088)
    at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

"__ejb-thread-pool13" - Thread t@130
   java.lang.Thread.State: WAITING
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for <5cfe398e> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

我的错在哪里?我只想执行call1SB(),如果它正在运行,则不再执行此方法(与call2SB相同)

P.S。我不能使用LockType.WRITE,因为我想同时执行call1SB()和call2SB()(我的单例中没有属性..只有方法)

1 个答案:

答案 0 :(得分:0)

默认的EJB锁定机制适用于常见用例,但它们不是很灵活。在这种情况下,我建议使用你自己的锁定机制,如下所示:

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

public void call1SB() {
    if(lock.writeLock().tryLock()) { 
    // Acquires the write lock only if it 
    // is not held by another thread at the time of invocation.
        stlsb.doSomething();
    } // else { return; } // do nothing if already locked
}

与第二个Singleton方法的第二个锁相同。