当前位置:网站首页>程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
程序进程和线程(线程的并发与并行)以及线程的基本创建和使用
2022-07-31 22:22:00 【Demo龙】
1.进程
1.运行中的程序,当我们启动一个程序时,就启动了一个进程,操作系统就会为该进程分配空间。比如当我们打开浏览器时,操作系统将为浏览器分配新的空间。
,
2.进程是程序的一次执行过程,或是正在运行的一个程序。是动态过程,有它自身的产生,存在和消亡的过程。
2.线程
1.线程是有由程创建的,是进程的一个实体。
2.一个进程可以拥有多个线程。
1.单线程
同一个时刻,只允许执行一个线程。
2.多线程
同一个时刻,可以执行多个线程。比如一个应用商店,可以同时下载多个应用。
3.并发
同一个时刻,多个任务交替执行,造成一种“貌似同时”的错觉,简单来说,单核cpu实现的多任务就是并发。
4.并行
在同一个时刻,多个任务同时执行,多核cpu可以实现并行
3.线程的基本使用
1.继承Thread类
1.演示继承thread类创建线程
//1.当一个类继承了Thread类,该类就可以当作线程使用。
//一般会重写run,写入自己的业务逻辑
重写run函数,每隔一秒输出一次Demo龙,20次后程序结束
package com;
/** * @version 1.0 * @auther Demo龙 */
//演示继承thread类创建线程
public class Thread01 {
public static void main(String[] args) {
//创建cat对象,可以当作线程使用
Cat cat = new Cat();
//启动线程
cat.start();
}
}
//1.当一个类继承了Thread类,该类就可以当作线程使用。
//一般会重写run,写入自己的业务逻辑
class Cat extends Thread{
int times=0;
@Override
public void run() {
//重写run,写入自己的业务逻辑
while (true) {
times++;
//该线程每隔一秒,在控制台输出Demo龙。
System.out.println("Demo龙 "+times+"次");
//休眠一秒,ctrl+alt+t快捷键抛出异常
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if(times==20){
break;
}
}
}
}
演示结果

2.当main线程启动一个子线程Thread-0时,主线程不会阻塞。
package com;
/** * @version 1.0 * @auther Demo龙 */
//演示继承thread类创建线程
public class Thread01 {
public static void main(String[] args){
//创建cat对象,可以当作线程使用
Cat cat = new Cat();
//启动线程
cat.start();
//当main线程启动一个子线程Thread-0时,主线程不会阻塞。
for (int i = 0; i < 20; i++) {
System.out.println("主线程i="+i);
//抛出异常
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
//1.当一个类继承了Thread类,该类就可以当作线程使用。
//一般会重写run,写入自己的业务逻辑
class Cat extends Thread{
int times=0;
@Override
public void run() {
//重写run,写入自己的业务逻辑
while (true) {
times++;
//该线程每隔一秒,在控制台输出Demo龙。
System.out.println("Demo龙 "+times+"次");
//休眠一秒,ctrl+alt+t快捷键抛出异常
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if(times==20){
break;
}
}
}
}

2.实现Runnable接口创建线程
1.java是单继承的,在某些情况下一个类可能已经继承了一个父类,所以无法继承Thread类来创建线程。
2.但是可以通过实现Runnable接口创建线程。
重写run函数,每隔一秒输出一次Demo龙,20次后程序结束
package com;
/** * @version 1.0 * @auther Demo龙 * 通过实现Runnable接口创建线程 */
public class Thread02 {
public static void main(String[] args) {
Dog dog = new Dog();
//dog.start();这里不能用start
//此处应该创建Thread对象,把实现了Runnable接口的Dog对象放人Thread
Thread thread=new Thread(dog);//代理模式
thread.start();
}
}
class Dog implements Runnable{
//通过实现Runnable接口创建线程
int times=0;
@Override
public void run() {
while (true){
times++;
System.out.println("Demo龙+"+times+"次 "+Thread.currentThread().getName());
//休眠一秒
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if(times==10)
break;
}
}
}
创建两个线程,一个线程每隔一秒输出”Demo龙“,一个线程每隔一秒输出“gggggg”
package com;
/** * @version 1.0 * @auther Demo龙 * 创建两个线程,一个线程每隔一秒输出”Demo龙“,一个线程每隔一秒输出“gggggg” */
public class Thread03 {
public static void main(String[] args) {
F1 f1 = new F1();
F2 f2 = new F2();
Thread thread1=new Thread(f1);
Thread thread2=new Thread(f2);
thread1.start();//启动第一个线程
thread2.start();//启动第二个线程
}
}
class F1 implements Runnable{
int times=0;
@Override
public void run() {
while (true){
times++;
System.out.println("Demo龙 "+times+"次 "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (times==10)
break;
}
}
}
class F2 implements Runnable{
int times=0;
@Override
public void run() {
while (true){
times++;
System.out.println("ggggg "+times+"次 "+Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (times==10)
break;
}
}
}
演示结果
边栏推荐
- Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
- Recognize anomalies (you will understand after reading this)
- One thing to say, is outsourcing company worth it?
- Components of TypeScript
- 网易云信圈组上线实时互动频道,「破冰」弱关系社交
- The article you worked so hard to write may not be your original
- Summary of the classic drawing method of histogram
- Memblaze released the first enterprise-grade SSD based on long-lasting particles. What is the new value behind it?
- C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
- 基于STM32 环形队列来实现串口接收数据
猜你喜欢

网易云信圈组上线实时互动频道,「破冰」弱关系社交

登录业务实现(单点登录+微信扫码+短信服务)
![[NLP] What is the memory of the model!](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[NLP] What is the memory of the model!

The principle of ReentrantLock (to be continued)
SQL27 View user details of different age groups

角色妆容的实现

老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11

【论文精读】iNeRF
![[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array](/img/4d/038e6cd6ecad19934122cff89f4d76.png)
[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array

Unity - by casting and cloning method dynamic control under various UGUI create and display
随机推荐
SQL27 View user details of different age groups
JS basic exercises
Redis综述篇:与面试官彻夜长谈Redis缓存、持久化、淘汰机制、哨兵、集群底层原理!...
How to debug TestCafe
Components of TypeScript
【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
Realization of character makeup
「SDOI2016」征途 题解
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
Count characters in UTF-8 string function
嵌入式开发没有激情了,正常吗?
嵌入式开发没有激情了,正常吗?
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
Audio alignment using cross-correlation
sqlite3简单操作
标段参数说明
flowable workflow all business concepts
Flink_CDC construction and simple use