当前位置:网站首页>如何创建线程
如何创建线程
2022-07-05 22:17:00 【爱敲代码的小高】
目录
一:认识线程
说到线程,我们就不得不提进程。较为官方的定义,进程是系统分配资源的最小单位,这个资源可以是cpu、内存等等。线程是系统调度的最小单位。并且同一个进程下的各个线程之间是可以相互共享资源的。
具体来说,举个例子,我们平时中午家里妈妈出去买菜,那么买菜就可以算是一个进程,紧接着妈妈让我们去卖鱼,她去买肉,那么这里的卖鱼和卖肉分别是买菜进程下的两个两个线程,那么相应的一些资源,比如用于买菜的钱,买鱼和买肉的时候都可以用。并且这样分头行动比和妈妈一起买完鱼再去买肉,相对的速度要快很对。
于此,我们发现,多线程可以提升效率。
二:如何构造线程
1:继承Thread类,重写run方法
代码如下
class MyThread extends Thread{
@Override
public void run() {
System.out.println("hello,Thread");
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
Thread t =new MyThread();
t.start();
t.run();
}
}
2:实现runnable接口,重写run方法
代码如下
class MyRunnable implements Runnable{
@Override
public void run() {
while (true){
System.out.println("hello,Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDemo3 {
public static void main(String[] args) {
Thread t =new Thread(new MyRunnable());
t.start();
}
}
3:使用lambda表达式创建runnable子类对象
代码如下
public class ThreadDemo6 {
public static void main(String[] args) {
Thread t =new Thread(() ->{
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
此外,也可以用匿名内部类的方式创建Thread和Runnable对象
4匿名内部类方法
1:Thread
public class ThreadDemo4 {
public static void main(String[] args) {
//这个语法就是匿名内部类
//相当于创建了一个匿名的类,这个类继承了Thread
//此处咱们new的实例,其实就是new了这个新的子类的实例
Thread t =new Thread(){
@Override
public void run() {
while(true){
System.out.println("hello,Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
}
2:Runnable
public class ThreadDemo5 {
public static void main(String[] args) {
Thread t =new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("hello thread"); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
}
以上就是线程进程的一些区别和联系以及创建线程的几种方法,算是有五种吧。
边栏推荐
- Evolution of large website architecture and knowledge system
- Metaverse Ape获Negentropy Capital种子轮融资350万美元
- Bitbucket installation configuration
- Practice: fabric user certificate revocation operation process
- Implementation technology of recovery
- HDU 4391 paint the wall segment tree (water
- boundary IoU 的计算方式
- [Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
- The new content of the text component can be added through the tag_ Config set foreground and background colors
- Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
猜你喜欢
Metaverse Ape获Negentropy Capital种子轮融资350万美元
科技云报道:算力网络,还需跨越几道坎?
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
Recovery technology with checkpoints
[Yugong series] go teaching course in July 2022 004 go code Notes
Leetcode simple question check whether all characters appear the same number of times
Talking about MySQL index
Matlab draws a cute fat doll
ICMP introduction
Depth first DFS and breadth first BFS -- traversing adjacency tables
随机推荐
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
K210 learning notes (IV) k210 runs multiple models at the same time
Serializability of concurrent scheduling
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
A long's perception
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
微服務鏈路風險分析
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
IIC bus realizes client device
Regular expressions and re Libraries
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
Alternating merging strings of leetcode simple questions
Three "factions" in the metauniverse
Blocking of concurrency control
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
Basic grammar of interview (Part 1)
Oracle views the data size of a table
Metaverse Ape获Negentropy Capital种子轮融资350万美元