top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: Can a Thread call a non-synchronized instance method of an object when a synchronized method is being executed?

+1 vote
459 views
Java: Can a Thread call a non-synchronized instance method of an object when a synchronized method is being executed?
posted Sep 4, 2013 by Vinay Shukla

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes

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.In case the method is not declared synchronized java 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 it need not to be declared synchronized.

answer Sep 4, 2013 by Arvind Singh
Similar Questions
+2 votes
public class ServletClassName extends HttpServlet {
//declare instance variables for the servlet
int  instanceVar ; // not thread−safe
// overriding the Generic Servlet.init ( )
@Override
public void init ( ) throws ServletException {
// initialize instance variables, etc.
instanceVar = 0 ;
}
. . .
}

Per each page request, in the service() method (which eventually will be either doGet(), doPost(), or whatever),
you can manipulate instance variables.

Write a Servlet that counts the total number of visits (how many times it's being visited from all clients). The Servlet should display the following information

• Total Page Visits:
• Client Remote Address:
• Client Host Address: <client_fully_qualied_url__or__client_IP_address>

The last two can be obtained from the HttpServletRequest, Refer to the Java EE API

then how can re-do these using the Java API for HttpSession.

...