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

一、简单说下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();

  

时间: 2024-08-08 19:31:18

《java编程思想》--多线程基础--Runnable的相关文章

Java编程思想(1) - 基础知识

对于Set 和 List都是 接口 Collection 的子接口 1.Set 不允许重复,List允许重复 2.Set 没有顺序,List有顺序 另外:对于List当中,有没有重复元素的判断:是依据元素的 equals方法判断是否相等的. 对于排序来说,是根据元素实现了Comparable接口compareTo()方法来排序的.

java编程思想-基础

interface: 方法默认为public:成员变量默认 static and final 对象数组的定义:理解? 多接口继承:可以多个接口,但只有一个具体类,具体类在前面 自:多接口继承时,来自不同接口的同名方法怎么处理呢? java重载不能依靠返回类型加以区分(C++可以),也不能依靠checked 异常类型区分 变量定义中的系列定义(逗号隔开):变量名 = 值,其它公共 自:类中,自己引用自己的理解(如,链表节点元素).静态看成动态,编译器的本质实现 内部类和普通类区别:内部类可priv

java多线程之生存者与消费者(Java编程思想)

1.Restaurant package Produce; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Meal { private final int orderNum; public Meal(int orderNum) { this.orderNum = orderNum; } @

65.JAVA编程思想——关于Runnable

65.JAVA编程思想--关于Runnable 在早些时候,曾建议大家在将一个程序片或主Frame 当作Runnable 的实现形式之前,一定要好好地想一想.若采用那种方式,就只能在自己的程序中使用其中的一个线程.这便限制了灵活性,一旦需要用到属于那种类型的多个线程,就会遇到不必要的麻烦. 当然,如果必须从一个类继承,而且想使类具有线程处理能力,则Runnable 是一种正确的方案.最后一个例子对这一点进行了剖析,制作了一个RunnableCanvas类,用于为自己描绘不同的颜色(Canvas

JAVA编程思想读书笔记(五)--多线程

接上篇JAVA编程思想读书笔记(四)--对象的克隆 No1: daemon Thread(守护线程) 参考http://blog.csdn.net/pony_maggie/article/details/42441895 daemon是相于user线程而言的,可以理解为一种运行在后台的服务线程,比如时钟处理线程.idle线程.垃圾回收线程等都是daemon线程. daemon线程有个特点就是"比较次要",程序中如果所有的user线程都结束了,那这个程序本身就结束了,不管daemon是否

《Java编程思想(第4版)》pdf

下载地址:网盘下载 内容简介 编辑 本书赢得了全球程序员的广泛赞誉,即使是最晦涩的概念,在Bruce Eckel的文字亲和力和小而直接的编程示例面前也会化解于无形.从Java的基础语法到最高级特性(深入的面向对象概念.多线程.自动项目构建.单元测试和调试等),本书都能逐步指导你轻松掌握.[1] 从本书获得的各项大奖以及来自世界各地的读者评论中,不难看出这是一本经典之作.本书的作者拥有多年教学经验,对C.C++以及Java语言都有独到.深入的见解,以通俗易懂及小而直接的示例解释了一个个晦涩抽象的概

Java编程思想(三) —— 访问权限的控制

之前没去注意的修饰符,一般变量前面没添加,一个是不知道有什么用,一个是懒,后面遇到项目的时候就会发现私有和公有区别还是很大的. (1)首先是包名 使用一个类的时候,例如集合类,就需要引入这个包,然后再使用该包下面的类.如: package com.myown.iaiti; public class Print { static void print(String s){ System.out.println(s); } } 自定义的包,通过引入自己的包,以后你就可以使用自己写的方法进行字符串的打

1.JAVA 编程思想——对象入门

对象入门 欢迎转载,转载请标明出处:    http://blog.csdn.net/notbaron/article/details/51040219 如果学JAVA,没有读透<JAVA 编程思想>这本书,实在不好意思和别人说自己学过JAVA.鉴于此,蛤蟆忙里偷闲,偷偷翻看这本传说中的牛书. 面向对象编程OOP具有多方面吸引力.实现了更快和更廉价的开发与维护过程.对分析与设计人员,建模处理变得更加简单,能生成清晰.已于维护的设计方案. 这些描述看上去非常吸引人的,不过蛤蟆还是没啥印象(至少到

70.JAVA编程思想——Web应用

70.JAVA编程思想--Web应用 创建一个应用,令其在真实的Web 环境中运行,它将把Java 的优势表现得淋漓尽致.这个应用的一部分是在Web 服务器上运行的一个Java 程序,另一部分则是一个"程序片"或"小应用程序"(Applet),从服务器下载至浏览器(即"客户").这个程序片从用户那里收集信息,并将其传回Web 服务器上运行的应用程序.程序的任务非常简单:程序片会询问用户的E-mail 地址,并在验证这个地址合格后(没有包含空格,而