Use Thread Classes and Runnable Interface to achieve the difference between multithreading
Let's first look at the steps of the two implementation methods :
public class ThreadDemo{
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
// Create and start inherited by Thread Class created thread
new Thread(new MyThread(),"Thread"+i).start();
// Create and start by implementing Runnable Interface created thread
new Thread(new Runner(),"Thread"+i).start();
}
}
}
// Inherit Thread class
class MyThread extends Thread{
// rewrite run Method
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Inherited by Thread establish ");
}
}
// Realization Runnable Interface
class Runner implements Runnable{
// Realization run Method
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" There is realization Runnable Interface to create ");
}
}
As you can see from the code above , When using Runnable Interface when creating multithreads , You need to enter and exit the implementation class as a parameter to Thread In the instance object , By calling Thread Object's start Method to start . Let's take a look Thread Source code
//Thread Class inherited Runnable class
public class Thread implements Runnable {}
//Thread The constructor of called init Method
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
//init Static method called init
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null, true);
}
// Look at the static method init
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
...
// private Runnable example
this.target = target;
...
}
// Look again. Runnable Method
@Override
public void run() {
if (target != null) {
target.run();
}
}
From the above process of tracking the source code, we can see ,Thread Class implements the Runnable Interface , And inheritance Thread Class rewriting run The essence of method is to realize Runnable Interface run Method .
Through the above analysis , Summarize the use of Thread Classes and Runnable Interface differences :
- Using inheritance Thread Class implements multithreading compared to Runnable It's simpler , Use Runnable The interface needs to use Thread Encapsulate again .
- because Java Multiple inheritance is not supported in , A class inherits Thread Class cannot inherit other classes , Therefore use Runnable Interface implementation of multithreading has better flexibility .
In addition to the above two multithreading implementation methods , You can also use Callable Interface implementation , I wrote an article about Callable and Runnable Interface implementation of multi-threaded comparison summary :
Use Runnable and Callable Interface to achieve the difference between multithreading
Use Thread Classes and Runnable More related articles on the difference between interface implementation and multithreading
- Thread Classes and Runnable Interface to implement multithreading --2019-4-18
1. adopt Thread Realization public class TestThread extends Thread{ public TestThread(String name) { super(name); } ...
- explore Java Multithreading Thread Classes and Runnable Connections between interfaces
First of all, review Java Multithreading implementation mechanism ,Java There are several ways to achieve multithreading : 1. Inherited (extends)Thread class 2. Realized (implements)Runnable Interface in other words There are two kinds of feelings ...
- Multithreading -----Thread Class and Runnable Interface differences
The first to inherit Thread Class to implement multithreading , Actually, it's equivalent to taking out three things, that is, three to sell breakfast 10 Each task is divided into three windows , They do their own things, sell their own breakfast, and finish their own tasks , because MyThread Inherit Thread class , So in newMy ...
- Java Multithreading and concurrency ( 3、 ... and ),Thread Classes and Runnable Interface
Catalog 1.Thread and Runnable Interface 3、 ... and .Thread Classes and Runnable Interface 1.Thread and Runnable Interface
- Thread Class and Runnable Deep understanding of interfaces
Thread Class and Runnable Deep understanding of interfaces 1.Thread Class implements the Runnable Interface , Realization run Method , among target The parameter corresponds to a Runnable Implementation class of interface @Override publ ...
- Java By inheritance thread Class and Implementation Runnable Interface to achieve the difference between multithreading
Java There are two ways to create threads in : 1. By inheritance Thread class , rewrite Thread Of run() Method , Put the logic of the thread running in it 2. By implementing Runnable Interface , Instantiation Thread class One . By inheritance T ...
- Multithreading , Thread class ,Runnable Interface
Multithreading Threads : A thread is an execution unit in a process , Responsible for the execution of programs in the current process , At least one thread in a process . There can be multiple threads in a process , This application can also be called a multithreaded program . Single threaded program : namely , If there are multiple tasks, they can only be performed in turn ...
- Several ways to create threads Thread Classes and Runnable Interface
For many people who want to learn java For people who , I often hear people say that threads are difficult , In fact, after really understanding threads , It's not hard to thread at all , Here I'll comb the thread creation method for you . One . There are three ways to create threads 1. Inherit Thread class 2. Realization Runn ...
- Java The multithreading Thread class and Runnable Preliminary use of interface
Catalog Thread class Thread Defines the thread class Thread To start a thread Runnable Interface Runnable Defines the thread class Runnable To start a thread @ Thread class Thread Class is ...
- Multithreading ----Thread class ,Runnable Interface , Thread pool ,Callable Interface , Thread safety
1 Concept 1.1 process A process is a running program . Exactly , When a program enters memory to run , It becomes a process , A process is a running program , And it has some independent functions . In Task Manager : 1.2 Threads A thread is an execution unit in a process ...
Random recommendation
- 【BZOJ-4245】OR-XOR Bitwise greed
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- xml Study note 2 ( The rules )
XML The rules of grammar are very simple , And it's very logical . These rules are easy to learn , It's also easy to use . all XML All elements must have closing tags stay HTML, It's common to see elements that don't have closed tags : <p>This is a para ...
- 【 turn 】 On request.getSession(true/false/null) The difference between
http://blog.csdn.net/gaolinwu/article/details/7285783 About request.getSession(true/false/null) The difference between One . Demand source ...
- One every day linux command ——crontab
crontab Commands are used to submit and manage user tasks that need to be performed periodically , And windows The next planned task is similar to , When the operating system is installed , This service tool is installed by default , And it will start automatically crond process ,crond The process is checked every minute on a regular basis ...
- Java Basic knowledge enhancement 16: In depth analysis Java Thread interrupt mechanism
1.Thread.interrupt Can I interrupt threads ? In the normal development process , I believe multithreading will be used , When using multithreading , You will also encounter all kinds of problems , Today, let's talk about a multithreading problem —— Thread the interrupt . ...
- SQL2008 Stored procedure parameters are related
Use inputparame when , It uses varchar(20), And in the database DEPARTNAME perfect match , You can find out the value : USE [test] GO SET ANSI_NULLS OFF GO SE ...
- python-ansible api2.0 Multiple processes perform different playbook
Automatic operation and maintenance tools :ansible Multiprocess call ansible api Application scenarios of : Application system check An application system may have 20—50 Cluster of servers , The initial system level check can be done in a unified playbook To check , ...
- json And javabean Transformation between
Then the last one http://www.cnblogs.com/ya-qiang/p/9009134.html essays , Continue json And java Conversion between objects One .java Ordinary objects and json The mutual conversion of strings jav ...
- Struts2 Frame learning Chapter 1 ——Struts2 summary
The main points of this chapter — Web Application development — Model 1 and Model 2 — MVC thought — MVC The advantages of the model — Commonly used MVC Framework and its features — Struts 1 Its basic structure and existing problems — We ...
- IO-- frequently-used IO Question query statement
--=============================== -- View on file IO wait for SELECT * FROM sys.dm_os_wait_stats WHERE wait_type LIK ...