gpt4 book ai didi

java - 在 Runnable 上设置 id

转载 作者:行者123 更新时间:2023-12-01 17:22:30 25 4
gpt4 key购买 nike

如何在此代码中的 Runnable 上设置 id

Runnable rd = new Runnable() {
@Override
public void run() {
populatePanel(referList.get(handler.getPosition()), handler.getPosition());
if (i == referList.size() - 1) {
i = 0;
}
}
};
handler.postDelayed(rd, delay);

我想区分一个线程和其他线程

最佳答案

您可以在线程上设置名称,而不是在 Runnable 上设置名称,例如使用以下命令:

    RunnableJob runnableJob = new RunnableJob();

Thread thread1 = new Thread(runnableJob);
thread1.setName("thread1");
thread1.start();

Thread thread2 = new Thread(runnableJob, "thread2");
thread2.start();

Thread thread3 = new Thread(runnableJob);
thread3.start();

Thread currentThread = Thread.currentThread();
System.out.println("Main thread: " + currentThread.getName() + "(" +currentThread.getId() + ")");

这将打印以下内容:

RunnableJob is being run by thread1 (11)
RunnableJob is being run by thread2 (12)
Main thread: main(1)
RunnableJob is being run by Thread-1 (13)

来源here .

关于java - 在 Runnable 上设置 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com