java中默认的新线程名称是如何给出的?

java中默认的新线程名称是如何给出的?,java,multithreading,Java,Multithreading,当我运行这个程序时 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();

当我运行这个程序时

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() + " ");
    }
}
有没有什么特别的原因可以解释为什么线程被命名为奇数-1,3,5。。。还是不可预测

new Thread(new Fabric());
由于
Fabric
是一个线程,因此您在此处创建了两个线程:)

JDK8代码:

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

除非在创建线程时指定了名称,否则线程名称中的默认数值为递增值<代码>结构扩展了
线程
,您正在传递
结构
实例以创建另一个线程-因此,内部线程计数器的增量是进程中创建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() + " ");
    }
}

正如其他人指出的,它只是一个递增计数器

日志文件没有按顺序打印线程名称的原因是java没有保证已启动线程的执行顺序

如果要强制执行该命令,则必须让线程彼此等待。一种可能的方法是使用
倒计时闩锁

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

你检查过文档了吗?是的,但我可能遗漏了什么。这是否意味着每次JVM创建
线程时,它都会增加一个全局计数器?@ChetanKinger-这很有意义。它还能做什么?选择一个动物名字?:)@Chetan Kinger,是的,除非线程在构造时被赋予一个特定的名称。动物名称会很有趣。@ChetanKinger-请参阅“真正奇怪的链接名称是什么?”“因为创建了两个线程对象”。在调用
start()
之前不会创建线程本身,因此只创建一个线程。@新手我试图澄清
thread
对象和实际运行的CPU线程之间的区别。
new Fabric()
new Thread()
都创建了
Thread
对象,但是
start()
只在其中一个对象上调用,所以一旦其中一个对象创建了一个CPU线程。@Andreas如果我遗漏了什么,这是出于好奇。谢谢你。
Thread-0 Thread-2 Thread-2 Thread-1 Thread-0 Thread-1
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();
}
String namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-";
String threadName = namePrefix + threadNumber.getAndIncrement()