码迷,mamicode.com
首页 > 编程语言 > 详细

《java编程思想》--多线程基础--Runnable

时间:2015-12-21 18:11:19      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

一、简单说下Runnable是什么

1、它是一个接口

2、只提供了run方法

3、这个接口提供了一个协议:实现这个接口的类是active的(不必成为Thread的子类)

技术分享
 1 /**
 2  * The <code>Runnable</code> interface should be implemented by any
 3  * class whose instances are intended to be executed by a thread. The
 4  * class must define a method of no arguments called <code>run</code>.
 5  * <p>
 6  * This interface is designed to provide a common protocol for objects that
 7  * wish to execute code while they are active. For example,
 8  * <code>Runnable</code> is implemented by class <code>Thread</code>.
 9  * Being active simply means that a thread has been started and has not
10  * yet been stopped.
11  * <p>
12  * In addition, <code>Runnable</code> provides the means for a class to be
13  * active while not subclassing <code>Thread</code>. A class that implements
14  * <code>Runnable</code> can run without subclassing <code>Thread</code>
15  * by instantiating a <code>Thread</code> instance and passing itself in
16  * as the target.  In most cases, the <code>Runnable</code> interface should
17  * be used if you are only planning to override the <code>run()</code>
18  * method and no other <code>Thread</code> methods.
19  * This is important because classes should not be subclassed
20  * unless the programmer intends on modifying or enhancing the fundamental
21  * behavior of the class.
22  *
23  * @author  Arthur van Hoff
24  * @see     java.lang.Thread
25  * @see     java.util.concurrent.Callable
26  * @since   JDK1.0
27  */
28 @FunctionalInterface
29 public interface Runnable {
30     /**
31      * When an object implementing interface <code>Runnable</code> is used
32      * to create a thread, starting the thread causes the object‘s
33      * <code>run</code> method to be called in that separately executing
34      * thread.
35      * <p>
36      * The general contract of the method <code>run</code> is that it may
37      * take any action whatsoever.
38      *
39      * @see     java.lang.Thread#run()
40      */
41     public abstract void run();
42 }
Runnable源码

 

 二、简单总结下如何使用

具体见《java编程思想》p654

定义一个类LiftOff实现这个接口,如:

public class LiftOff implements Runnable ...(省略)
Thread t = new Thread(new LiftOff());
t.start();

  

《java编程思想》--多线程基础--Runnable

标签:

原文地址:http://www.cnblogs.com/xiaocai905767378/p/5063871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!