File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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@rediffmail.com


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

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.)
--Mark
hershey@vaultus.com
Mahanthi Bukkapatnam
Greenhorn

Joined: Dec 19, 2000
Posts: 11
/*
Hi

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.

<PRE>
*/
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() {
method();
}
}
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() {
method();
}
}
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() );
t1.start();
t2.start();

t1.join();
t2.join();
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 );
t3.start();
t4.start();
t3.join();
t4.join();

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() );
t5.start();
t6.start();
t5.join();
t6.join();
}
}
//</PRE>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronozation How it Works
 
Similar Threads
Synchronization on Threads in Java
threads for a instance
Intance methods
I need to synchronize a block of code...
Basic question on wait & notify