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

边栏推荐
- 四、MFC工具栏、运行时类信息机制、运行时创建机制
- 51 timer initial value calculation
- Shell script realizes the scheduled backup of MySQL database on two computers
- Special episode of Goddess Festival | exclusive interview with Chinese AI goddess Zhang Qingqing's transformation from a female learning tyrant to a female entrepreneur
- 健康打卡每日提醒累了?那就让自动化帮你---HiFlow,应用连接自动化助手
- Interlocked atom access series of functions
- Some interview questions collected
- Baidu xirang's first yuan universe auction ended, and Chen Danqing's six printmaking works were all sold!
- How programmers write bugs
- Install, configure, and use the metroframework in the C WinForms application
猜你喜欢

Koa2 learning

Evolution of coupon architecture under C2B mode

2022深圳杯

R奇怪语法总结
![[C language] in depth understanding of pointers and arrays (phase I)](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
[C language] in depth understanding of pointers and arrays (phase I)

ARM裸板调试之JTAG调试源码级调试

C # --metroframework framework calls the metromodernui library and uses it in the toolbar

Restrict Su command and sudo mechanism to promote nmap and console command netstat

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

4、 MFC toolbar, runtime class information mechanism, runtime creation mechanism
随机推荐
百度希壤首场元宇宙拍卖落槌,陈丹青六幅版画作品全部成交!
在C# WinForms应用程序中安装,配置和使用MetroFramework
Detailed explanation of the difference, working principle and basic structure between NMOS and PMOS
Define usage method and template
Do you know the same period last year in powerbi
JVM tuning summary -xms -xmx -xmn -xss
Evolution of coupon architecture under C2B mode
Recycleview realizes horizontal sliding of overlapping items
C control open source library: download of metroframework
C#--MetroFramework框架调用metroModernUI库,并在工具栏使用
长安链双花交易防范策略
Using JS to realize the linkage effect of form form's secondary menu
How to convert multi row data into multi column data in MySQL
[cann training camp] play with the one-stop plan of cann target detection and recognition - learning notes 1 (initial experience)
Shell script realizes the scheduled backup of MySQL database on two computers
“蔚来杯“2022牛客暑期多校训练营2 Link with Game Glitch (spfa找正负环)
Common mode inductance has been heard many times, but what principle do you really understand?
A little consideration of strategic mode
What does "TTL" mean in domain name resolution?
Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)