2015-05-29 9 views
6

जब मैं इस कार्यक्रम को चलाने केजावा में डिफ़ॉल्ट नया धागा नाम कैसे दिया जाता है?

public class Fabric extends Thread { 
    public static void main(String[] args) { 
     Thread t1 = new Thread(new Fabric()); 
     Thread t2 = new Thread(new Fabric()); 
     Thread t3 = new Thread(new Fabric()); 
     t1.start(); 
     t2.start(); 
     t3.start(); 
    } 
    public void run() { 
     for(int i = 0; i < 2; i++) 
     System.out.print(Thread.currentThread().getName() + " "); 
    } 
} 

मैं उत्पादन

Thread-1 Thread-5 Thread-5 Thread-3 Thread-1 Thread-3 

कोई विशिष्ट कारण धागे विषम संख्या के साथ नाम दिए गए हैं है - 1, 3, 5 ... या यह है अप्रत्याशित?

+0

क्या आपने प्रलेखन की जांच की? –

+1

हां, लेकिन मुझे कुछ याद आ गया होगा। – vicky96

उत्तर

12
new Thread(new Fabric()); 

Fabric के बाद से एक धागा है, तो आप यहां 2 धागे :)

JDK8 कोड बनाया:

/* For autonumbering anonymous threads. */ 
private static int threadInitNumber; 
private static synchronized int nextThreadNum() { 
    return threadInitNumber++; 
} 
+0

क्या इसका मतलब यह है कि जब भी JVM 'थ्रेड' बनाता है, तो यह वैश्विक काउंटर को बढ़ाता है? – CKing

+3

@ चेतनकिंजर - जो समझ में आएगा। यह और क्या कर सकता है? एक पशु नाम उठाओ? :) – ZhongYu

+1

@ खेतान किंगर, हां, जब तक थ्रेड को निर्माण पर एक विशिष्ट नाम नहीं दिया जाता है। – copeg

6

थ्रेड नाम पर डिफ़ॉल्ट अंकीय मान एक संख्या बढ़ मूल्य जब तक है थ्रेड बनाने के दौरान नाम निर्दिष्ट किया गया है। FabricThread बढ़ाता है, और आप Fabric इंस्टेंस को एक और थ्रेड बनाने के लिए पास कर रहे हैं - इस प्रकार आंतरिक थ्रेड काउंटर को दो बार बढ़ाया जाता है क्योंकि प्रक्रिया के दौरान 2 धागे बनाए जाते हैं।

2

यदि आप नीचे दिए गए कार्यक्रम को बदलते हैं तो आपको अनुक्रम में थ्रेड नंबरिंग मिल जाएगी।

public class Fabric extends Thread { 
    public static void main(String[] args) { 
     Thread t1 = new Fabric(); 
     Thread t2 = new Fabric(); 
     Thread t3 = new Fabric(); 
     t1.start(); 
     t2.start(); 
     t3.start(); 
    } 
    public void run() { 
     for(int i = 0; i < 2; i++) 
      System.out.print(Thread.currentThread().getName() + " "); 
    } 
} 

और आउटपुट

Thread-0 Thread-2 Thread-2 Thread-1 Thread-0 Thread-1 
0

के रूप में अन्य लोगों ने बताया है, यह सिर्फ एक incrementing काउंटर है।

कारण आपकी लॉग फ़ाइल आपके थ्रेड नामों को क्रम में मुद्रित नहीं करती है, इसलिए जावा प्रारंभिक धागे के निष्पादन के आदेश को गारेन नहीं करता है।

यदि आप आदेश को मजबूर करना चाहते हैं, तो आपको धागे को एक दूसरे के लिए इंतजार करना होगा। ऐसा करने का एक संभावित तरीका, CountDownLatch का उपयोग कर रहा है।

private static CountDownLatch latch; 

public static void main(String[] args) throws InterruptedException { 
    Thread t1 = new Thread(new Fabric()); 
    Thread t2 = new Thread(new Fabric()); 
    Thread t3 = new Thread(new Fabric()); 

    // the countdown starts at 1. 
    latch = new CountDownLatch(1); 
    t1.start(); 
    // the thread will wait till the countdown reaches 0. 
    latch.await(); 

    latch = new CountDownLatch(1); 
    t2.start(); 
    latch.await(); 

    latch = new CountDownLatch(1); 
    t3.start(); 
    latch.await(); 
} 
public void run() { 
    for(int i = 0; i < 2; i++) 
     System.out.print(Thread.currentThread().getName()); 

    // thread is done: set counter to 0. 
    latch.countDown(); 
} 

दूसरी ओर, कुछ कक्षाएं एक ThreadFactory का उपयोग धागा नाम आवंटित करने के लिए। (उदाहरण के लिए ScheduledThreadPoolExecutor)। उदाहरण के लिए DefaultThreadFactory इसके धागे नाम निम्नानुसार बनाता है:

String namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; 
String threadName = namePrefix + threadNumber.getAndIncrement() 
संबंधित मुद्दे