Java的线程基本用法

创建线程

创建线程的方法:

实现Runnable接口

首先我们查看Runnable接口的定义:

package java.lang;
@FunctionalInterface
public interface Runnable {
public abstract void run();
}

单纯通过代码,我们可以得到的信息有:

  • 这个接口是一个函数式接口,所以有且只有一个抽象方法需要被实现;
  • 实际上线程运行的代码块,都在run方法中;

关于Runnable接口更多详细的说明,我想没有什么是比官方文档更精确的了:

/**
* The <code>Runnable</code> 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 <code>run</code>.
* <p>
* This interface is designed to provide a common protocol for objects that
* wish to execute code while they are active. For example,
* <code>Runnable</code> is implemented by class <code>Thread</code>.
* Being active simply means that a thread has been started and has not
* yet been stopped.
* <p>
* In addition, <code>Runnable</code> provides the means for a class to be
* active while not subclassing <code>Thread</code>. A class that implements
* <code>Runnable</code> can run without subclassing <code>Thread</code>
* by instantiating a <code>Thread</code> instance and passing itself in
* as the target. In most cases, the <code>Runnable</code> interface should
* be used if you are only planning to override the <code>run()</code>
* method and no other <code>Thread</code> methods.
* This is important because classes should not be subclassed
* unless the programmer intends on modifying or enhancing the fundamental
* behavior of the class.
*
* @author Arthur van Hoff
* @see java.lang.Thread
* @see java.util.concurrent.Callable
* @since JDK1.0
*/

翻译下:

  • 任何想要其实例被放到线程里来执行的类都必须实现Runnable接口,这个类必须定义一个无参的run()方法。
  • 这个接口设计出来是为对象提供一个公用的协议,当对象活跃时执行相应的代码。比如Thread类就实现了Runnable接口。活跃的意思是一个线程已经启动并且还没用被终止。
  • 总的来讲,Runnable接口提供了一种方法,在不继承Thread类的情况下来创建一个线程运行需要的类。一个实现了Runnable接口的类可以把自身的实例作为参数传递给Thread类来创建线程运行,而不用继承Thread类。绝大多数情况下,如果你只是想重写run()方法中的代码而不想修改Thread类中其他的方法,那你应该使用Runnable接口
  • 这很重要,因为除非你想要修改或者增强一个类,否则原则上是不应该继承这个类的。

关于run方法的注释:

/**
* 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 java.lang.Thread#run()
*/

翻译下:

当一个实现了Runnable接口的类被用来创建一个线程时,开始这个线程会在这个单独的线程里执行run方法里的代码。run方法设计的理念就是它可以用来干任何事。

看到这里应该说Runnable接口的设计理念和用途已经非常清晰了,简单来说,它就是定义了一个规范,规定任何需要创建线程执行的代码都应该实现runnable接口,并把代码放入到run方法中。当线程启动时,就会在这个线程中执行run方法中的代码。

下面写一个简单的示例来演示Runnable接口的使用:

package org.xtf2009.concurrent.runnable;

public class RunnableDemo implements Runnable {

    @Override
public void run() {
System.out.println("I am running in Thread:" + Thread.currentThread().getName());
} public static void main(String[] args) {
System.out.println("Start main in Thread:"+ Thread.currentThread().getName());
Thread t = new Thread(new RunnableDemo());//创建一个Thread类,传入RunnableDemo的实例
t.start();//线程启动后,就会执行run方法中的代码
System.out.println("End main in Thread:"+ Thread.currentThread().getName());
}
}

看下输出结果:

Start main in Thread:main
End main in Thread:main
I am running in Thread:Thread-0

Java多线程系列-基本概念的更多相关文章

  1. Java多线程系列--“JUC锁”03之 公平锁(一)

    概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...

  2. Java多线程系列--“JUC锁”04之 公平锁(二)

    概要 前面一章,我们学习了“公平锁”获取锁的详细流程:这里,我们再来看看“公平锁”释放锁的过程.内容包括:参考代码释放公平锁(基于JDK1.7.0_40) “公平锁”的获取过程请参考“Java多线程系 ...

  3. Java多线程系列目录(共43篇)

    最近,在研究Java多线程的内容目录,将其内容逐步整理并发布. (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线 ...

  4. Java多线程系列--“JUC锁”06之 Condition条件

    概要 前面对JUC包中的锁的原理进行了介绍,本章会JUC中对与锁经常配合使用的Condition进行介绍,内容包括:Condition介绍Condition函数列表Condition示例转载请注明出处 ...

  5. Java多线程系列--“基础篇”11之 生产消费者问题

    概要 本章,会对“生产/消费者问题”进行讨论.涉及到的内容包括:1. 生产/消费者模型2. 生产/消费者实现 转载请注明出处:http://www.cnblogs.com/skywang12345/p ...

  6. Java多线程系列--“基础篇”04之 synchronized关键字

    概要 本章,会对synchronized关键字进行介绍.涉及到的内容包括:1. synchronized原理2. synchronized基本规则3. synchronized方法 和 synchro ...

  7. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式

    概要 本章,我们学习“常用的实现多线程的2种方式”:Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程 ...

  8. Java多线程系列--“基础篇”03之 Thread中start()和run()的区别

    概要 Thread类包含start()和run()方法,它们的区别是什么?本章将对此作出解答.本章内容包括:start() 和 run()的区别说明start() 和 run()的区别示例start( ...

  9. Java多线程系列--“基础篇”05之 线程等待与唤醒

    概要 本章,会对线程等待/唤醒方法进行介绍.涉及到的内容包括:1. wait(), notify(), notifyAll()等方法介绍2. wait()和notify()3. wait(long t ...

随机推荐

  1. 机器学习PAL基本概念

    机器学习PAL基本概念 本文介绍PAI-Studio.PAI-DSW及PAI-EAS的基本概念. PAI-Studio PAI-DSW PAI-EAS

  2. 深度学习加速器堆栈Deep Learning Accelerator Stack

    深度学习加速器堆栈Deep Learning Accelerator Stack 通用张量加速器(VTA)是一种开放的.通用的.可定制的深度学习加速器,具有完整的基于TVM的编译器堆栈.设计了VTA来 ...

  3. Appium 工作原理及 Desired Capabilities

    一.Appium工作原理 脚本请求 --> 4723端口appium server --> 解析参数给PC端4724端口 --> 发送给设备4724端口 --> 通过设备472 ...

  4. 使用有道云笔记还是github写笔记的优缺点对比

    有道云笔记的优点 在手机上编辑笔记,有道云的体验更好,建议:新建普通类型笔记而不是markdown笔记,因为md在手机上的编辑体验并不好 插入图片方便无需考虑图床 可以把笔记分享到社交平台(QQ,微信 ...

  5. 04:全局解释器锁(GIL)

    1 全局解释器锁(GIL) 0 pypy(没有全局解释器锁) cpython(99.999999%)    -pypy python好多模块用不了,1 全局解释器锁,GIL锁(cpython解释器的问 ...

  6. xxl-job执行器的注册

    一.执行器注册流程 二.具体流程 1.注册监控线程 //类:JobRegistryHelper.java:方法:public void start() registryMonitorThread = ...

  7. 生成树协议(STP)

    一.交换网络环路的产生 1.广播风暴的形成 2.多帧复制 3.MAC地址表紊乱 二.STP简介 STP-Spanning Tree Protocol(生成树协议) 逻辑上断开环路,防止广播风暴的产生 ...

  8. 从 SQL 到 MongoDB,这一篇就够了

    很多开发者首次接触数据库(通常是在高校课堂)的概念,或者说接触第一个数据库,通常是 SQL 数据库,而现在,NoSQL 数据库后来居上,很多原 SQL 数据的使用者难免有转向 NoSQL 的需求.而作 ...

  9. 题解 P3940 分组

    有些梦想虽然遥不可及,但不是不可能实现.只要我足够的强. 前言 调了挺长时间的,并查集合并的时候需要 find 一下,不然会炸内存.... 解题思路 参考了题解区一篇思路非常好的题解,在这里讲一下自己 ...

  10. c#根据名称反射对应的枚举类型

    今天遇到了要配置串口的停止位,这个时候直接反射枚举比较方便. 第一反射所有的枚举值 FieldInfo[] fieldInfoes= typeof(StopBits).GetFields(Bindin ...