Java basic tutorial on the basic concepts of multithreading

  • 2020-04-01 02:44:33
  • OfStack

Multithreading is an inevitable and important part of Java. Next we'll expand on multithreading. The next content is the explanation of the Java multithreading content before "JUC package added in JDK", including the interfaces such as wait() and notify() in the Object class. The interface in the Thread class; The synchronized keyword.

Note: the JUC package is the java.util.concurrent package, which was created by Java guru Doug Lea and added to Java in JDK1.5.


Before moving on to the rest of the chapter, let's take a look at some of the concepts associated with multithreading.
Thread state diagram

< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201401/20140114094003.jpg? 201401494139 ">

Description:
Threads include the following five states.
1. New status (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". After the thread object is created, other threads call the start() method of the object 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: the thread obtains the CPU permission for execution. It is important to note that threads can only go from ready to run.
4. Blocked & Blocked; : a blocking state is when a thread gives up the CPU for some reason and temporarily stops running. It is not until the thread is in the ready state that it has a chance to go to the run state. There are three types of obstruction:
      (01) wait block - makes a thread wait for a job to complete by calling the thread's wait() method.
      (02) synchronized block - when a thread fails to acquire a synchronized synchronized lock (because the lock is held by another thread), it enters a synchronized block.
      (03) other blocking - when a thread's sleep() or join() is called, or an I/O request is made, the thread enters a blocking state. When the sleep() state timeouts, join() waits for the thread to terminate or timeout, or when I/O processing is complete, the thread is re-entered into the ready state.
5. Dead       : when a thread finishes executing or exits the run() method due to an exception, the thread ends its life cycle.

 

The five states include the Object class, Thread, and the synchronized keyword. We will study each of these in later chapters.
Object class, which defines the wait(), notify(), notifyAll() and other sleep/wake functions.
The Thread class, which defines a series of Thread manipulation functions. For example, sleep(), interrupt(), getName(), get the thread name, and so on.
Synchronized, is the keyword; It distinguishes between synchronized code blocks and synchronized methods. Synchronized is used to let a thread acquire a synchronized lock on an object.
We'll look at why "wait(),notify() and other methods are defined in the Object class,not in the Thread class, when we go into more detail later.


Related articles: