Java concurrent programming-two ways to create a thread and their differences

Source: Internet
Author: User
Tags thread logic

Java concurrent programming-two ways to create a thread and their differences

 
1. Differences between threads and processes

Parallel: Multiple tasks are executed at the same time. For example, a multi-core computing task can be considered as a parallel task.
Concurrency: At the micro level, multiple tasks seize one CPU to execute their own tasks and execute the tasks in turn. However, in case of resource conflicts, the execution efficiency is not greatly improved. However, the CPU usage efficiency is improved.

A diagram on GitHub some time ago can be used to describe the concept above.

2. Two ways to create a thread in Java

1.Method 1: Directly inherit from Thread:

Public class Dog extends Thread {// private variable private int number; // initialize the upper number of the multiplication table public Dog (int number) {this. number = number;} // rewrite the logic method @ Override public void run () {// print the multiplication table for (int I = 1; I <= number; I ++) {for (int j = 1; j <= I; j ++) {System. out. print (j + * + I + = + I * j +);} System. out. println () ;}} public static void main (String [] args) {Dog dog = new Dog (9); dog. start (); // start the thread to print the multiplication table }}

2.Method 2: How to Implement the Runnable interface

Public class Cat implements Runnable {private int number; public Cat (int number) {this. number = number;} // rewrite the logic method @ Override public void run () {System. out. println (Current Thread: + Thread. currentThread (); for (int I = 0; I <= number; I ++) {for (int j = 0; j <= I; j ++) {System. out. print (I + * + j + = + I * j +);} System. out. println () ;}} public static void main (String [] args) {Thread task = new Thread (new Cat (9); task. start ();}}

Compared with the above two methods, we can find that the first method inherits the Thread and cannot inherit other parent classes, so it is not very friendly for program scalability, but can access the information of the current Thread through this. Method 2 InheritanceRunnableThe interface is flexible from the design point of view. Java does not support multi-inheritance but supports implementing multiple interfaces. This can be overcome. To access thread information, useThread.currentThread();Method.

3. Source Code Analysis

Public class Thread implements Runnable {// associate with the Runnable interface private Runnable target; // The Thread logic method public void run () {if (target! = Null) {target. run () ;}}// thread interface public interface Runnable {// define the abstract thread logic method, public abstract void run ();}

The above Thread class and Runnable interface are two of the most important core parts of Java JDK. inheriting Thread is an extension of JDK's system class, the Runnable interface is implemented as a standard. No matter which method JVM starts the thread, it loads them in different ways. Which method is better? Of course, the second method is more elegant and flexible in terms of design.

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.