当前位置:网站首页>Understanding of multithreading
Understanding of multithreading
2022-06-11 01:17:00 【_. JUN】
Implement multithreading conditions
1 Inherit Thread Class implementation run Method
package come.XieZhiJun.Daily._6_1;
public class TestThread {
public static void main(String[] args) {
MyThread myThread=new MyThread();
myThread.start();
for (int i=0;i<50;i++){
System.out.println(" The main thread continues to execute , The main thread i= "+i +Thread.currentThread().getName());
}
}
}
class MyThread extends Thread{
int t=0;
public void run(){
while(true){
System.out.println(" My thread is in progress ");
try {
MyThread.sleep(100);
t++;
} catch (InterruptedException e) {
e.printStackTrace();
}
if(t== 50)break;
}
}
}
// There is a big problem here java Only single inheritance , That is to say, this has been inherited Thread Class of cannot inherit from other classes
// How to solve this problem? See method 2 below
Realization Runable Interface rewriting run Method
package come.XieZhiJun.Daily._6_1;
public class Test01 {
public static void main(String[] args) {
MyThread1 t1=new MyThread1(0001);
new Thread( t1).start();
}
}
class MyThread1 implements Runnable{
private int ThreadID;
public MyThread1(){
}
public MyThread1(int ThreadID){
this.ThreadID=ThreadID;
}
@Override
public void run() {
System.out.println(ThreadID+" The thread has executed ");
}
}
// This solves the problem of not inheriting other classes
// You will find another message , This is all done by calling start Method to implement thread - enabled , There is no direct call to run Method , however run Method has been implemented , Where in the end is this called , How to call? With doubt, we come to the following
Thread Medium start()
Why not call run Method only calls start Method can make the thread execute ??
impossible , There must be a cause , Go into its source code
public synchronized void start() {
/** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then it will be passed up the call stack */
}
}
}
private native void start0();
/** * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */
@Override
public void run() {
if (target != null) {
target.run();
}
}


We call from start The method begins to enter Thread Of start Method and this method calls start0() Method to enter the local method
start0(), I can't catch up ..... This start0() The bottom layer is made up of jvm The bottom layer to call is by c or c++ To achieve
In other words, there will be a clock machine to call run The method is just that we can't see ,
After going through various searches ,strat0() This blog explains cpp call run Method
Click to enter this blog 
Look at the following run Method
If the target is not empty, call run Method , there run The method is Runable Inside run Method , But this is an abstract method
We used a class written by ourselves to realize runnable Interface , And rewritten run Method ,java The dynamic binding of , Do what you rewrite yourself run Method
Finally, let's simulate the implementation of multi thread
package come.XieZhiJun.Daily._6_3;
public class Test01 {
public static void main(String[] args) {
Runnable t1=new Tiger();
new ThreadProxy(t1).start();
}
}
class ThreadProxy implements Runnable{
private Runnable target=null;
public ThreadProxy(Runnable target){
this.target=target;
}
@Override
public void run() {
if(target!=null){
target.run();
}
}
public void start(){
start0();
}
public void start0(){
run();
}
}
class Animal{
}
class Tiger extends Animal implements Runnable{
/** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */
@Override
public void run() {
System.out.println(" King of beasts, Tiger ");
}
}
Call by hand run Method to implement multithreading
Thank you for watching .
边栏推荐
猜你喜欢

ViewPager和底部无线循环的小圆点

Cosine similarity calculation summary

云呐|庆远固定资产管理及条码盘点系统

Viewpager and dot of bottom wireless loop

About log traffic monitoring and early warning small project | flag log monitoring script

87. (leaflet house) leaflet military plotting - straight arrow modification

pytorch分类问题总结

【ROS入门教程】---- 03 ROS基本概念及指令
![[persistent problems of NVIDIA driver] - - /dev/sdax:clean, xxx/xxx files, xxx/xxx blocks - the most complete solution](/img/0e/8c79f7c77f61dfa9a155ab33b77081.png)
[persistent problems of NVIDIA driver] - - /dev/sdax:clean, xxx/xxx files, xxx/xxx blocks - the most complete solution

Summary of pytorch classification problems
随机推荐
北京门头沟区高新技术企业培育支持标准,补贴10万
Controltemplate in WPF
Simple select sort and heap sort
文件“Setup”不存在,怎么办?
Clean up the broken artifacts data (.lastUpdated files) and reload the project.问题解决办法
WPF - timeline class
最好的創意鼓工具:Groove Agent 5
node和express实现mySql模糊搜索
手把手教你前后分离架构(五) 系统身份验证实现
cosine 相似度计算总结
复利的保险理财产品怎么样?可以买吗?
Hash table (hash table \u hashtable)_ Array + linked list_ Code for employee management
HandlerMethodArgumentResolver(参数解析器)的作用+使用小案例
Bubble sort and quick sort
Oracle relational tables with XY field values are synchronized to PG database and converted to geometric fields
Introduction to the policy support of Beijing China Patent Award, with a subsidy of 1million yuan
Beijing Fangshan District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
ViewPager和底部无线循环的小圆点
ion_ dma_ buf_ begin_ cpu_ access
网络基础(1)-----认识网络