logo
down
shadow

Java - Thread.join( ) does not release the lock


Java - Thread.join( ) does not release the lock

By : Alexander Okunev
Date : November 23 2020, 03:01 PM
it helps some times That's because Thread.join() doesn't release any locks. You've designed a perfectly working deadlock, where Thread-1 is waiting for Thread-2 to die having locked on Foo, and Thread-2 is waiting to lock on Foo.
(Technically speaking the current implementation does release locks, as it uses internally wait() while synchronized on Thread. However that's an internal mechanism not related to user code)
code :


Share : facebook icon twitter icon
Thread synchronization- When does a thread release the lock on an object

Thread synchronization- When does a thread release the lock on an object


By : user2535469
Date : March 29 2020, 07:55 AM
I wish this helpful for you A synchronized method will only stop other threads from executing it while it is being executed. As soon as it returns other threads can (and often will immediately) get access.
The scenario to get your 1 1 2 2 ... could be:
Stop a thread and release the lock in Java

Stop a thread and release the lock in Java


By : Nick Heinbaugh
Date : March 29 2020, 07:55 AM
around this issue You can use thread interruption mechanism. If you want to interrupt on LOCK acquiring, you should use LOCK.lockInterruptibly() instead of LOCK.lock():
code :
Thread thread1 = new Thread() {
  @Override
  void run() {
    try {
      LOCK.lockInterruptibly();
      System.out.println("work");
      LOCK.unlock();
    } catch (InterruptedException ier) {
      this.interrupt()
    }
  }
};
thread1.interrupt();
Thread thread1 = new Thread(
  new Runnable() {
    @Override
    void run() {
      try {
        LOCK.lockInterruptibly();
        System.out.println("work");
        LOCK.unlock();
      } catch (InterruptedException ier) {
        Thread.currentThread().interrupt()
      }
    }
  }
);
If a thread is interrupted before calling Object.wait(), will the thread even bother to release the lock before throwing

If a thread is interrupted before calling Object.wait(), will the thread even bother to release the lock before throwing


By : Chatwinb
Date : March 29 2020, 07:55 AM
wish helps you
I just want to be absolutely certain that the use of word "restored" means the lock must be released and reacquired, instead of being hold continuously by the thread calling Object.wait(). In other words, there is a chance that the lock is granted to other threads first.
thread join vs. lock ( or semaphore ) in java

thread join vs. lock ( or semaphore ) in java


By : NuHouse
Date : March 29 2020, 07:55 AM
help you fix your problem It doesn't make any sense to ask about the difference between thread.join() and lock.lock()/lock.unlock(). They are used for completely different purposes.
Use thread.join() to make one thread wait for another thread to die. One use case for join() would be a program that performs some lengthy calculation. The main thread could create as many worker threads as the computer has CPUs and assign each worker to do one independent part of the calculation. Then the main thread could wait for all of the workers to finish their tasks by calling thread1.join(), thread2.join(), ... And then after they all were done, the main thread could combine the partial results produced by each thread into a final result
Does Thread.join() release the lock? Or continue to hold it?

Does Thread.join() release the lock? Or continue to hold it?


By : mtv
Date : March 29 2020, 07:55 AM
will be helpful for those in need wait releases the monitor held on the object which wait is invoked on, but not any other monitors.
code :
synchronized (a) {
    synchronized (b) {
        b.wait();
    }
}
shadow
Privacy Policy - Terms - Contact Us © voile276.org