Multiple threads passing an object reference to a static helper method

I am just starting out in Java and stumbled upon multithreaded applications. I know this question is similar to some posts here, but I could not find a better answer for my request. Basically, I want to pass an object to a static method, and the method will simply return a result based on the values ​​/ properties of the object. For each call, I create a new instance of the object, and there is no chance that I will modify the object inside the method. Now, to my question, will the JVM create a new instance of the static method and its local variables on the stack (excluding the object, as it will be on the heap) for each call by multiple threads? For a clear idea of ​​what I want to achieve, here is my code:

TestConcurrent.java

import classes.Player; public class TestConcurrent { private static int method(Player player) { int y = (player.getPoints() * 10) + 1; try { Thread.sleep(1000); } catch (InterruptedException e) {} return ++y; } public static void main(String[] args) throws Exception { // Create 100 threads for(int i=1;i<=100;i++) { final int j = i; // Create a new Thread new Thread() { public void run() { // Create a new instance of the Player class Player player = new Player(j,j,"FirstName" + j, "LastName" + j); // Call static method() and pass a new instance of Player class System.out.println("Thread " + j + ": " + TestConcurrent.method(player)); // Check the values of the Player class after the call to the static method() System.out.println("Player" + player.getAcctId() + " : Points=" + player.getPoints() + " Name=" + player.getFirstName() + " " + player.getLastName()); } }.start(); } } } 

Player.java

 package classes; public class Player { private int acctId, points; String firstName, lastName; public Player(int acctId, int points, String firstName, String lastName) { this.acctId = acctId; this.points = points; this.firstName = firstName; this.lastName = lastName; } public int getAcctId() { return acctId; } public void setAcctId(int acctId) { this.acctId = acctId; } public int getPoints() { return points; } public void setPoints(int points) { this.points = points; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } 

OUTPUT:

Since I did not set a synchronized keyword, the output will be different every time, and it looks something like this: (the output is correct, and this is exactly what I expect, I just want to clarify that I am in the right path, since I do not want use synchronization, as this will slow down the process, because each thread will have to wait for the completion of the other thread before it can call the static method)

 Thread 2: 22 Player8 : Points=8 Name=FirstName8 LastName8 Thread 22: 222 Thread 26: 262 Thread 23: 232 Player23 : Points=23 Name=FirstName23 LastName23 Thread 21: 212 Player21 : Points=21 Name=FirstName21 LastName21 Thread 25: 252 Player25 : Points=25 Name=FirstName25 LastName25 Thread 20: 202 Thread 19: 192 Thread 24: 242 Player24 : Points=24 Name=FirstName24 LastName24 Player9 : Points=9 Name=FirstName9 LastName9 Thread 28: 282 
+7
source share
2 answers

Will the JVM create a new instance of the static method and its local variables on the stack (excluding the object, as it will be on the heap) for each call by multiple threads?

Yes, that's for sure.

If the static method applies only to local variables, it is automatically thread safe. (In fact, this applies to non-stationary methods.)

Generally speaking, I would say that you should avoid static if possible. In general, this makes the code more difficult to verify and justify, because static members are global in some ways.

+4
source

static not a problem, only static variables will be shared between threads.

So, two threads calling

 public static int sum(int a, int b) { int tmp = a + b; return tmp; } 

will not run into problems.

 static int tmp; public static int sum(int a, int b) { tmp = a + b; return tmp; } 

Multithreading failed because one thread may overwrite another tmp value.

Local variables, even in static methods, are still local and thus safe.

Using static methods is good. It emphasizes that the method does not require access to object variables. Using static mutable variables is error prone, avoid it at all costs (and use constant synchronization if you need to access the variable).

+4
source

All Articles