• 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

static synchronized / synchronized Threads

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scenario1
------------

Public class Test {

static synchronized int test1() {

}

synchronized int test2() {

}
}

Whether both test1 and test2 will acquire the lock at the same time or after Thread1(calls test1) releases the lock only Thread2(calls test2) will get the lock?

scenario2
-----------
Public class Test {

synchronized int test1() {

}

synchronized int test2() {

}
}

Whether both test1 and test2 will acquire the lock at the same time or after Thread1(calls test1) releases the lock only Thread2(calls test2) will get the lock?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's pretty easy (and quick) to write a program that will test this. Why don't you take a shot at it, and report back what you see?

Henry
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I recall correctly, static and non-static synchronized methods are synchronized on different object, in the latter case it's "this", while in the former it's a class. So these two methods are not really synchronized against each other whatsoever.
Speaking of Scenario 2, these methods are synchronized on "this" so there's no way for them to acquire the lock at the same time a someone will have to wait.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic