好的,我知道这可能是对线程如何真正工作的理解不好,但是直到有人帮助我理解我才会相信这是一个错误:)
在我的 Main 类及其 main() 方法中,我有:
public static void main(String args[]){
StoneBucket stoneBucket = new StoneBucket();
StonePutter spRunnable = new StonePutter(stoneBucket);
StoneThrower stRunnable = new StoneThrower(stoneBucket);
StoneThrower stRunnable2 = new StoneThrower(stoneBucket);
//Create the Threads that will take the Runnables as arguments
Thread puttingThread = new Thread(spRunnable);
Thread throwingThread = new Thread(stRunnable);
Thread throwingThread2 = new Thread(stRunnable);
puttingThread.setName("Putter");
throwingThread.setName("Thrower 1");
throwingThread2.setName("Thrower 2");
[...]
然后在我的 StoneThrower 课上
public class StoneThrower implements Runnable{
private StoneBucket sb;
private String name;
public StoneThrower(StoneBucket _sb){
this.sb = _sb;
}
public void run(){
name = Thread.currentThread().getName();
System.out.println("T::"+name+" started...");
int count = 0;
while(true){
[...]
当我编译并运行这段代码时,我得到:
所以,我的问题是为什么这两个线程都返回相同的名称currentThread().getName()
?当它们被创建时,它们被分配了名称,threadX.setName("XXX")
并且这些可运行文件通过调用开始threadX.start()
......有人可以澄清一下吗?
编辑:我接受了正确的答案,因为将 stRunnable 更改为 stRunnable2 行为符合预期。现在真正的问题是为什么会发生这种情况。我创建了两个线程并分别启动它们。run() 方法(在创建线程时调用一次)如何返回错误的名称?