पर ऑब्जेक्ट संदर्भ पास करने वाले एकाधिक थ्रेड्स मैं जावा में बस शुरुआत कर रहा हूं और बहु-थ्रेडेड अनुप्रयोगों पर ठोकर खा रहा हूं। मुझे पता है कि यह प्रश्न यहां कुछ पोस्ट के समान है लेकिन मुझे अपनी क्वेरी के लिए बेहतर उत्तर नहीं मिला। असल में, मैं किसी ऑब्जेक्ट को एक स्थिर विधि में पास करना चाहता हूं और विधि ऑब्जेक्ट के मानों/गुणों के आधार पर केवल आउटपुट लौटाएगी। प्रत्येक कॉल के लिए, मैं ऑब्जेक्ट का एक नया उदाहरण बना रहा हूं और इस तरीके के अंदर ऑब्जेक्ट को संशोधित करने का कोई मौका नहीं है। अब, मेरा सवाल यह है कि, क्या JVM कई थ्रेड द्वारा प्रत्येक कॉल के लिए स्टैक में स्थिर विधि और उसके स्थानीय चर का एक नया उदाहरण बना देगा (ऑब्जेक्ट को छोड़कर यह ढेर पर होगा)? मैं क्या हासिल करना चाहते का एक स्पष्ट विचार के लिए, यहाँ मेरी कोड है:स्टैटिक हेल्पर विधि
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;
}
}
उत्पादन:
चूंकि मैंने सिंक्रनाइज़ किए गए कीवर्ड को नहीं रखा है, वें ई आउटपुट हर बार अलग होगा और यह निम्न के जैसा दिखता है: (आउटपुट सही है और यह वही है जो मैं उम्मीद कर रहा हूं, मैं सिर्फ यह स्पष्ट करना चाहता हूं कि मैं सही रास्ते पर हूं क्योंकि मैं सिंक्रनाइज़ेशन का उपयोग नहीं करना चाहता इसे नीचे प्रक्रिया धीमी हो जाएगी क्योंकि प्रत्येक धागा खत्म करने के लिए अन्य थ्रेड के लिए प्रतीक्षा करने के लिए इससे पहले कि यह स्थिर विधि कॉल कर सकते हैं होगा)
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 - महान पहला सवाल! StackOverflow में आपका स्वागत है! – jmort253