के कारण चलने वाले थ्रेड में देरी निम्न कोड में, यदि मैं लूप के अंदर सिसआउट कथन का उपयोग करता हूं तो कोड निष्पादित होने के बाद लूप के अंदर निष्पादित करता है और चला जाता है लेकिन अगर मैं अंदर sysout कथन का उपयोग नहीं करता लूप तब तत्काल लूप चला जाता है अगर स्थिति में अंदर प्रवेश किए बिना भी भले ही अगर स्थिति संतुष्ट हो .. क्या कोई भी कृपया इसके लिए सही कारण जानने में मेरी मदद कर सकता है। बस एक sysout कथन अगर शर्त सच बनने के लिए बनाते हैं। ऐसा क्यों है?system.out.println कथन
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo(String name){
threadName = name;
System.out.println("Creating " + threadName);
}
public void run() {
System.out.println("Running " + threadName);
for(;;)
{
//Output 1: without this sysout statement.
//Output 2: After uncommenting this sysout statement
//System.out.println(Thread.currentThread().isInterrupted());
if(TestThread.i>3)
{
try {
for(int j = 4; j > 0; j--) {
System.out.println("Thread: " + threadName + ", " + j);
}
} catch (Exception e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
}
}
public void start()
{
System.out.println("Starting " + threadName);
if (t == null)
{
t = new Thread (this, threadName);
t.start();
}
}
}
public class TestThread {
static int i=0;
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo("Thread-1");
R1.start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i+=4;
System.out.println(i);
}
}
अनंत लूप में sysout बयान बिना आउटपुट: - -: अनंत लूप में sysout बयान के साथ enter image description here
आउटपुट: - enter image description here
कोड के रूप में इस प्रकार है
इम वाला लगता है कि आप अस्थिर मुद्दों में चल रहे हैं। स्थिर int i = 0 बनाने का प्रयास करें; स्थैतिक अस्थिर int i = 0; यदि व्यवहार अब एक ही बीमार है तो एक स्पष्टीकरण के साथ एक जवाब डाल दिया। –
@Aaron यह एक अनंत लूप है, इसलिए वे इसे लगातार निष्पादित करते रहते हैं, है ना? संपादित करें: हारून ने इमारत छोड़ दी। –
@ हेनक डी बोयर, मैंने अस्थिरता का उपयोग करने की कोशिश की और यह ठीक काम कर रहा है। लेकिन क्या आप समझा सकते हैं कि sysout कथन कौन से परिवर्तन करता है जो कोड को अस्थिर कीवर्ड का उपयोग किये बिना भी काम करने देता है। –