Sono solo un principiante in Java e sono incappato in applicazioni multi-thread. So che questa domanda è simile ad alcuni post qui, ma non ho trovato una risposta migliore per la mia richiesta. Fondamentalmente, voglio passare un oggetto a un metodo statico e il metodo restituirà solo un output basato sui valori/proprietà dell'oggetto. Per ogni chiamata, sto creando una nuova istanza dell'oggetto e non c'è alcuna possibilità che io modifichi l'oggetto all'interno del metodo. Ora, la mia domanda è, JVM creerà una nuova istanza del metodo statico e le sue variabili locali nello Stack (escludendo l'oggetto come sarà nell'Heap) per ogni chiamata da più thread? Per una visione chiara di ciò che voglio raggiungere, qui è il mio codice:Più thread che passano un riferimento di oggetto al metodo di supporto statico
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;
}
}
USCITA:
Poiché non ho inserito una parola chiave sincronizzata, L'output sarà diverso ogni volta ed è simile al seguente: (l'output è corretto ed è esattamente quello che mi aspetto, voglio solo chiarire che sono sulla strada giusta dal momento che non voglio usare la sincronizzazione come sarà rallentare il processo perché ogni thread dovrà attendere che l'altro filo per terminare prima di chiamare il metodo statico)
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
+1 - Ottima prima domanda! Benvenuto in StackOverflow! – jmort253