The concept of multithreading

Source: Internet
Author: User

The concept of multithreading

I. Procedures and processes

1, program: a static code.

2, Process: a dynamic execution process of the program, it should be loaded from the code, execution to completion of a complete process.

3, the process is also called the task, supporting multiple processes concurrently executing the OS is called the multi-process OS or multi-tasking OS.

Ii. Processes and Threads

It is also possible to implement multiple tasks concurrently within a program, where each task is called a thread.

A thread is a smaller execution unit than a process, which is a separate control flow within a process, that is, the control flow inside the program.

Feature: Threads cannot run independently and must be dependent on the process to run in the process.

At least one thread per program is referred to as the main threaded.

Single threaded: Processes with only one thread are called single-threaded

Multithreading: Processes that have more than one thread are called multithreading

Third, the advantages and disadvantages of opening multi-threading

Improve interface program response speed. By using threads, a process that takes a lot of time to complete can be started in the background by a separate thread, increasing the corresponding speed of the foreground interface.

Make full use of system resources to improve efficiency. By executing multiple processes at the same time within a program, you can take advantage of system resources such as CPUs to maximize the performance of your hardware.

When the number of threads in the program is relatively long, the system will spend a lot of time to switch threads, which will reduce the execution efficiency of the program. However, compared to the advantages, the disadvantage is still very limited, so the current project development, multi -threaded programming technology has been widely used.

Multithreading implementation

First, in the implementation of threading programming, it is necessary to make a class with the ability to multithreading, inheriting the thread class or implementing the Runnable interface of the class with the ability to multithreading, and then create a thread object, call the corresponding startup thread method to implement threading programming.

Multiple threads can be implemented in a program, multithreaded programming means that two or more than two threads are started in the same program.

In the actual implementation of threads, the Java language provides three implementations:

(1) Inherit the thread class

(2) Implement Runnable interface

(3) using the timer and timertask combination

Second, inherit thread thread class to implement multithreading
A specialized threading class (thread) is provided in the Java.lang package, which encapsulates many methods for scheduling and processing threads. If a class inherits the thread class, the class is capable of multithreading and can be executed in a multi-threaded manner.

12345678910111213141516171819202122232425262728293031 class MyThread extends Thread{ public void run(){ //线程体 } } 继承Thread类实现多线程。 MyThread tt1 = new MyThread (); //启动线程  tt1.start(); try{ for(int i = 0;i < 5;i++){ //延时1秒 Thread.sleep(1000); System.out.println("Main:" + i); } }catch(Exception e){}

Attention:

Threading Features: Randomness, the system only guarantees that threads are executed alternately when executing a multithreaded program, and it is not guaranteed that the thread executes after which thread is executed first, and the order of execution can be guaranteed by writing specialized code.

Multiple threads can also be started for the same thread class

The same thread cannot be started two times,

When the Run method in the custom thread finishes executing, the custom thread dies naturally. For system threads, the end is only when the main method executes and the other threads that start are finished. When the system thread executes, the execution of the program really ends.

Third, realize runable interface

A) Multithreaded objects implement the Java.lang.Runnable interface and override the Run method of the Runnable interface in this class.

b) Benefits: The method of implementing the Runable interface avoids the limitations of single inheritance.

Example 1: Multithreading is implemented in a way that implements the Runable interface.

1234567891011 class mythread2 implements runable { &NBSP;  public void run () {} //override the Run () method in the Runable interface &NBSP;  }    mythread2 mt1= new mythread2 (); &NBSP;  thread t1= new thread (MT1); &NBSP;  t1.start ();
The life cycle of a thread

One, the life cycle of the thread

A thread is a process of dynamic execution, and it also has a process from birth to death, which is called the life cycle. A thread has 5 states over its life cycle:

1. Newly created (new Thread)

When an instance (object) of the thread class is created, the thread enters a new state (not started).
For example: Thread t1=new thread ();

2. Ready (runnable)

The thread has been started and is waiting to be allocated to the CPU time slice, which means that the thread is waiting in the ready queue to get CPU resources. For example: T1.start ();

3. Operation (running)

The thread obtains the CPU resources that are executing the task (the Run () method), at which point the thread will run until the end, unless the thread automatically discards the CPU resources or has a higher priority thread entry.

4. Death (Dead)

When a thread executes or is killed by another thread, the thread goes into a dead state, and the thread is no longer ready to wait for execution.

Natural termination: Terminates after running the run () method normally

Abort: Call the Stop () method to let a thread terminate the run

5. Blockage (blocked)

For some reason, the running thread is getting out of the CPU and pausing its own execution, which goes into a blocked state.

Sleeping: Use the sleep (long T) method to put the thread into sleep mode. A sleeping thread can go into a ready state at the specified time.

Waiting: Call the Wait () method. (Call Motify () method back to Ready state)

Blocked by another thread: Call the Suspend () method. (Call Resume () method recovery)

QQ Group: Web Developer Official Group (321386971) authentication message: Admin10000

The concept of multithreading

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.