English 中文(简体)
不再锁定
原标题:ReentrantLock.tryLock() not locking

我认为,我看不出过去工作的情况。

我的锁定了排他性锁,在设计了新物体时,将安排<条码>、Lock、真实回归和另一条<条码> 。

public class A {
    private static Timer timer = new Timer();
    private static Lock clean_lock = new ReentrantLock();
    private static ConcurrentHashMap<String,B> _b_dict = new ConcurrentHashmap<String,B>();

    public A() {
        if(clean_lock.tryLock()) {
            timer.scheduleAtFixedRate(new TimerTaskThread(), new Date(), 60000);
        }
    }

    //Various NON static methods 
    // use an iterator at one point so they must be NON static

    class TimerTaskThread extends TimerTask {
        public void run() {
            //delete old stuff in _b_dict
        }
    }
}

//sample usage
public class Main {
    public Main() {
        A a = new A();
        a.contains(new B());
    }
}
最佳回答

Are you creating the other A instance from another thread? Because if you re creating both instances from the same thread, then, the lock being reentrant, tryLock obviously returns true.

如果你真的想从施工人员而不是从固定的组群中排出,那么你就应当使用静态的<代码>AtomicBoolean变量,并且只安排时间:compareAndSet(false, real)

问题回答

For what purpose are you using the lock here; just to ensure that timer.scheduleAtFixedRate is called only once (the first time the constructor of A is called)?

You could do it in a static initializer block instead, so that you don t need the lock at all:

public class A {
    private static final Timer timer = new Timer();
    private static final ConcurrentHashMap<String,B> _b_dict = new ConcurrentHashmap<String,B>();

    static {
        timer.scheduleAtFixedRate(new TimerTaskThread(), new Date(), 60000);
    }

    // etc.
}

If you use a static block, the scheduling will be done when the class is initialized (after it s loaded), rather than the first time you create an instance of it.

另一种可能性是使用<代码>。 原子生物:

public class A {
    private static final Timer timer = new Timer();
    private static final AtomicBoolean initDone = new AtomicBoolean();
    private static final ConcurrentHashMap<String,B> _b_dict = new ConcurrentHashmap<String,B>();

    public A() {
        if (!initDone.getAndSet(true)) {
            timer.scheduleAtFixedRate(new TimerTaskThread(), new Date(), 60000);
        }
    }

    // etc.
}

这就是我要写的。

public class A {
    private static final ExecutorService service = Executors.newScheduledExecutorService();
    private static final ConcurrentMap<String,B> _b_dict = new ConcurrentHashmap<String,B>();
    static {
       service.scheduleAtFixedRate(new Runnable() {
           public void run() {
               cleanUp();
           }
       }, 1, 1, TimeUnit.MINUTES);
    }

    static void cleanUp() {
       // remove old entries
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签