创建一个线程并在类的构造函数中调用其start()方法是否正确?
public class Server implements Runnable { private ServerSocket server; public Server(int port) { try { //Opens a new server server = new ServerSocket(port); } catch (IOException ioe) { ioe.printStackTrace(); } new Thread(this,"Server").start(); } @Override public void run() { } }
解决方法
恕我直言,不要这样做.你允许这个引用在构造期间逃脱.