Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java threads and synchronized blocks

Suppose I'm executing a synchronized block of code inside some thread and within the synchronized block I call a method that spawns another thread to process a synchronized block of code that requires the same lock as the first method. So in pseudo Java code:

public void someMethod() {
  synchronized(lock_obj) {
    // a whole bunch of stuff...

    // this is the last statement in the block
    (new Thread(someOtherMethod())).start();
  }
    // some more code that doesn't require a lock
}

public void someOtherMethod() {
  // some setup code that doesn't require a lock

  // return the stuff we want to run in another thread
  // that does require a lock
  return new Runnable() {
    @Override
    public void run() {
      synchronized(lock_obj) {
        // some more code
      }
    }
  };
}

I have no idea how to make sense of that code. Is what I have written even legal? Syntactically I don't see any issues but I'm not sure how to reason through code like that. So when I execute someOtherMethod() in order to create an instance of Runnable in what kind of scope does the code before the return statement run? Does it execute as part of the first synchronized block? Assume there are some other threads working as well that might require the lock on lock_obj.

like image 705
David K. Avatar asked Mar 14 '11 05:03

David K.


People also ask

What are synchronized threads in Java?

Thread Synchronization is a process of allowing only one thread to use the object when multiple threads are trying to use the particular object at the same time. To achieve this Thread Synchronization we have to use a java keyword or modifier called “synchronized”.

What is Java synchronization block?

A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.

Does Java support thread synchronization?

Java programming language provides a keyword Synchronized' that allows us to synchronize the threads by making a block or method as Synchronized. The shared resources that the threads need to access are kept under this Synchronized block/method.

What is threads and synchronization?

Thread synchronization basically refers to The concept of one thread execute at a time and the rest of the threads are in waiting state. This process is known as thread synchronization.


2 Answers

You are still holding the lock during the creation of runnable and the thread, but after you call start and before the thread actually picks up you are relinquishing the lock. The new thread will have to compete for the lock with other threads.

like image 53
Konstantin Komissarchik Avatar answered Sep 30 '22 00:09

Konstantin Komissarchik


There's nothing wrong about this code. Before the return statement in someOtherMethod(), the code is running in the synchronized block of someMethod(). After the new thread starts, it will block on the synchronized statement inside the run() method until it obtains a lock on lock_obj (at the earliest, whenever someMethod() exits its synchronized block).

like image 31
Ted Hopp Avatar answered Sep 30 '22 00:09

Ted Hopp