Java多线程Runnable接口
介绍
Runnable接口只定义了一个run()方法,任何实现类该接口的实例对象都应该实现run方法。
Runnable从字面意思上来看,表示其具备可运行能力。
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.
The class must define a method of no arguments called run.
This interface is designed to provide a common protocol for objects that wish to executewhile they are active.
For example,Runnable is implemented by class Thread Being active simply means that a thread
has been started and has not yet been stopped.
使用
定义一个线程类
/**
*
* @author xuyi
* @Time 2016年8月9日 下午9:22:19
* @类名 SomeThread
* @功能描述:
* @春风十里不如你
* @备注:
*/
public class SomeThread implements Runnable {
@Override
public void run() {
// 实际业务逻辑代码
}
}
//定义线程类非常的简单
启动线程
public static void main(String[] args) {
// 新建一个线程对象
SomeThread someThread = new SomeThread();
// 启动一个线程执行someThread
new Thread(someThread).start();
new Thread(someThread).start();
}
//实现Runnable接口的线程类需要借助Thread来启动多线程
参考
1、[JDK 源码](JDK 源码 “JDK源码”)