How is a new stream name set in java?

When I run this program

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() + " "); } } 

I get a conclusion

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

Is there any specific reason why the threads are given names with odd numbers - 1, 3, 5 ... Or is this unpredictable?

+6
source share
4 answers
 new Thread(new Fabric()); 

Since Fabric is Thread, you created 2 threads here :)

JDK8 Code:

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

The default numeric value in the Thread name is an increased value, unless the name is specified when creating the Thread. Fabric extends Thread , and you pass the Fabric instance to create another thread - this way, the internal thread counter doubles when 2 threads are created during the process.

+6
source

If you change the program as shown below, you will get the thread numbering in sequence.

  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() + " "); } } 

and the way out is

 Thread-0 Thread-2 Thread-2 Thread-1 Thread-0 Thread-1 
+2
source

As others have pointed out, this is just an incrementing counter.

The reason your log file does not print your thread names in order is because java does not guarantee the execution order of running threads .

If you want to force an order, you need to make the threads wait for each other. A possible way to do this is using a 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(); } 

On the other hand, some classes use ThreadFactory to assign thread names. (e.g. a ScheduledThreadPoolExecutor ). For example, DefaultThreadFactory creates its thread names as follows:

 String namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; String threadName = namePrefix + threadNumber.getAndIncrement() 
0
source

Source: https://habr.com/ru/post/988184/


All Articles