gpt4 book ai didi

Java - 线程不想启动

转载 作者:行者123 更新时间:2023-11-29 03:00:44 25 4
gpt4 key购买 nike

我有这个控制台应用程序,但出于某种原因线程的 run() 方法不想启动。代码第一次看起来很长,但我尽可能多地组织它。

结果输出:

eThread starting!! 

所以看起来 CarManager.startFunctionalities() 被执行了,但是 eThread.start() 行根本没有执行,因为“started”行没有被打印出来。

这是源代码。

主类:

package rpicar.android;


public class AndroidEmulator{


public static void main(String args[]) throws InterruptedException {
CarManager cm = new CarManager ("localhost");
}
}

汽车管理器:

package rpicar.android;

import rpicar.common.Direction;
import rpicar.common.EnvironmentData;


public class CarManager {
private MotorManager mManager;
private final String RPIADDRESS = "localhost";
private Thread mThread; //motor
private EnvironmentManager eManager;
private Thread eThread;
public CarManager(String rpiAddress) {
//initialize MotorManager
mManager = new MotorManager(RPIADDRESS);
//Make a thread for the Motor commands
mThread = new Thread(mManager);
//Initialize EnvironmentManager
eManager = new EnvironmentManager(RPIADDRESS);
//Makea thread for collecting EnvironmentData
eThread = new Thread (eThread);
startFunctionalities();
}

public void move(Direction d){
this.mManager.setDirection(d);
}

public EnvironmentData getCurrentEnvironmentData(){
return this.eManager.getCurrentEnvironmentData();
}

private void startFunctionalities(){
//Start MotorManager for sending movement commands when needed.
//mThread.start();
//Start EnvironmentManager to collect EnvironmentData
System.out.println("eThread starting!! ");
eThread.start();
}
}

环境管理器:

package rpicar.android;

import rpicar.common.CarComponent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import rpicar.common.EnvironmentData;


public class EnvironmentManager extends CarComponent implements Runnable{
private EnvironmentData currentEnvironmentData;

public EnvironmentManager(String rpiAddress) {
super(rpiAddress, 2176, true);
this.currentEnvironmentData = new EnvironmentData();
}

public synchronized EnvironmentData getCurrentEnvironmentData() {
return currentEnvironmentData;
}

public synchronized void setCurrentEnvironmentData(EnvironmentData currentEnvironmentData) {
this.currentEnvironmentData = currentEnvironmentData;
}

@Override
public void run() {
System.out.println("eThread started!! ");
super.connect();
while(true){
try {
this.setCurrentEnvironmentData((EnvironmentData) super.in.readObject());
} catch (IOException ex) {
super.connect();
} catch (ClassNotFoundException ex) {
Logger.getLogger(EnvironmentManager.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
}

最佳答案

当您创建 eThread 的实例时,您不小心将线程本身传递给了构造函数(或者根据操作顺序,您传递了 null)。

您应该将 eManager 传递给构造函数。

eThread = new Thread (eThread);

会变成

eThread = new Thread (eManager);

您可以通过将 eThread 设为 final 字段来保护自己将来免受此错误的影响,这样您就无法在声明它之前使用它。

关于Java - 线程不想启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35159548/

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