It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes Synchronozation How it Works Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronozation How it Works" Watch "Synchronozation How it Works" New topic

Synchronozation How it Works

Neeraj Thakkar
Ranch Hand

Joined: Dec 06, 2000
Posts: 80
I made a method synchronized. Then I tried to acess that method through two threads. They both accessed the method and executed the method simultaneously of curse scheduling was there.
But when I made it Static synchronized Only thread could access it then the other thread came and accessed it WHY?

Neeraj Thakkar<br />"Try to get what you like,Else you will be forced to like what you get."
Mark Herschberg

Joined: Dec 04, 2000
Posts: 6037
I'm not sure I understand the question. But here's what happens.
The two threads do not run simultaneously, of course. They never can on a single processor. The synchronization creates a synchronization block (using it in the method declaration makes the whole method the block). What it (effectively) garantees is that once one thread enters the block, i.e. runs the code, no other thread can enter the block, until the first thread leaves it. (Technically, it garantess that no other thread can acquire the lock until the first one gives it up, which it will do when it is done executing the code block.)
Without a synchronization block, one thread can start executing a few lines of code, and then a second thread can execute more lines, and put the objects which get modified in the code, into an inconsistent state.
The locks used in sychronization can be any java object. When you use the synchronized in a non-staic method, or if you "synchronize (this)" you are using the instance of the class as the lock. When the method is static, there is no specific instance which can be used as the lock (simiar to the reason you can't use the "this" object reference in static methods). Instead, each class has a class level lock. This class lock is shared by all instances of the object. What it means is that given two seperate object instances, synchronizations on any static method compete for the same lock, whereas if the method was non-static, those methods use different locks.
See Doug Lee's Concurrent Programming in Java for more info.
(I'm not the best thread expert, so others should feel free to correct anything I got wrong.)
Mahanthi Bukkapatnam

Joined: Dec 19, 2000
Posts: 11

Try running the following code. Hope it rings a bell.

Two threads on different Objects They run independently, no blocking happens.
Two threads on same Object They fight for the lock, blocking happens.
Two threads on calling static method on different Objects They fight for the Class lock, blocking happens.

class COne implements Runnable {
synchronized void method() {
for( int i=0; i<5; i++ ) {
System.out.println( "COne i = " + i );
try { Thread.sleep(1500); } catch( InterruptedException e ) { }
public void run() {
class CTwo implements Runnable {
static synchronized void method() {
for( int i=0; i<5; i++ ) {
System.out.println( "COne i = " + i );
try { Thread.sleep(1500); } catch( InterruptedException e ) { }
public void run() {
public class ThreadSynchronization {

public static void main( String [] args ) throws InterruptedException {
System.out.println( "Case 1, two threads calling on different objects, each thread gets a lock");
Thread t1 = new Thread( new COne() );
Thread t2 = new Thread( new COne() );

System.out.println( "Case 2, two threads calling on the same object. The second thread blocks.");
COne obj = new COne();
Thread t3 = new Thread( obj );
Thread t4 = new Thread( obj );

System.out.println( "Case 3, two threads calling a static synchronized method on different objects. The second thread Blocks");
Thread t5 = new Thread( new CTwo() );
Thread t6 = new Thread( new CTwo() );
I agree. Here's the link:
subject: Synchronozation How it Works
It's not a secret anymore!