Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-06-2012, 02:40 PM   PM User | #1
Zaid
New Coder

 
Join Date: Sep 2012
Location: Mumbai
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Zaid is an unknown quantity at this point
Question Interesting fact about synchronized method.

Code:
class A
{
	int x=1,y=1;
	static int z;
	synchronized void f1()
	{
		x++;
		z++;
		System.out.println("Called "+z+" Time");
		try
		{
			Thread.sleep(10000);
		}
		catch(InterruptedException e){}
		System.out.println("(Synchronized)X= "+x);
	}	
}

class B extends Thread
{
	A ab;
	B(A aob)
	{
		ab=aob;
	}
	
	public void run()
	{
		ab.f1();
	}
}

class Multi_sync
{
	public static void main(String args[])
	{
		A a1=new A();
		A a2=new A();
		B b1=new B(a1);
		B b2=new B(a2);
		b1.start();
		b2.start();
	}
}
The output of the program is

Called 1 Time
Called 2 Time
(Synchronized)X= 2
(Synchronized)X= 2

Although it should be,
Called 1 Time
(Synchronized)X= 2
Called 2 Time
(Synchronized)X= 2

I don't understand the o/p, because I think if a synchronized method is invoked for the first time, it won't get executed for the 2nd until the first invocation returns the control....is it right?
Zaid is offline   Reply With Quote
Old 11-06-2012, 03:52 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
a1 and a2 are running through b1 and b2. These are threads of their own, so you are not doing anything to block the other objects, so issuing a sleep on that running thread won't impact another thread. There are many ways to do this from the simple implicit locks of the object, to running with ExecutorService and pools or writing your own threads and runnables. But you'll need to write the blocking conditions for the thread.
Fou-Lu is offline   Reply With Quote
Old 11-06-2012, 05:24 PM   PM User | #3
Zaid
New Coder

 
Join Date: Sep 2012
Location: Mumbai
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Zaid is an unknown quantity at this point
Thumbs up

Quote:
Originally Posted by Fou-Lu View Post
a1 and a2 are running through b1 and b2. These are threads of their own, so you are not doing anything to block the other objects, so issuing a sleep on that running thread won't impact another thread. There are many ways to do this from the simple implicit locks of the object, to running with ExecutorService and pools or writing your own threads and runnables. But you'll need to write the blocking conditions for the thread.
I think u got it wrong...My question was that if a synchronized method is called by a thread then it should not allow its control to other threads until it releases control for the 1st thread....
I guess when a synchronized method is called by the same reference for the 2nd time then it serializes it(blocks it until the first call returns control)...In the above case, I have used different references(a1 and a2) and although they call the same method they are allow concurrent access without synchronization. Correct if I am wrong
Zaid is offline   Reply With Quote
Old 11-06-2012, 07:13 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Uh, no. The synchronized functionality in Java's purpose is to maintain integrity of an instance of an object, not multiple instances of an object. Multiple threads is of course the purpose, but when each of these threads has an instance of their own to work with, then there is no impact of using the synchronization functionality from one object into the next object. Synchronized can also be done on static methods as well as blocks (my preference if I run synchronized).
So in short, its purpose is to handle mutual access to a shared object. Give both instances of new B call the a1, and it will run as you expect. To implement shared handling on the class itself, you need to handle the threading at a higher level including using locks.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
multithreading, synchronized

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:32 PM.


Advertisement
Log in to turn off these ads.