Java Basic Tutorial Multi-Threading Basic Concepts Java Multithreading Tutorial _java

Source: Internet
Author: User
Tags sleep thread class

Multithreading is an unavoidable important subject in Java. Below we will expand the study of multithreading. The next step is to explain the Java multithreaded content before the "new Juc package in JDK", which includes interfaces such as Wait (), notify () in the object class, interfaces in the thread class, synchronized keywords.

Note: The Juc package refers to the Java.util.concurrent package, which was completed by the Java Master Doug Lea and added to Java in the JDK1.5 version.


Before you go to the later chapters, learn about the concepts of multithreading.
Thread state diagram

Description
A thread consists of the following 5 states altogether.
1. New State (new): After the thread object is created, it enters the new state. For example, thread thread = new Thread ().
2. Ready state (Runnable): Also known as "executable state." When a thread object is created, the other thread invokes the object's start () method to start the thread. For example, Thread.Start (). A thread in the ready state may be scheduled to execute by the CPU at any time.
3. Running state (Running): The thread acquires CPU privileges to execute. It should be noted that the thread can only move from the ready state to the running state.
4. Blocking state (Blocked): Blocking state is the thread for some reason to give up the use of CPU, temporarily stop running. Until the thread enters the ready state, there is an opportunity to go to the running state. The blocking situation is divided into three types:
(01) Wait for blocking--by invoking the thread's Wait () method, the thread waits for the completion of a work.
(02) Synchronous blocking-the thread fails to acquire the synchronized synchronization lock (because the lock is occupied by another thread) and it enters the synchronized blocking state.
(03) Other blocking--the thread enters the blocking state by calling the thread's sleep () or join () or sending an I/O request. When the sleep () state times out, join () waits for the thread to terminate or timeout, or the I/O process completes, the thread is back in ready state.
5. State of Death (Dead): The thread has finished executing or exited the run () method because of an exception, which ends the lifecycle.

The 5 states involved include the object class, thread, and synchronized keywords. We will study each of these in the following chapters. The
object class, which defines a wait (), notify (), Notifyall (), and so on hibernate/wake functions. The
Thread class, which defines the thread action functions for some columns. For example, the sleep () Hibernate function, the interrupt () interrupt function, GetName () Gets the thread name, and so on.
synchronized, which is a keyword, is distinguished by synchronized code blocks and synchronized methods. The role of synchronized is to let the thread get the synchronized lock of the object.
When we go into the details of wait (), notify (), and so on, we analyze Why "wait (), notify (), and so on, are defined in the object class, not in the thread class."

Related Article

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.