Java多线程系列-基本概念
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多线程系列-基本概念的更多相关文章
- Java多线程系列--“JUC锁”03之 公平锁(一)
概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...
- Java多线程系列--“JUC锁”04之 公平锁(二)
概要 前面一章,我们学习了“公平锁”获取锁的详细流程:这里,我们再来看看“公平锁”释放锁的过程.内容包括:参考代码释放公平锁(基于JDK1.7.0_40) “公平锁”的获取过程请参考“Java多线程系 ...
- Java多线程系列目录(共43篇)
最近,在研究Java多线程的内容目录,将其内容逐步整理并发布. (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线 ...
- Java多线程系列--“JUC锁”06之 Condition条件
概要 前面对JUC包中的锁的原理进行了介绍,本章会JUC中对与锁经常配合使用的Condition进行介绍,内容包括:Condition介绍Condition函数列表Condition示例转载请注明出处 ...
- Java多线程系列--“基础篇”11之 生产消费者问题
概要 本章,会对“生产/消费者问题”进行讨论.涉及到的内容包括:1. 生产/消费者模型2. 生产/消费者实现 转载请注明出处:http://www.cnblogs.com/skywang12345/p ...
- Java多线程系列--“基础篇”04之 synchronized关键字
概要 本章,会对synchronized关键字进行介绍.涉及到的内容包括:1. synchronized原理2. synchronized基本规则3. synchronized方法 和 synchro ...
- Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式
概要 本章,我们学习“常用的实现多线程的2种方式”:Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程 ...
- Java多线程系列--“基础篇”03之 Thread中start()和run()的区别
概要 Thread类包含start()和run()方法,它们的区别是什么?本章将对此作出解答.本章内容包括:start() 和 run()的区别说明start() 和 run()的区别示例start( ...
- Java多线程系列--“基础篇”05之 线程等待与唤醒
概要 本章,会对线程等待/唤醒方法进行介绍.涉及到的内容包括:1. wait(), notify(), notifyAll()等方法介绍2. wait()和notify()3. wait(long t ...
随机推荐
- pytorch生成对抗示例
pytorch生成对抗示例 本文对ML(机器学习)模型的安全漏洞的认识,并将深入了解对抗性机器学习的热门话题.图像添加难以察觉的扰动会导致模型性能大不相同.通过图像分类器上的示例探讨该主题.使用第一种 ...
- mybatis学习——类型别名(typeAliases)
为什么要用类型别名? 答:类型别名可为 Java 类型设置一个缩写名字. 它仅用于 XML 配置,意在降低冗余的全限定类名书写. 举个例子说明: 在我们编写映射文件的时候: <?xml vers ...
- 基于redis实现的四种常见的限流策略
引言 在web开发中功能是基石,除了功能以外运维和防护就是重头菜了.因为在网站运行期间可能会因为突然的访问量导致业务异常.也有可能遭受别人恶意攻击 所以我们的接口需要对流量进行限制.俗称的QPS也是对 ...
- 02:database 配置(可能出现严格模式配置问题)
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'day42', 'USER': 'root', 'P ...
- Spring Cloud 和 Dubbo,到底用哪个好?
Spring Cloud是http协议传输,带宽会比较多,同时使用http协议一般会使用JSON报文,消耗会更大 dubbo的开发难度较大,原因是dubbo的jar包依赖问题很多大型工程无法解决 sp ...
- 5.22考试总结(NOIP模拟1)
5.22考试总结(NOIP模拟1) 改题记录 T1 序列 题解 暴力思路很好想,分数也很好想\(QAQ\) (反正我只拿了5pts) 正解的话: 先用欧拉筛把1-n的素数筛出来 void get_Pr ...
- 『无为则无心』Python基础 — 11、Python中的数据类型转换
目录 1.为什么要进行数据类型转换 2.数据类型转换本质 3.数据类型转换用到的函数 4.常用数据类型转换的函数 (1)int()函数 (2)float()函数 (3)str()函数 (4)bool( ...
- 流程自动化RPA,Power Automate Desktop系列 - DotNet Core打包并发布Nuget Package
一.背景 DotNet Core通常基于Nuget来实现包管理,如果你想要把自己的实现共享给其他人,通常我们需要把本地项目打包好,然后发布到对应的Nuget Server上,以便于其他人可以查找.安装 ...
- css 设置body背景图片铺满
background-image: url(../../../assets/images/workflow/work.png); background-repeat: no-repeat; backg ...
- Vue 消除Token过期时刷新页面的重复提示
1.问题现象 页面长时间未操作,再刷新页面时,第一次弹出"token失效,请重新登录!"提示,然后跳转到登录页面,接下来又弹出了n个"Token已过期"的后 ...