Threads - Yash-777/LearnJava GitHub Wiki

Threads - Multi Threading

  • How to interrupt a running Threads
  • Synchronized block. in multi thread concept how do other threads.
  • How do we come to know the block is used by other thread so we need to wait.

Multi Threading over shared resource can be achieved in two ways internal-synchronized member and lock variable.


Object level lock vs Class level lock in Java.

In Java, a synchronized block of code can only be executed by one thread at a time. Also, java supports multiple threads to be executed concurrently. This may cause two or more threads to access the same fields or objects at same time.

When a method is declared as synchronized; the thread holds the monitor or lockjava.util.concurrent.locks.Lock object for that method’s object. If another thread is executing the synchronized method, your thread is blocked until that thread releases the monitor.

//       Object level lock                           Class level lock
public class DemoClass {                     public class DemoClass { //Method is static
    public synchronized void demoMethod(){}    public synchronized static void demoMethod(){}
}                                            }
 
public class DemoClass {                     public class DemoClass {
  public void demoMethod() {                   public void demoMethod() { //Acquire lock on .class reference
    synchronized (this) {                        synchronized (DemoClass.class) {
      //other thread safe code                     //other thread safe code
    }                                            }
  }                                            }
}                                            }
 
public class DemoClass {                     public class DemoClass {
  private final Object lock = new Object();    private final static Object lock = new Object();
                                              
  public void demoMethod() {                   public void demoMethod() { //Lock object is static
    synchronized (lock) {                        synchronized (lock) {
      //other thread safe code                     //other thread safe code
    }                                            }
  }                                            }
}                                            }

Java synchronized keyword is re-entrant in nature it means if a synchronized method calls another synchronized method which requires same lock then current thread which is holding lock can enter into that method without acquiring lock.

Lock framework package java.util.concurrent.locksSince:1.5

The Lock framework in java.util.concurrent.lock is an abstraction for locking, allowing for lock implementations that are implemented as Java classes rather than as a language feature. It makes room for multiple implementations of Lock, which may have different scheduling algorithms, performance characteristics, or locking semantics.

Class ReentrantLock

Because the thread owns the lock it will allow multiple calls to lock(), so it re-enter the lock. This can be achieved with a reference count so it doesn't has to acquire lock again.stackoverflow

private static ReentrantLock lock = new ReentrantLock();

void accessResource() {
    lock.lock();
    if( checkSomeCondition() ) {
        accessResource();
    }
    lock.unlock();
}

Form Java DOC: This lock supports a maximum of 2147483647 recursive locks by the same thread. Attempts to exceed this limit result in Error throws from locking methods.

Interface ReadWriteLock - Class ReentrantReadWriteLock

⚠️ **GitHub.com Fallback** ⚠️