Java Notes: Understanding and Application of multithreading (I.)

Source: Internet
Author: User

Processes and Threads

A process is a process of dynamic execution of a program that undergoes a complete process of loading, executing, and executing from code, which is also the life cycle of the entire process.

Multithreading is an effective means to implement concurrency mechanism. Processes and threads are the basic units for implementing concurrency mechanisms.

Traditional single-core CPUs can have multiple programs executing at the same time period, but only one program runs at a point in time, and all programs preempt CPU resources.

The multi-core CPU program executes concurrently.


Implementation of multi-threading in Java

There are two ways to implement multithreading in Java:

1. Inheriting the thread class

2. Implement the Runnable interface


1. Inheriting the thread class

The inherited thread class is defined in the Java.lang package, and a class that inherits the thread class must overwrite the Run method in the thread class, which is the principal of the threads.

Instance:

Class MyThread extends Thread {private String name;public MyThread (String name) {this.name = name;} public void Run () {//Overwrite the Run method as the principal for the thread operation for (int i = 0; i < 3; i++) {System.out.println (name + "Test i=:" + i);}} public class ThreadTest01 {public static void main (string[] args) {//TODO auto-generated method Stubmythread MT1 = new My Thread ("Threads a"); MyThread mt2 = new MyThread ("Thread B"); Mt1.run (); Mt2.run ();}}


However, from the running result, the program does not achieve the effect of concurrent execution, because the method is called when the object is still used. The format of the method. To allow them to execute concurrently, call the Start method in the thread class.

The call changes little, here slightly.


After the change, you can see that two programs are executed concurrently.

2. Implement the Runnable interface

The start thread relies on the thread class, and if you have inherited the thread class, you can use the Start method directly, but there is no method in the Runnable interface.

The thread class is constructed as: public thread (Runnable target)

Use the above construction method to start multithreading.

Class MyThread implements Runnable {private String name;public MyThread (String name) {this.name = name;} public void Run () {//Overwrite the Run method as the principal for the thread operation for (int i = 0; i < i++) {System.out.println (name + "Test i=:" + i);}} public class ThreadTest01 {public static void main (string[] args) {//TODO auto-generated method Stubmythread MT1 = new My Thread ("Threads a"); MyThread mt2 = new MyThread ("Thread B"); thread T1 = new Thread (MT1); Thread t2 = new Thread (MT2); T1.start (); T2.start ();}}



The relationship between the thread class and the Runnable interface

As you can see in the API, the thread class is also a subclass of the Runnable interface.

RunnableInterfaces should be implemented by classes that intend to execute their instances through a thread. The class must define a run parameterless method called.

This interface is designed to provide a common protocol for objects that want to execute code at the time of the activity. For example, the Thread class implements the Runnable . Activation means that a thread has started and has not stopped.


the difference between the thread class and the Runnable interface

The use of the thread class does not achieve the purpose of resource sharing, and the use of the Runnable class can be used for resource sharing purposes.


Example of selling tickets:

Thread:

Class MyThread extends Thread {private int ticket = 5;private string Name;public MyThread (string name) {this.name = name;} public void Run () {//Overwrite the Run method as the principal for the thread operation for (int i = 0; i < i++) {if (Ticket > 0) {System.out.println (name + "Te St I=: "+ ticket); ticket--;}}} public class ThreadTest01 {public static void main (string[] args) {//TODO auto-generated method Stubmythread MT1 = new My Thread ("Threads a"); MyThread mt2 = new MyThread ("Thread B"); Mt1.start (); Mt2.start ();}}

Runnable:

Class MyThread implements Runnable {private int ticket = 5;private string Name;public MyThread (string name) {this.name = n Ame;} public void Run () {//Overwrite the Run method as the principal for the thread operation for (int i = 0; i < i++) {if (Ticket > 0) {System.out.println (name + "Te St I=: "+ ticket); ticket--;}}} public class ThreadTest01 {public static void main (string[] args) {//TODO auto-generated method Stubmythread MT1 = new My Thread ("Threads a"); thread T1 = new Thread (MT1); T1.run (); T1.run (); T1.run (); }}


The visible runnable interface can be used for resource sharing.


Summary: The runnable interface has a greater advantage than inheriting the thread class: 1. Suitable for processing the same resource in code with multiple identical programs. 2. The impact of single-inheritance limitations can be avoided 3. The code can be shared by multiple processes, and the code is independent of the data.


The state of the thread

Create a status: Ready for a Multithreaded object

Ready: Call the Start method, wait for the dispatch (the Start method is ready to start, and wait for the CPU to dispatch, in fact, execute the Run method)

Run: Execute the Run method;

block, pause: Suspend this thread and give it to another thread;

Termination: Thread execution is complete and is no longer used.

Java Notes: Understanding and Application of 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.