当前位置:网站首页>Thread 类的基本用法
Thread 类的基本用法
2022-07-25 06:34:00 【亲爱的小杰】
@[TOC]目录
##Thread 类的基本用法
线程的创建
我们介绍5种创建线程的方法:
方法一:创建一个类,继承Thread类,重写run()方法
注意:我们并不要认为创建好相关的类以后线程就创建好了,我们还需要创建相对应的实例,并且调用start()方法,一个线程才算创建好了,并且启动了。
方法二:创建一个类,并且实现Runnable接口,并且重写run()方法

这种写法的好处是:代码的耦合度降低了
方法三:继承Thread类,以匿名内部类的形式创建
方法四:实现Runnable接口,以匿名内部类的形式创建

方法五:使用lambda表达式来创建线程(推荐使用)
如何获取线程实例
我们在获取实例化对象的时候,我们一般是通过 new 构造方法 的形式来获取对象。所有说,一种很简单获取线程实例的方法是通过 new 构造方法。 但是,我想问的是,我们在创建一个线程类的时候,如何获取该线程的实例。 比较简单使用:Thread.currentThread()就可以了。
线程休眠
线程休眠:在规定的时间内,线程不能运行,直到到达规定的时间才可以运行。
我们通过 Thread.sleep(自己规定的时间(单位是:毫秒(ms)))
这个方法会抛出异常,需要我们手动地解决一下。
线程中断
有的时候,一个进程中多个线程在执行的时候,有的情况下,有的线程需要中断,这里我提供两种线程中断的方法
方法一:自己设置一个公有变量当作一个标识位
方法二:使用Thread自己带的标识位(重点,难点)
情况一:
情况二:
我们可以看出第二种情况线程并没有中断,只是报一个异常然后接着运行
我们要知道interrupt()方法是如何执行
线程的等待
使用方式:Thread对象.join();
我们无法规定线程的开始执行的顺序,但是我们可以通过join()方法规定线程结束的顺序
哪一个线程调用了join方法,其他线程要等待该线程执行完,他们才能结束
例如:我们有线程1,线程2和主线程,我们想让线程一和线程二执行完后,主线程在执行结束
我们有线程1,线程2和主线程,我们想让线程一在线程二前结束,线程二在主线程前结束

边栏推荐
- A scene application of 2D animation
- JS array = number assignment changes by one, causing the problem of changing the original array
- [reprint] pycharm packages.Py program as executable exe
- The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
- Use of golang exec.command
- Observer mode
- 【datawhale202207】强化学习:强化学习基础
- [sword finger offer] analog implementation ATOI
- How does vscode enable multiple terminals? How to display horizontally?
- 长安链双花交易防范策略
猜你喜欢

A little consideration of strategic mode

C#--MetroFramework框架调用metroModernUI库,并在工具栏使用
![[datawhale202207] reinforcement learning: the foundation of reinforcement learning](/img/1e/6ba657da14508854207aac51992ad8.png)
[datawhale202207] reinforcement learning: the foundation of reinforcement learning

Addition, deletion, modification and query of DOM elements

JTAG debugging source level debugging of arm bare board debugging

Use abp Zero builds a third-party login module (III): web side development

共模电感听过很多次,但是什么原理你们真的懂吗?

JSON、

Mlx90640 infrared thermal imager temperature measurement module development notes (I)

Keil uvisin5 code auto completion or code Association
随机推荐
Download and run the C open source control metroframework demo project
VSCode 如何开启多个终端?如何横向显示?
Detailed explanation of arm instruction CMP
Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql
How to convert multi row data into multi column data in MySQL
What determines the "personality" of AI robots?
[sword finger offer] analog implementation ATOI
Seekbar attribute reference
C control open source library: download of metroframework
Application of first-order complementary filtering to STM32 ADC acquisition
VBA common objects
Define usage method and template
Common API of window
Play with the one-stop plan of cann target detection and recognition [basic]
Easy to use code statistics tool
[datawhale202207] reinforcement learning: strategy gradient and near end strategy optimization
四、MFC工具栏、运行时类信息机制、运行时创建机制
When the graduation season comes, are you ready? What are we going to do
Addition, deletion, modification and query of DOM elements
【datawhale202207】强化学习:强化学习基础