Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent access to static methods

I have a static method with the following signature:

public static List<ResultObjects> processRequest(RequestObject req){   // process the request object and return the results. } 

What happens when there are multiple calls made to the above method concurrently? Will the requests be handled concurrently or one after the other?

like image 934
user1226058 Avatar asked Jan 02 '13 17:01

user1226058


People also ask

Can static method be accessed by multiple threads?

accessing the code is no problem, static methods can be called with multiple threads. It depends on how it is programmed in the method, if the code is not thread safe, it will cause problems.

Why static methods have several restrictions?

Methods declared as static have several restrictions: They can only directly call other static methods. They can only directly access static data. They cannot refer to this or super in any way.

How are static methods accessed?

Static methods can be accessed without having to create a new object. A static method can only use and call other static methods or static data members. It is usually used to operate on input arguments (which can always accept), perform calculation and return value.

Can we call static method multiple times?

@SangitGurung it's not. The methods are executed in separate activation frames.

How do I synchronize a thread with a static method?

The .NET class library provides a number of classes for synchronizing threads. See Overview of Synchronization Primitives. You can use the Monitor class or a compiler keyword to synchronize blocks of code, instance methods, and static methods. There is no support for synchronized static fields.

How many threads are allowed to access the methods and fields?

Multiple threads are allowed to access the methods and fields, but only a single thread is allowed at any one time. Learn about .NET thread synchronization primitives used to synchronize access to a shared resource or control thread interaction See how unhandled exceptions are handled in .NET.

How to synchronize all instance methods and fields in a contextboundobject?

In .NET Framework and Xamarin applications only, you can use the SynchronizationAttribute on any ContextBoundObject to synchronize all instance methods and fields. All objects in the same context domain share the same lock. Multiple threads are allowed to access the methods and fields, but only a single thread is allowed at any one time.

What is the need of synchronizing data for multithreading?

Synchronizing data for multithreading. When multiple threads can make calls to the properties and methods of a single object, it is critical that those calls be synchronized. Otherwise one thread might interrupt what another thread is doing, and the object could be left in an invalid state.


1 Answers

Answering exactly your question:

  1. Method will be executed concurrently (multiple times in the same time if you have several threads).
  2. Requests will be handled concurrently.

You need to add the synchronized modifier if you are working with objects that require concurrent access.

like image 132
Andremoniy Avatar answered Sep 21 '22 02:09

Andremoniy