When I was doing my big project in junior high, the main interface couldn't come out when my server opened the thread that listened to the client's request. I believe we all know that there is a process preemption that happens. But I was depressed, obviously I opened a sub-thread, why and the main thread occurred preemption? After studying, I found my thread to start the function with the Thread.run (). When I changed run to Thread.Start (), the problem was solved. At this point, let's take a look at the threading mechanism of this thing.
As shown, the thread is divided into 5 states. New, ready, running, dead, blocked. NEW: The process has just been newly created, new ready: The thread can run. The start () function runs on the surface thread, but it is not yet running. When in a blocking state, the blocking condition resolves to a functioning state. Run: the thread runs. The only way into the run is to call the function run (). Blocking: The thread is stopped for some reason and needs to be resolved for some reason to run. Resolves the post to Ready state. The Sleep (), Yieid (), join () method can be called to stop or pause. Death: The thread dies automatically when the run () method has finished executing. To let a thread die, do not use Stop (), it is best not to let while () loop, break out, let the Run method executes, the thread died normally. Or, call interrupt when the thread state is in a blocked state. This blog writes quite detailed http://blog.csdn.net/lonelyroamer/article/details/7949969 my problem, because I use Run (), let the thread start to execute, The Accept () method waits for the client to connect. and start () is to let it into the waiting state, when the interface is displayed and then executed. It won't make the main interface go out.
Java Thread life cycle