Analysis of Java thread mechanism

Source: Internet
Author: User

The basic concept of a thread:

A thread is a sequential flow of control within a program, a process that is equivalent to a task, and a thread is equivalent to a single execution path in a task. Multi-process: multiple tasks (Programs) can be run simultaneously in the operating system; Multithreading: multiple sequential streams execute simultaneously in the same application; Java threads are implemented through the Java.lang.Thread class; a VM starts with a Main method (public static void The thread defined by main () {}) to create a new thread by creating an instance of thread; Each thread completes its operation by using the method run () of a particular thread object. Method Run () is called the thread body to start a thread by invoking the start () method of the Thread class

Creation and startup of two Java threads:

There are two ways to create a new thread:

First type:

1. Define thread class implement Runnable interface

2.Thread mythread = new Thread (target);//target is runnable interface type

There is only one method in 3.Runnable: public void Run (), which defines the thread-running body

4. Use the Runnable interface to provide shared data for multiple threads

5. The static method public static thread CurrentThread () of thread can be used in the definition of the run () method of the class that implements the Runnable interface (); Gets the reference of the current thread

The second type:

1. You can define a subclass of thread and override its run method such as:

class MyThread extends Thread {   
public void run() {...}

}

2. Then generate the object of the class:

Mythread mythread = new Mythread ();

Three basic methods of Java Threading Control:

IsAlive (): Determine if the thread is still "alive"

GetPriority (): Gets the priority value of the thread

SetPriority (): Set priority values for threads

Thread.Sleep (): Specify the number of milliseconds for current thread sleep

Join (): Invokes the method of a thread, "merges" the current thread with the thread, waits for the thread to end, and then restores the current thread's running

Yield (): Let the CPU, the current thread into the ready queue waiting for scheduling

Wait (): The current thread enters the object's

Wait pool notify ()/notifyall (): Wakes one/all waiting threads in the await pool of the object

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.