Basic knowledge of multithreading

Source: Internet
Author: User

If a class inherits the thread, it is not suitable for resource sharing. However, if the Runable interface is implemented, it is easy to realize the resource sharing.

The benefits of implementing the Runnable interface are more than inheriting the thread class:

1): Suitable for multiple threads of the same program code to process the same resource

2): Can avoid the restriction of single inheritance in Java

3): Increase the robustness of the program, the code can be shared by multiple threads, code and data Independent

4): The thread pool can only be put into a class that implements runable or callable classes and cannot be placed directly into the inherited thread

In Java, at least 2 threads are started each time a program runs. One is the main thread and one is the garbage collection thread. Because every time a class is executed with a Java command, a JVM is actually started, and each JVM internship starts a process in the operating system.

Thread state transitions

1. New state: A new Thread object was created. 2. Ready state (Runnable): After the thread object is created, other threads call the object's start () method. The state of the thread is located in a pool of running threads that becomes operational and waits for the CPU to be used. 3, running State (Running): The ready state of the thread gets the CPU, executes the program code. 4, blocking state (Blocked): Blocking state is the thread for some reason to abandon the use of the CPU, temporarily stop running. Until the thread is in a ready state, the opportunity to go to the running state is reached. There are three types of blocking: (i), waiting for blocking: The running thread executes the wait () method, and the JVM puts the thread into the waiting pool. (wait will release the held lock) (ii), synchronous blocking: When a running thread acquires a synchronization lock on an object, the JVM puts the thread into the lock pool if the synchronization lock is occupied by another thread. (iii), other blocking: The running thread executes the sleep () or join () method, or when an I/O request is made, the JVM will place the thread in a blocked state. When the sleep () state times out, join () waits for the thread to terminate or time out, or the I/O process finishes, the thread is re-entered in a ready state. (Note that sleep does not release held locks)5. Dead State (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.

Basic knowledge of multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.