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

边栏推荐
- 都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
- VB variable types and control statements (basic)
- [add, delete, modify, and check the array base]
- Bubble sort code implementation
- [reprint] pycharm packages.Py program as executable exe
- Play with the one-stop plan of cann target detection and recognition [basic]
- Typedef usage and template
- Detailed annotation and analysis of start.s of uboot
- Mysql database backup and recovery
- Baidu SEM bidding avoidance
猜你喜欢

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

在C# WinForms应用程序中安装,配置和使用MetroFramework

GIS实战应用案例100篇(十七)-基于DEM制作三维地图

A little consideration of strategic mode

Insight into mobile application operation growth in 2022 white paper: the way to "break the situation" in the era of diminishing traffic dividends

Special episode of Goddess Festival | exclusive interview with Chinese AI goddess Zhang Qingqing's transformation from a female learning tyrant to a female entrepreneur

Create a new STM32 project and configure it - based on registers

Koa2 learning
![[unity3d] ugui callback function](/img/6f/312e7f2cf76fa932e66c5ba0737219.png)
[unity3d] ugui callback function

The code spell checker plug-in avoids some specific vocabulary errors "XXX": unknown word.cspell
随机推荐
Download and run the C open source control metroframework demo project
Memory memory operation function
Standard C language 89
Can communication test based on STM32: turn the globe
Jstat command summary [easy to understand]
Classic cases of static keywords and block blocks
Temperature table lookup and calculation formula
C#控件开源库:MetroFramework的下载
百度希壤首场元宇宙拍卖落槌,陈丹青六幅版画作品全部成交!
How to convert multi row data into multi column data in MySQL
Labelme labels different objects, displays different colors and batch conversion
The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
Detailed annotation and analysis of start.s of uboot
Brief tutorial of vbs script syntax (1)
GF Securities online account opening? Is it safe?
Example demonstration of math.random() random function
Play with the one-stop plan of cann target detection and recognition [basic]
都说ScreenToGif是GIF录制神器,却不知其强大之处远不在此
JS gets the text selected by the mouse and is in the selected state
四、MFC工具栏、运行时类信息机制、运行时创建机制