के रूप में अन्य लोगों ने बताया है, यह सिर्फ एक 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()
क्या आपने प्रलेखन की जांच की? –
हां, लेकिन मुझे कुछ याद आ गया होगा। – vicky96