• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Synchronization on Threads in Java

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
I have a few clarifications.


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


Need your inputs/valuable suggestions on above scenarios..please help me to resolve this.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joe Lemmer,Can you pleasse help me with scenario 3 as stated above.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/ ...).
 
Destroy anything that stands in your way. Except this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic