Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

multithreading - Dying thread in java

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The thread name is allocated by the ThreadFactory. Java defaults to using "Thread-X" where X is an incremented index. Each new thread gets a new index. The thread with the loop (Thread-2) doesn't complete/die, but the others appear to be (if the logging can be trusted)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...