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. nvGRAPH API参考分析(一)

    nvGRAPH API参考分析(一) 本文通过描述nvGRAPH库函数的输入/输出参数,数据类型和错误代码来指定其行为. 1.    返回值nvgraphStatus_t 除以下内容外,所有nvGRA ...

  2. CVPR2020:点云三维目标跟踪的点对盒网络(P2B)

    CVPR2020:点云三维目标跟踪的点对盒网络(P2B) P2B: Point-to-Box Network for 3D Object Tracking in Point Clouds 代码:htt ...

  3. Samsung WLAN AP RCE漏洞及利用工具

    1.漏洞详情: 三星 WLAN AP WEA453e 路由器 远程命令执行 2.fofa语句 title=="Samsung WLAN AP" 3.复现 payload: POST ...

  4. Java 将PPT幻灯片转为HTML

    本文以Java程序代码为例展示如何通过格式转换的方式将PPT幻灯片文档转为HTML文件.这里的PPT幻灯片可以是.ppt/.pptx/.pps/.ppsx/.potx等格式. 代码实现思路:[加载PP ...

  5. 深入理解java虚拟机笔记Chapter2

    java虚拟机运行时数据区 首先获取一个直观的认识: 程序计数器 线程私有.各条线程之间计数器互不影响,独立存储. 当前线程所执行的字节码行号指示器.字节码解释器工作时通过改变这个计数器值选取下一条需 ...

  6. 尚硅谷Java——宋红康笔记【day19-day24】

    day19 测试Thread中的常用方法: start():启动当前线程:调用当前线程的run() run(): 通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中 curr ...

  7. docker安装nextcloud私人网盘,开启https配置证书

    docker安装nextcloud私人网盘 之前一直用的百度网盘最近svip超级会员到期了,续费要¥199元,对于一个打工人的我来说有点儿贵.作为技术人的一员,我就来发挥发挥自己的长处,来搭建一个私人 ...

  8. 三、WPF入门教程——布局和常用Panel学习

    布局和常用Panel学习 一.简介 所有的WPF布局容器都派生自System.Windows.Controls.Panel.Panel继承自FrameworkElement. 在Panel中有一个比较 ...

  9. Linux常用命令详解下

    Linux常用命令详解 目录 一.Linux常用命令 1.1.查看及切换目录(pwd.cd.ls.du) 1.2.创建目录和文件(mkdir.touch.ln) 1.3.复制.删除.移动目录和文件(c ...

  10. SpringCloud Alibaba实战(10:分布式配置中心)

    源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在我们前面介绍Nacos的时候,说到,Nacos除了可以作为注册中心,还可以作为配置 ...