Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when two threads call the same static method at the same time?

Tags:

What happens when two threads call the same static method at the same time? For example:

public static String someMethod(){      //some logic, can take about 1 second to process      return new String(result); } 

First thread calls someMethod() now. Second thread calls someMethod() 0.5 seconds from now (first thread still processing the data).

I know that someMethod() can be synchronized. But what will happen if it's not synchronized?

like image 460
Ernestas Gruodis Avatar asked Feb 18 '14 20:02

Ernestas Gruodis


People also ask

Can a static method be called by multiple threads?

Many threads/callers can be calling the same static method at the same time. They don't wait on each other. If you wanted to limit the calls so they *couldn't* run in parallel, you'd need to add the word "synchronized" to the method. But by default, methods do what you want and there is no bottleneck.

What will happen if multiple threads accessing the same resource?

Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.

Can we use static method in multithreading Java?

Static Synchronized method is also a method of synchronizing a method in java such that no two threads can act simultaneously static upon the synchronized method. The only difference is by using Static Synchronized. We are attaining a class-level lock such that only one thread will operate on the method.

Can two threads execute static and non static methods concurrently?

Since both the objects are different hence both synchronized static and non-static method will not block each other in case of multi-threading. Both the methods will execute simultaneously. Show activity on this post. Yes..


2 Answers

When a method is called, the JVM creates a stack frame for the call in the executing thread. This frame contains all of the local variables declared in the method. In the case of any method, static or otherwise, that doesn't access fields, each execution proceeds completely independently on each thread. If the method uses parameters in its calculation, these parameters are also located in the stack frame, and multiple calls don't interfere with each other.

like image 103
chrylis -cautiouslyoptimistic- Avatar answered Dec 31 '22 16:12

chrylis -cautiouslyoptimistic-


It depends on whether your method changes outside state.

static long i = 0l; public static String someMethod(){     String accm = "";     for(;i < Integer.MAX_VALUE*20/*Just to make sure word tearing occurs*/; i++)         accm += i     return accm; } 

Will cause problems:

  • Longs are not guaranteed to be set atomically, so they might be 'torn' (Spec)
  • ++ is not an atomic operation. It is exactly the same as {int n = i; i = i + 1; return n}
    • i = i + 1 is also not atomic, if it changes in the middle, some values will be repeated
    • The return of n might be stale

But if i is a local variable, there will be no problems. As long as any outside state is guaranteed to be immutable while it is being read, there can never be any problems.

like image 26
user60561 Avatar answered Dec 31 '22 17:12

user60561