我希望 main 方法打印出值 x,该值是由正在运行的线程返回的。我该怎么做?希望我的问题有意义。
import java.*;
public class ServerStudentThread extends Thread
{
public ServerStudentThread(Socket x) {
client = x;
}
public void run()
{
//Do something here and return an integer,
// for example **x**
}
public static void main(String args[]) throws Exception {
// ...
// I want to print out x value here.
// But as you can see, x disappears after thread finish its job.
}
}
最佳答案
您必须将 x 设置为 ServerStudentThread 类中的字段,而不是局部变量。然后在类中添加一个返回 x 的方法,例如 getValue()。在 main 方法中,创建线程后,运行它,调用类上的 getValue() 方法来打印获取 x 的值并打印它。
关于java - 我希望 main 方法打印出值 x,该值是由正在运行的线程返回的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4978345/