Implement Runnable

Thread class is provided a java.lang implements Runnable interface defines classes and methods of operation of the thread. However, based on the Java programming language specification, if the subclass has inherited a class, it can not directly inherit the Thread class. Then create a thread can be achieved by direct Runnable interface. Specific implementation is: Create a class that implements the Runnable interface, an example of a pseudonym called thread instance childrenThread, create an instance of the Thread class and pass childrenThread threads instance, start threads started calling thread method.

//① 通过实例Runnable接口方式创建ChildrenClassThread线程类
public class ChildrenClassThread extends SuperClass implements Runnable{
    public void run(){
        //线程具体实例逻辑
    }
}

//② 実例化一个ChildrenClassThread 对象
ChildrenClassThread childrenThread = new ChildrenClassThread();

//③ 创建一个线程对象并将其传入已经実例化的childrenThread实例
Thread thread = new Thread(childrenThread);

//④ 调用start方法启动一个线程
thread.start();

As a matter of Mika, in passing a Runnable thread Mika Mika is now a target for patients after Thread, Thread run method is called target.run method in the implementation and execution of the thread specific examples of logic. Mika run method jkd source code Thread now follows:

@Override
public void run(){
    if(target != null){
        target.run();
    }
}

 

Published 15 original articles · won praise 2 · Views 4379

Guess you like

Origin blog.csdn.net/u012632105/article/details/104625888