wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multithreading" Watch "Multithreading" New topic
Author

Multithreading

priyanaka jaiswal
Ranch Hand

Joined: Jun 03, 2011
Posts: 79
Hi all,



What will happen if two thread try to access two synchronized method using same object?

What will happen two thread t1 and t2 are trying to access one synchronized method and other unsynchronized method using same object?


Thanks in advance....
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

priyanaka jaiswal wrote:What will happen if two thread try to access two synchronized method using same object?

What will happen two thread t1 and t2 are trying to access one synchronized method and other unsynchronized method using same object?

Then those methods will get invoked

I didn't understand your exact question. If two threads are trying to access two different methods, then synchronized or not, those methods will get invoked. What do you exactly want to know? Can you give some code to illustrate your question?


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

priyanaka jaiswal wrote:Hi all,



What will happen if two thread try to access two synchronized method using same object?


If T1 and T2 are both trying to call X.foo(), and foo() is synchronized, and both threads are referring to the same X object, then one thread will get there first and the other one will block until the first one has completed the method or called X.wait().
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Synchronization is either object level or class level.

For example:



Now as per your first and second question:
If T1 calls sameObj.m1() then T2 can't call sameObj.m2().
But T2 can call other methods like m3, m4, m5, m6 at the same time.

Further details:
1. If T1 calls TestSync.m3() then T2 can't call TestSync.m4().
But T2 can call other methods like m1, m2, m5, m6 at the same time.

2. If T1 calls sameObj.m5() then T2 can call all the other methods at the same time.

Hope that clears your doubt.



Thanks and Regards,
~Sujoy
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sujoy Choudhury wrote:Synchronization is either object level or class level.


False.

Synchronization always does exactly the same thing: It obtains the lock for an object (waiting until the current holder releases it if it's already being held), and prevents other threads from obtaining that lock until the current thread releases it.

There is no "class level" vs. "object level" synchronization. What you are calling "class level" just obtains a different object's lock--the Class object's lock.
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

You are right Jeff.
By level I meant two types of object locking.
I should be more careful about the language .
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sujoy Choudhury wrote:You are right Jeff.
By level I meant two types of object locking.
I should be more careful about the language .


But it's not two types. They are exactly the same thing. The only difference is which object we're syncing on. It's just synchronized (x) { ... } instead of synchronized (y) { ... }. There's no behavioral differences at all.
Sujoy Choudhury
Ranch Hand

Joined: Sep 17, 2008
Posts: 136

Okay. Then I have to understand this more in depth.
In my last example, T1 and T2 can access m1() and m3() simultaneously as they would be accessing different locks here.
So, are not Class object and instance object two different things?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sujoy Choudhury wrote:Okay. Then I have to understand this more in depth.
In my last example, T1 and T2 can access m1() and m3() simultaneously as they would be accessing different locks here.
So, are not Class object and instance object two different things?


No. In terms of syncing and locking a Class object it just an object like any other. It's an instance of java.lang.Class. It doesn't matter if we lock an an instance of java.lang.Class or java.lang.Object or java.util.ArrayList or com.mycompany.MyClass. They're all just objects. There's no such distinction a "class instance" vs. "object instance."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multithreading