Basic instance analysis of java thread
There are two ways to establish threads in Java: inheriting the thread class and implementing the runnable interface.
Inherit thread
As mentioned above, the thread object in Java must be created in the form of a class, and the run () method of the base class must be rewritten in this class, which is actually the execution body of the thread. Calling the start method of this class instance implicitly calls the run method.
It is not difficult to see that because new has twice myThread, the two instances are different, that is, each has its own I variable, which is independent of each other.
Runnable interface
It can be seen that this method is implemented by loading an object into the thread class as a target, so even if there are more new thread objects, as long as the target is the same referenced object, the run method of the object will be called, and all threads share the resources of the target object. Therefore, thread 1 and thread 2 will output a total of 51 times, The two threads jointly complete the output of I from 0 to 49, instead of outputting 5 times respectively as above. As for why 51 threads are output, and the two threads enter the ready state almost at the same time (the start method only allows the threads to enter the ready state). It is not difficult to observe the above I variable. When I is equal to 0, thread 1 and thread 2 are running at the same time, resulting in concurrency, and jointly output I = 0. After that, the CPU continuously switches threads, So that only one thread is outputting at the same time.
Thread state
Threads are divided into four states
Ready state: call the start method to enter the ready state@ H_ 403_ 37@ running state: threads in the ready state will be scheduled by the JVM to become running state@ H_ 403_ 37 @ blocking status: if some synchronization methods do not return results, blocking status occurs, or sleep and yeld@ H_ 403_ 37 @ dead state: the method body has finished executing or forcibly stops a thread@ H_ 403_ 37@
Basic operation of thread
Join() merge threads: after the current thread calls a thread's join method, it will wait for a thread to execute before the thread continues@ H_ 403_ 37 @ sleep (long milliseconds) thread sleep: blocks the current thread. It will not continue until the blocking time is up. When blocking again, the CPU ownership will be handed over to other threads, so sleep (1) is often used to switch threads@ H_ 403_ 37 @ * * yield() thread concession: * * yeild is similar to sleep, but it will only give way to other threads higher than itself or at the same level. If no other thread is lower than itself, execute this thread again.
Background thread
After a program is executed by the operating system, there will be a process. A process has at least one thread (main thread). The main thread does not have much particularity than other threads, just because it is the earliest thread to be executed. Other threads will be created in the main thread. If it is not specified, the foreground thread (including main thread) will be created by default, If setdaemon (true) is called, the thread will be explicitly set as a background thread. The background thread is a daemon thread. As can be seen from its name, its main function is to provide guard and service functions for other threads. When all foreground threads end, the background thread will be forced to end because it has no meaning at this time.
Foreground Threads
0 to 9998 in the complete output sub thread of the program; It shows that there is nothing special about the main thread, and its end will not affect the operation of other foreground threads.
Background thread
The program does not exit with a complete output of 0-9998, indicating that after the main foreground thread ends, the JVM forcibly ends the background thread.
summary
The above is all about the basic instance parsing of Java threads in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!