通过为单个线程分配唯一 ID 来提高输出的清晰度

Increase clarity of output by assigning unique ID to individual thread

我是 Java 的新手,正在尝试学习高级并发的概念。我在 Java Oracle 教程中看到了下面的代码。

但是,当我 运行 代码时,输​​出没有指出它来自哪个线程。所以我的问题是,我怎样才能输出线程 ID(即以某种方式为单个线程分配一个唯一的 ID),这样我才能真正确定哪个线程在做什么。

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;

public class Safelock {
    static class Friend {
        private final String name;
        private final Lock lock = new ReentrantLock();

        public Friend(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public boolean impendingBow(Friend bower) {
            Boolean myLock = false;
            Boolean yourLock = false;
            try {
                myLock = lock.tryLock();
                yourLock = bower.lock.tryLock();
            } finally {
                if (! (myLock && yourLock)) {
                    if (myLock) {
                        lock.unlock();
                    }
                    if (yourLock) {
                        bower.lock.unlock();
                    }
                }
            }
            return myLock && yourLock;
        }

        public void bow(Friend bower) {
            if (impendingBow(bower)) {
                try {
                    System.out.format("%s: %s has"
                        + " bowed to me!%n", 
                        this.name, bower.getName());
                    bower.bowBack(this);
                } finally {
                    lock.unlock();
                    bower.lock.unlock();
                }
            } else {
                System.out.format("%s: %s started"
                    + " to bow to me, but saw that"
                    + " I was already bowing to"
                    + " him.%n",
                    this.name, bower.getName());
            }
        }

        public void bowBack(Friend bower) {
            System.out.format("%s: %s has" +
                " bowed back to me!%n",
                this.name, bower.getName());
        }
    }

    static class BowLoop implements Runnable {
        private Friend bower;
        private Friend bowee;

        public BowLoop(Friend bower, Friend bowee) {
            this.bower = bower;
            this.bowee = bowee;
        }

        public void run() {
            Random random = new Random();
            for (;;) {
                try {
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {}
                bowee.bow(bower);
            }
        }
    }


    public static void main(String[] args) {
        final Friend alphonse =
            new Friend("Alphonse");
        final Friend gaston =
            new Friend("Gaston");
        new Thread(new BowLoop(alphonse, gaston)).start();
        new Thread(new BowLoop(gaston, alphonse)).start();
    }
}

是你can.Use在构造函数下面创建线程

Thread(Runnable target, String name)

然后你可以通过调用

打印它的名字
Thread.getCurrentThread().getName();

为了生成唯一名称,如果您想打印 ID,您可以使用 choice.Or 的任何算法 我认为 getId() 中也有一种方法 Thread class.

您可以使用"Thread.currentThread().getName()"打印当前线程的名称。所以你的 sysouts 会变成:

System.out.format(Thread.currentThread().getName() + "%s: %s has" +
            " bowed back to me!%n",
            this.name, bower.getName());

如果你想给线程起一个特殊的名字,那么你可以在创建的时候给它们命名:

new Thread(new BowLoop(alphonse, gaston), "MyThread1").start();
new Thread(new BowLoop(gaston, alphonse), "Mythread2").start();**