Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a thread call a non-synchronized instance method of an Object when a synchronized  RSS feed

 
Priyanka Dandekar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a thread call a non-synchronized instance method of an Object when a synchronized method is being executed ?

I am having difficulty understanding this synchronization stuff.

Assume if a Class has two methods
- method1 is synchronized instance method
- method2 is a regualr instance method

So in this scenario, can there be two threads calling these two methods in parallel for the same instance?
 
Sachin Joshi
Ranch Hand
Posts: 83
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you are having problem understanding the core fundamental of synchronization. You should start writing code for testing all these scenarios.

To answer your question.

Yes, a Non synchronized method can always be called without any problem. In fact Java does not do any check for a non-synchronized method. The Lock object check is performed only for synchronized methods/blocks. In case the method is not declared synchronized Jave will call even if you are playing with shared data. So you have to be careful while doing such thing. The decision of declaring a method as synchronized has to be based on critical section access. If your method does not access a critical section (shared resource or data structure) it need not be declared synchronized.

See the Example Code which demonstrate the same.
 
Rohith Kumar Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Non-Synchronized method can always be called without any problem.
Cheers,
Rohit
[ August 27, 2008: Message edited by: Bear Bibeault ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!