If thread died when run() finish or when there is an exception, why my log keeps adding numbers. For example, I have a thread that on every loop iteration opens a new thread and send something over a socket. My question, the threads are dying right?
class otherClass implements Runnable {
private static Logger logger = LogManager.getLogger(otherClass.class);
String dir_dest = "localhost";
Integer port_dest = 8888;
public ServerExecuter(){
Thread miHilo = new Thread(this);
miHilo.start();
}
public void run() {
try {
while (true) {
Socket sock = server.accept();
BufferedReader in= new BufferedReader(new InputStreamReader(miSocket.getInputStream(), "UTF8"));
String msg = in.readLine();
logger.info(msg);
oneClass ex = new oneClass();
if (ex.isClose()) {
sock.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class onClass implements Runnable {
private static Logger logger = LogManager.getLogger(oneClass.class);
private String host = "localhost";
private Integer port = 9999;
public oneClass(){
Thread my_thread = new Thread(this);
my_thread.start();
}
public void run() {
socket = new Socket(host, port);
DataOutputStream out = new DataOutputStream(socket .getOutputStream());
msg = "Hi";
out.writeBytes(msg);
socket.close();
logger.info("thread finish");
}
public boolean isClose() {
return close;
}
public void setClose(boolean close) {
this.close = close;
}
}
My code is roughly like this, and it prints the following in the console:
11:22:50.810 [Thread-2] INFO
11:22:50.811 [Thread-92] INFO
11:22:50.813 [Thread-92] INFO - thread finish
11:22:50.937 [Thread-2] INFO
11:22:50.938 [Thread-93] INFO
11:22:50.947 [Thread-93] INFO - thread finish
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…