GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Synchronization on Threads in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization on Threads in Java" Watch "Synchronization on Threads in Java" New topic
Author

Synchronization on Threads in Java

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Hi Ranchers,
I have a few clarifications.


-----------------------------------------------------------------------------------------------------------------------------------


Need your inputs/valuable suggestions on above scenarios..please help me to resolve this.


When The Going Gets Tougher,The Tougher gets Going
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Scenario 1:
Can Thread 2 access the synchronized methodB?
Yes, with this limitation: IF Thread 1 and Thread 2 are accessing the same instance of ClassA, then Thread2 can only access methodB when Thread1 is NOT accessing methodA. That is, if Thread1 is executing code in methodA, then Thread2 can not access code in methodB from the same instance of the class.

Scenarion2:
Can Thread 2 access the non-synchronized methodB?
Yes, with no limitation as to when.


Steve
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Thanks Steve for your impeccable replies.

My next question is how do i complete the code for Scenario 1 and Scenario 2 i.e how should my Public static main method look like.I'm unable to figure that part for Scenario 1 and Scenario 2
Help provided will be highly appreciated.

Best Regards
Deepak lal
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Well, I don't know what the main() should look like. What do you want to do?

At the very least, I guess, you would need to:

1) Make one instance of the class A.
2) Make two Threads (or Runnables) which access the appropriate method in the class.
3) Execute both Threads (or Runnables in Threads)
4) Repeat 1-3 for class B
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Deepak Lal wrote:
how should my Public static main method look like.I'm unable to figure that part for Scenario 1 and Scenario 2


How could anybody know this Deepak,untill and unless we know what you want to do with that


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

1 more things your scenerio1 will look like this for compiler..

public class A1{
public void MethodA(){
synchronized(this){
System.out.println("In synchronized MethodA"); //Accessed by Thread (1)
}
}

//in the same code below here i have another synchronized method(MethodB)
public void MethodB(){
synchronized(this){
System.out.println("In synchronized MethodB"); //Can Thread (2) access this synchronized methodB ?
}
}
}


synchronized(this)//will occupy a lock on the current object montor..
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Deepak,

I don't know if this is what you want to do, but maybe this code might help as an example:



So, the same instance of the A class is referenced by each instance of the classes that implement runnable. You can't tell whether threadOne or threadTwo will get the lock on that instance of A, but you can say with certainty that once one of the Threads gets the lock then the other Thread will be blocked until the first Thread's run() method has completed.

I hope that helps.

cheers

Joe


OCPJP 85%
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Thank you,
I have another scenario



How should we synchronize the read and write method for static variable integer i.?Example provided will be helpful.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Joe Lemmer,Can you pleasse help me with scenario 3 as stated above.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Deepak Lal wrote:
How should we synchronize the read and write method for static variable integer i.?Example provided will be helpful.

Synchronization is for objects! Not to primitives!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Abimaran Kugathasan wrote:
Deepak Lal wrote:
How should we synchronize the read and write method for static variable integer i.?Example provided will be helpful.

Synchronization is for objects! Not to primitives!


That is incorrect! You must protect primitives with synchronization as well, you just can't gain a lock on the primitive itself so you need to lock on something else.

Abimaran, this situation appears special because the data you want to protect is static, and the methods are not. In reality, the situation is the same as any other synchronization problem. You have to lock on an Object which is available in the same contexts as the data you want to protect. For static content that generally means an Object stored in a static reference. A common one to use is the Class' class Object since it is the same thing used by static synchronized methods. So in you scenario3 you might have:


Note that you should make the variable 'i' private so you can not access it directly from an outside source - you have to access it through one of the protected accessor methods.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Steve you just made my day...Its correct...Wow i have no words for this answer.Thanks Steve...!!!




@Steve and @Ranchers:
Where can i find such nice examples on Core Java and MultiThreading application.Please suggest some good book with examples on real time multithreaded examples.Please advise ranchers
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

@Ranchers, @Steve I'm back with another scenario.see below



How should i apply synchronization for Scenario 4 such that read and write method are executed by 2 different threads. (as demonstrated by Joe lemmer in one of the posts) ?Sample code provided will be appreciated.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Rather than post code for you again ('cause that doesn't seem to make it stick for you), why don't you post code you think will work and we can give you pointers?

<hint>What is different between scenario 3 and scenario 4?</hint>
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

In the scenario 4 we have applied "static" to the read and write methods.i dont know whats the answer.


Please let us know the answer Steve.Your help provided will be appreciated.

@Steve.I want help/guidelines/Website links for Multi Threading concepts.Please could you share it with me.
Help provided will be appreciated.

Best Regards
Deepak lal
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Steve Luke wrote:
Abimaran Kugathasan wrote:
Deepak Lal wrote:
How should we synchronize the read and write method for static variable integer i.?Example provided will be helpful.

Synchronization is for objects! Not to primitives!


That is incorrect! You must protect primitives with synchronization as well, you just can't gain a lock on the primitive itself so you need to lock on something else.

Abimaran, this situation appears special ....

I mean there, we can't directly synchronize the primitives like we use in objects!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Deepak Lal wrote:@Ranchers, @Steve I'm back with another scenario.see below



How should i apply synchronization for Scenario 4 such that read and write method are executed by 2 different threads. (as demonstrated by Joe lemmer in one of the posts) ?Sample code provided will be appreciated.

Could you please explain a bit more? I couldn't understand you question!
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

@ Abimaran Kugathasan, Scenario 4 is a different scenario...are you sure of your answer

@Steve,Can you please comment on this now.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Deepak,

Think it through! I had said in a previous post:
You have to lock on an Object which is available in the same contexts as the data you want to protect. For static content that generally means an Object stored in a static reference.


What do you think you have to synchronize on? What is the difference between Scenario 3 and Scenario 4? What does that mean to your synchronization scheme? Think it through, show us what you come up with, and we can comment.

Note to others: I will watch this thread, and if anyone else posts code for Deepak I will hide it. Deepak has to start writing his own code rather than getting others to do it for him. Please let him work this through.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I mean, we can't directly synchronize on primitives.

But, we can make the int variable as Thread-safe in other ways!

PS: This just for explanation, not for the answer for the question which Deepak asked!
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Please see the code for scenario 4,
For static methods too like static variables declared as an int,it is similar to Scenario 3 (For static content that generally means an Object stored in a static reference.)

@STEVE, please go ahead and correct my understanding if im wrong and add suitable comments to this posts.




:-) Deepak Lal

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Hi Deepak, that is correct. Now for a shortcut - You can use

You get the same exact behavior - it is the same thing as you wrote, just a little more compact. There are trade-offs, but for what you wrote they are the same.
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

Thank you Steve

Steve,I needed the latest multi threading tutorial in Core Java that covers the new chapters in Thread,So can you suggest me a good tutorial with ample explanation of examples.Or can you share a pdf which you use for your reference by mailing it across to my mail id.Please reply Steve

Regards
Deepak Lal
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Hi Deepak, the Concurrency chapter of the Java Tutorial is an okay starter: http://download.oracle.com/javase/tutorial/essential/concurrency/. For more details it is best to get a good book (like Java Concurrency In Practice: http://www.javaconcurrencyinpractice.com/ ...).
 
GeeCON Prague 2014
 
subject: Synchronization on Threads in Java