Preventing an unsafe method in a multi-threaded context

Is there a way to throw an exception when a user tries to use an unsafe class method in a multi-threaded context? I think the problem is basically finding out that multiple threads are trying to use this method. Or is there a "not_synchronous" keyword / tag that I could use in a function declaration?

+7
source share
4 answers

There is no easy way to do this, no. If you find that multiple threads use the method, then most likely you will have to use thread-safe collections and the like. If you do all this, you may also need to make this method self-dependent.

+2
source

You can check if this method is used before allowing the thread to run it, but this is not much different than using a lock (note: my example is not repeated):

private static final AtomicBoolean used = new AtomicBoolean(); public static void unsafe() throws InterruptedException { if(!used.compareAndSet(false, true)) { throw new IllegalStateException(); } //do you stuff used.set(false); } 
+3
source

To extend the answer to โ€œGrayโ€: suppose you wanted to do this (determine when a method is used by multiple threads). A naive (and incorrect) implementation of this might look like this:

 volatile boolean methodBeingUsed = false; public void doSomething() { if (methodBeingUsed) throw new IllegalStateException("You can't do that!"); try { methodBeingUsed = true; // do something... } finally { methodBeingUsed = false; } } 

OK, OK ... but two threads could pass the first if (methodBeingUsed) and enter the critical section at the same time. Now, perhaps we will try to add a lock to protect the methodBeingUsed flag:

 Lock methodLock = new ReentrantLock(); volatile boolean methodBeingUsed = false; public void doSomething() { try { lock.lock(); if (methodBeingUsed) throw new IllegalStateException("You can't do that!"); methodBeingUsed = true; } finally { lock.unlock(); } try { // do something... } finally { try { lock.lock(); methodBeingUsed = false; } finally { lock.unlock(); } } } 

Of course, this suggests that doSomething () cannot call itself recursively. If possible, you should also monitor the calling thread. Add a few more checks to take into account other conditions that I donโ€™t think about right now, and itโ€™s easy to understand that the effort spent synchronizing the logic to find the method used by multiple threads is better spent just by making the -safe method thread start.

+1
source

Actually, if your goal is to determine that "several threads try to use this method", and you do not limit it to "several methods ... at the same time" - then (forgive me) Alex code can be easily adapted:

  • Change methodBeingUsed to threadOwningMethod and set it to thread instead of true .

  • You do not need to clean it at the end - save these lock / unlock steps. After he belonged, he owned (capitalist pig !!).

  • threadOwningMethod can be checked up to see if it matches the current thread (A-OK) or not (throw exception), without blocking. If it is not set (null), you get a one-time hit: check / lock / check & set / unlock; it is safe because threadOwningMethod marked volatile.

0
source

All Articles