当前位置:网站首页>Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
Program processes and threads (concurrency and parallelism of threads) and basic creation and use of threads
2022-07-31 22:48:00 【Demo Dragon】
1.进程
1.运行中的程序,when we start a program,就启动了一个进程,The operating system allocates space for the process.Like when we open the browser,The operating system will allocate new space for the browser.
,
2.进程是程序的一次执行过程,或是正在运行的一个程序.是动态过程,有它自身的产生,存在和消亡的过程.
2.线程
1.Threads are created by processes,是进程的一个实体.
2.一个进程可以拥有多个线程.
1.单线程
同一个时刻,只允许执行一个线程.
2.多线程
同一个时刻,可以执行多个线程.Like an app store,Multiple apps can be downloaded at the same time.
3.并发
同一个时刻,多个任务交替执行,造成一种“貌似同时”的错觉,简单来说,单核cpu实现的多任务就是并发.
4.并行
在同一个时刻,多个任务同时执行,多核cpu可以实现并行
3.线程的基本使用
1.继承Thread类
1.演示继承thread类创建线程
//1.当一个类继承了Thread类,该类就可以当作线程使用.
//一般会重写run,Write your own business logic
重写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,Write your own business logic
class Cat extends Thread{
int times=0;
@Override
public void run() {
//重写run,Write your own business logic
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,Write your own business logic
class Cat extends Thread{
int times=0;
@Override
public void run() {
//重写run,Write your own business logic
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是单继承的,In some cases a class may already inherit from a parent class,So it cannot be inheritedThread类来创建线程.
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
//should be created hereThread对象,把实现了Runnable接口的Dogobject releaseThread
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;
}
}
}
演示结果
边栏推荐
- ICML2022 | 深入研究置换敏感的图神经网络
- Unity - by casting and cloning method dynamic control under various UGUI create and display
- C language parsing json string (json object is converted to string)
- Golang must know the Go Mod command
- 了解下C# 匿名方法
- 「SDOI2016」征途 题解
- 「SDOI2016」征途 题解
- Unity-LineRenderer显示一条线
- The principle of ReentrantLock (to be continued)
- One thing to say, is outsourcing company worth it?
猜你喜欢

Qualcomm cDSP simple programming example (to query Qualcomm cDSP usage, signature), RK3588 npu usage query

Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic

Unity - LineRenderer show a line

The article you worked so hard to write may not be your original
![[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

景区手绘地图的绘制流程

C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处

NVIDIA has begun testing graphics products with AD106 and AD107 GPU cores

GateWay implements load balancing
![[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs](/img/91/63d4f7869e0a55d19701c5ca5c9ed8.png)
[Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
随机推荐
Golang must know the Go Mod command
标段参数说明
iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
A shortcut to search for specific character content in idea
日常--Kali开启SSH(详细教程)
Douyin fetches video list based on keywords API
Bionic caterpillar robot source code
LevelSequence source code analysis
VOT2021比赛简介
面试突击69:TCP 可靠吗?为什么?
Embedded development has no passion, is it normal?
利用反射实现一个管理对象信息的简单框架
基于RT1052 Aworks nanopb string 类型固定长度使用方式(二十七)
focus on!Haitai Fangyuan joins the "Personal Information Protection Self-discipline Convention"
[Intensive reading of the paper] iNeRF
A high-quality WordPress download site template theme developed abroad
如何导入 Golang 外部包并使用它?
手写一个简单的web服务器(B/S架构)
JS basic exercises
HTC使用官方固件作为底包制作rom卡刷包教程