Java Learning Notes-multithreading (i)

Source: Internet
Author: User

Programs, processes, and threads

Program: A static code, an ordered set of instructions, not running is just a bunch of code.

The program cannot be executed alone, only the program is loaded into memory, and the system allocates resources for it to execute, which is called a process. This means that the process is a separate unit of the system's resource allocation and scheduling, with each process having its own separate address space. So the difference between the program and the process is that the program is a set of instructions, is the static description of the process running text, and the process is the program on the system in order to execute the dynamic activities.

Process: A running program is called a process and has system resources (CPU, memory).

However, there are many defects in the process, mainly focusing on two points:

    1. A process can only do one thing at a time, and if you want to do two or more things at once, the process is powerless.
    2. If, for some reason, the process is blocked during execution, such as waiting for input, the entire process hangs, and other input-independent work must wait for the input to finish before it can be executed sequentially.

In order to solve the above two defects, the concept of threading was introduced.

Thread: A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process.

A process can have multiple threads, and a thread must have a parent process. A thread can have its own stack, its own program counter, and its own local variables, but cannot have system resources. It shares all the resources of the process with other threads of the parent process.

The life cycle and five states of a thread

Java threads have v basic state

new State (new): when the thread object pair is created, it enters the new state, such as thread t = new MyThread ();

ready State (Runnable): when the Start () method (T.start ()) of the thread object is called, the thread enters the ready state. A thread that is in a ready state simply means that the thread is ready to wait for CPU scheduling to execute, not to say that the T.start () thread executes immediately;

Run State (Running): When the CPU starts scheduling a thread in the ready state, the thread is actually executing, which is going into the running state. Note: The ready state is the only entry into the running state, that is, the thread must first be in a ready state if it wants to execute in a running state;

blocking State (Blocked): A thread that is in the running state temporarily abandons the use of the CPU for some reason, stops execution, and then enters a blocking state until it enters the ready state, and is then given the opportunity to be called again by the CPU to enter the running state. Depending on the cause of the blockage, the blocking state can be divided into three different types:

1. Wait for blocking: the thread in the running state executes the wait () method, which causes the thread to enter the waiting blocking state;

2. Synchronization blocking--the thread acquires the synchronized synchronization lock failure (because the lock is occupied by another thread), it will enter the synchronization blocking state;

3. Other blocking-the thread will go into a blocking state by calling the thread's sleep () or join () or making an I/O request. 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.

dead State (Dead): The thread finishes executing or exits the run () method because of an exception, and the thread ends the life cycle.

Creating Threads in Java

Here are two common ways to create threads in Java.

1. Inherit the thread class, overriding the run () method of the class.

 Public class extends thread{    privateint  i;    @Override    publicvoid  run () {        for (i=0;i<10;i++  ) {            + "" + i);     }}}
 Public class Main {    publicstaticvoid  main (string[] args) {        new MyThread ();        Thread.Start ();    }}

Operation Result:

Thread-0 0Thread-0 1thread-0 2thread-0 3thread-0 4thread-0 5 thread-0 6thread-0 7thread-0 8thread-0 9

2. Implement the Runnable interface and override the run () method of the interface, the run () method is also the thread execution body, create an instance of the Runnable implementation class, and use this instance as the target of the thread class to create the thread object. This thread object is the real thread object.

 Public class Implements runnable{    @Override    publicvoid  run () {        for(int i=0;i<10;i++) {            + "" + i);     }}}
 Public class Main {    publicstaticvoid  main (string[] args) {        new myrunnable ();         New Thread (runnable);        Thread.Start ();    }}

Operation Result:

Thread-0 0Thread-0 1thread-0 2thread-0 3thread-0 4thread-0 5Thread -0 6thread-0 7thread-0 8thread-0 9

Java Learning Notes-multithreading (i)

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.