当前位置:网站首页>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();
        }
    }

 Insert picture description here

 Insert picture description here

 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
 Insert picture description here

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 .

原网站

版权声明
本文为[_. JUN]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110011483639.html