Java – calling a new thread inside is a constructor
•
Java
Is it correct to create a thread and call its start () method in the constructor of the class?
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() { } }
Solution
With all due respect, don't do that You allow this reference to escape during construction
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码