• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multithreading

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Ranch Hand
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Jeff.
By level I meant two types of object locking.
I should be more careful about the language .
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 136
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic