• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static synchronized / synchronized Threads

 
kri shan
Ranch Hand
Posts: 1453
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Garik Ustinov
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic