开发者

How do I check the method state in java?

Lets say I have a instance method named myMethod(). I need to check if any thread is executing the method. Is there a way I can check this using reflection or some other API?

I want to execute some开发者_开发问答 cleanup logic when the method is NOT in execution. Hence need to check if the method is running or not.


There is no way to do exactly what you said (check whether other threads are inside of a method directly).

However, this sounds like a classic case for synchronization. Make the method invocation synchronize on some object's monitor; make your cleanup code synchronize on the same monitor, and voila! Now, if any thread is running the method(s) that you've made synchronized, your cleanup code can't run, but will wait until all threads have exited the methods and will then proceed.

Here's an example:

public class Foo
{
    private final Object LOCK = new Object();

    public void importantMethodOne()
    {
        synchronized(LOCK)
        {
            // Method body here
        }
    }

    public void importantMethodTwo()
    {
        synchronized(LOCK)
        {
            // Method body here
        }
    }

    public void otherMethod()
    {
        // Method body here
    }

    public void cleanupMethod()
    {
        synchronized(LOCK)
        {
            // Method body here
        }
    }
}

The LOCK monitor is a mutex - only one thread can hold it at once, so only one thread can be calling any of importantMethodOne, importantMethodTwo or the cleanupMethod at once. Any threads that try to invoke any of these methods while another is within the scope (and thus has the lock) will wait. In this example, otherMethod can be called at any time without interrupting anything else, because it doesn't acquire the lock.

Note that in this case, two threads cannot execute myMethod() at once because they'll block each other. If this is an issue, you can get around this with a ReadWriteLock (which allows many "readers" to hold it at once but one "writer"). If you acquire the read lock for your "normal" methods and the write lock for your "cleanup" methods, this will have the desired semantics.


I suppose you could have something like this: (Use indentation, don't feel like dealing with braces today)

int busy;

public boolean is Running()
    synchronized(lock1)
        return busy != 0;

public void run()
    try {
        synchronized(lock1)
            busy++

        do lots of stuff here
    } finally {
        synchronized(lock1)
            busy--
    }

It's early, but my tired brain can't find a problem with this--I'll re-read it later and see if it still makes sense.

But you will need more synchronization logic to ensure that nothing enters it while you are cleaning up--I didn't code that since today I'm answering the question you asked and not the one you meant to ask (others already did that)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜