Java thread (1), java thread (

Source: Internet
Author: User

Java thread (1), java thread (
What is the basis of a java thread?

Here we reference Baidu encyclopedia's statement: "A thread, sometimes called a Lightweight Process (LWP), is the smallest unit of a program execution flow. A standard thread consists of thread ID, current Instruction Pointer (PC), register set, and stack .", In short, the minimum unit for thread cpu execution program scheduling. The cpu calls and executes threads to enable the program to run normally.

Relations among threads, processes, and applications

In an operating system, a process represents an application. If an application is regarded as an entity, the process is a running entity. An application can contain multiple threads, and a process can also contain multiple threads. A combination of multiple threads is called an application. If an application contains only one thread, the modified thread is a utility program, which is called a single-threaded program!

How to Create a thread in java

There are two ways to create a Thread in java. One is to implement the run () method of the Runnable interface and pass its object instance to the Thread construction to create a Thread, one is to extend the run method of the Thread class to create a Thread.

1 new Thread (new Runnable () {2 public void run () {3 // omitting the code to be executed by this Thread 4} 5}). start ();
How to Implement the Runnable interface
New Thread () {@ Override public void run () {// omitting the code super. run () ;}}. start () to be executed by this Thread ();
Extension of Thread class

The Thread class is a Thread class provided by java APIs. It starts a Thread through the start () method and executes the code in the run () method after the Thread starts. When run () after the code in the method is executed, the thread ends. We recommend that you use the Runnable interface to create a thread in the preceding two ways, because this method is more in line with the object-oriented idea and does not destroy the design structure of the original java API code, the method of extending the Thread class not only destroys the original design structure of Java API but does not conform to the object-oriented design idea. In addition, the original run () method of the Thread class also calls the run () method of the Runable interface to call the Thread content code.

 

Several States of a thread

Threads can be divided into five states: creation, ready, blocked, running, and dead.

Create:The new Thread () method is used to create a Thread instance, but the start () method is not called. At this time, the thread instance has been created, but the thread has not been started.

Ready:Call the start () method after the thread instance is created. At this time, the thread has been started and can be scheduled by JVM to be executed by the cpu, but the cpu does not execute this thread currently.

Run:The thread has been started and scheduled by JVM to be executed by the cpu. the cpu is executing this thread.

Blocking:When a thread is running, it calls sleep () and join () of the thread object ()Method or the wait () method of the Monitored object locked by this thread enters the blocking status on the site and enters the ready state after certain conditions are met.

Death:When the run () method of the thread object is executed, or an error occurs during execution, causing the thread to end abnormally, the thread is in the dead state.

Several Methods for changing the thread state details the methods produced by the thread object

Sleep (): The sleep method enables the thread to discard the cpu usage and enter the sleep state for a period of time. After this time, the thread automatically enters the ready state.

Join (): The join () method causes the thread to discard the cpu usage right and add it to another thread. When another thread stops running and enters the dead state, the thread automatically enters the ready state, otherwise, it will remain in the blocking status.

Yield (): The yield () method enables the thread to temporarily discard cpu usage and enter the ready state. However, because the cpu scheduling is random and cannot be intervened, sometimes yield () method does not have any effect.

Start (): the start () method enables the startup of the thread object to enter the ready state. Note: The start () method can only be called once after the thread instance is created. When the thread enters the dead state, the start () method cannot be called to bring it into the ready state.

Method for generating a monitoring object locked by a thread

Wait (): The wait () method causes the current thread to discard the cpu usage right and the lock of this object, and enters the blocking state to know whether other threads call the policy () or policyall () of this object () method to enter the ready state.

Y (): notify () method checks whether there are waiting threads on the object. If there are one or more threads, randomly select a thread to make it ready. If no, no operation is performed.

Yyall (): The notifyall () method checks whether there are waiting threads on the object. If yes, it enters the ready state. If no, no operation is performed.

  

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.