Java thread Summary (1), java thread Summary
1 Java programs are inherently multi-threaded programs
Public class MultiThread {public static void main (String [] args) {// get the Java thread management MXBeanThreadMXBean threadMXBean = ManagementFactory. getThreadMXBean (); // you do not need to obtain the synchronous monitor and synchronizer information. You only need to obtain the thread and thread stack information ThreadInfo [] threadInfos = threadMXBean. dumpAllThreads (false, false); // traverses the thread information. Only the thread ID and thread name information are printed for (ThreadInfo threadInfo: threadInfos) {System. out. println ("[" + threadInfo. getThreadId () + "]" + threadInfo. getThreadName ());}}}
The running of a Java program is not only the running of the main () method, but also the main thread and multiple other lines.
.
2. Reasons for Multithreading
1 multi-processor core; one thread can only run on one processor
2. faster response time
3. Java thread priority
1Java thread uses integer member variable priority to control the priority. The priority ranges from 1 to 10.
SetPriority (int) is used to set the priority. The default priority is 5. Threads with a higher priority are allocated with a lower priority.
2. the correctness of a program does not depend on the priority of the thread. Different JVM environments and operating systems have different requirements. Some operating systems have different plans to ignore the priority of the thread.
4. thread status
Java threads have six different states throughout the lifecycle. At a given moment, a thread can only be in one state.
1. Initial status-the thread is built 2. Running status 3. Blocking status 4. Waiting status 5. Timeout status 6. Terminated status
It can be seen that after the thread is created, a start () method is removed to start running. After the county seat executes the wait () method, it enters the waiting state. The thread that enters the waiting state must rely on notifications from other threads to return to the running state. The timeout status is based on the actual waiting status and the timeout limit is added, that is, when the timeout is reached, the system will return to the Yunxiang status. When a thread calls a synchronous method, it enters the blocking state without obtaining the lock.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/qq_32423845/article/details/79494209