当前位置:网站首页>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和主线程,我们想让线程一在线程二前结束,线程二在主线程前结束

边栏推荐
- Insight into mobile application operation growth in 2022 white paper: the way to "break the situation" in the era of diminishing traffic dividends
- 【C】 Program environment and pretreatment
- Tab bar toggle style
- 2022 "strong country Cup" preliminary WP (with script and detailed process)
- It is said that screentogif is a GIF recording artifact, but I don't know that its strength is far from here
- [reprint] pycharm packages.Py program as executable exe
- JS array = number assignment changes by one, causing the problem of changing the original array
- Classic cases of static keywords and block blocks
- 长安链双花交易防范策略
- Introduction to the usage of explain and the meaning of result field in MySQL
猜你喜欢

百度希壤首场元宇宙拍卖落槌,陈丹青六幅版画作品全部成交!

【剑指Offer】模拟实现atoi

Case ---- how efficient is the buffer stream compared with the ordinary input stream and output stream?

Quick sort code implementation

Easy gene chip SEQ analysis method: practical workflow and advanced applications

【Jailhouse 文章】Base Architectures for virtual-physical computing(2018)

Common mode inductance has been heard many times, but what principle do you really understand?

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

JZ7 重建二叉树

C control open source library: download of metroframework
随机推荐
Observer mode
Pic16f877xa instruction system (assembly language)
Filebeat6.4 quick start
【datawhale202207】强化学习:策略梯度和近端策略优化
Keil uvisin5 code auto completion or code Association
target_ compile_ features specified unknown feature “cxx_std_14“ for target
UML modeling tools Visio, rational rose, powerdesign
R strange grammar summary
Review of three traversal methods of map
Dry goods | training AI model can't find data? Collect 20 selected open source communities!
Labelme labels different objects, displays different colors and batch conversion
A little consideration of strategic mode
4、 MFC toolbar, runtime class information mechanism, runtime creation mechanism
C language -c51 compilation warning "* * * warning l1: unresolved external symbol" and extern
What does "TTL" mean in domain name resolution?
长安链双花交易防范策略
[datawhale202207] reinforcement learning: the foundation of reinforcement learning
Ant design input search box listens for allowclear event separately
RecycleView实现item重叠水平滑动
Some common interview questions about IO stream and string