当前位置:网站首页>2022/08/02 Study Notes (day22) Multithreading
2022/08/02 Study Notes (day22) Multithreading
2022-08-03 04:31:00 【aggressive cucumber】
目录
4.CPUMulticore cache structure:
多线程:
1.创建线程:
(1)继承Thread类:
Thread类中的run方法不是抽象方法,ThreadA class is also not an abstract class
MyThread当继承了Thread类之后,It's just an independent thread.
To let the thread start.调用线程的start方法.
class MyThread extends Thread {
@Override
public void run() {
System.out.println(2);
}
}
public class Ch01 {
public static void main(String[] args) {
System.out.println(1);
//
MyThread myThread = new MyThread();
// 当调用start方法启动一个线程时,会执行重写的run方法的代码
// 调用的是start,执行的是run,为什么不直接调run
myThread.start();
// Ordinary object call method
// myThread.run();
// 线程的优先级,概率问题!Can't do 100%
// 90Will run the main method first 10先跑mythread
System.out.println(3);
System.out.println(4);
}
}
(2)Runnable接口(推荐使用):
class MyThread2 implements Runnable {
@Override
public void run() {
System.out.println(2);
}
}
public class Ch02 {
public static void main(String[] args) {
System.out.println(1);
// start
MyThread2 myThread2 = new MyThread2();
// If you want the thread to start,必须调用Thread类中的start方法
// 问题:实现了Runnable接口,找不到start方法了?
Thread t = new Thread(myThread2);
t.start();
System.out.println(3);
System.out.println(4);
}
}
2.Thread和Runnable的区别:
如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享.
总结:
实现Runnable接口比继承Thread类所具有的优势:
1):适合多个相同的程序代码的线程去处理同一个资源
2):可以避免java中的单继承的限制
3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立
4):线程池只能放入实现Runable或callable类线程,不能直接放入继承Thread的类
3.线程的生命周期:
RUNNABLE: | 线程正在JVM中被执行,Waiting for scheduling from the operating system |
NEW: | This state is mainly the thread is notstart()调用执行 |
BLOCKED: | 阻塞.For some reason, it cannot be executed immediately and needs to suspend and wait |
WAITING: | 无限期等待.Object类.如果没有唤醒,则一直等待 |
TIMED_WAITING: | 有限期等待,The thread waits for a specified amount of time |
TERMINATED: | 终止线程的状态,线程已经执行完毕 |
The concepts of waiting and blocking are somewhat similar,Blocked for external reasons,需要等待,
The wait is generally active call method,Initiate an active wait.Waiting can also pass in parameters to determine the waiting time.
4.CPUMulticore cache structure:
物理内存:硬盘内存.(固态硬盘,Try not to choose a hybrid hard drive) CPUCache in order to improve the performance of the program running,现在CPUPrograms are optimized in many ways. CPU处理速度最快,内存次之,Hard disk speed is the lowest. 在CPU处理内存数据时,If the memory runs too slow,就会拖累CPU的速度 为了解决这样的问题,CPUA multi-level caching strategy is designed. CPU分为三级缓存:每个CPU都有L1,L2缓存,但是L3The cache is common to multiple cores. CPU查找数据时,CPU->l1->l2->l3->内存->硬盘 从CPU到内存,60-80纳秒 从CPU到L3,15纳秒 从CPU到L2,3纳秒 从CPU到L1,1纳秒 寄存器,0.3纳秒 进一步优化,CPU每次读取一个数据,It is adjacent to it when read64个字节的数据. 【缓存行】. Intel came up with a dealMESI协议 1、修改态,This cache is passed over,The contents are different from those in main memory,Exclusive to this cache 2、专有态,This cache is consistent with main memory,但是其他CPU中没有 3、共享态,This cache is consistent with main memory,There are other caches too 4、无效态,This cache is invalid,需要从主内存中重新读取 java内存模型-JMM Try to achieve consistent access between the hardware and the operating system.
线程争抢: The best way to solve the problem of thread contention is【加锁】 synchronized同步锁,线程同步 When a method is addedsynchronized修饰,This method is called the synchronization method.
5.线程安全的实现方法:
(1)数据不可变. All immutable objects must be thread-safe. The caller of the object's method's implementation method,No need for any thread-safety safeguards. 比如final关键字修饰的基本数据类型,字符串. 只要一个不可变的对象被正确的创建出来,那外部的可见状态永远都不会改变.
(2)互斥同步.加锁.【悲观锁】
(3)非阻塞同步.【无锁编程】,自旋.我们会用cas来实现这种非阻塞同步.
(4)无同步方案.Multiple threads need to share data,But again these data can be calculated in a separate thread,得出结果 We can limit the visibility of shared data to a single thread,This eliminates the need for synchronization.Bring the shared data, 我用我的,你用你的,从而保证线程安全.
public class Ch02 {
private static int x = 0,y = 0;
private static int a = 0,b = 0;
private static int count = 0;
private volatile static int NUM = 1;
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
for (;;) {
Thread t1 = new Thread(() -> {
a = 1;
x = b;
});
Thread t2 = new Thread(() -> {
b = 1;
y = a;
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("一共执行了:" + count++ + "次");
if(x == 0 && y ==0){
long end = System.currentTimeMillis();
System.out.println("耗时:" +(end - start) + "毫秒,(" + x + "," + y + ")");
break;
}
a = 0;b = 0;x = 0;y = 0;
}
}
We found that most of the test results felt correct,(0,1)或(1,0),一个是线程1先执行,一个是线程2先执行. 按道理来说,绝对不会出现(0,0),如果出现(0,0)Indicates that there is instruction rearrangement,乱序执行. 使用volatilekeyword to ensure that a variable is read and written during a read or write operation,避免指令重排. We add an instruction before the read and write operations,当CPUAfter encountering this instruction, you must wait until the previous execution is completed before continuing to execute the next instruction.
线程状态转换:
边栏推荐
猜你喜欢
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
在竞争白热化的电商行业,链动2+1为什么还有企业在用
MySQL 入门:Case 语句很好用
那些让电子工程师崩溃瞬间,你经历了几个呢?
C# WPF设备监控软件(经典)-上篇
StarRocks 7 月社区动态
测开:项目管理模块-项目curd开发
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
修饰生物素DIAZO-生物素-PEG3-DBCO|重氮-生物素-三聚乙二醇-二苯基环辛炔
Dialog manager in the fourth chapter: the dialog message loop
随机推荐
Redis缓存雪崩、缓存穿透、缓存击穿
肖sir___面试就业课程____性能测试
t conditional judgment statement and if loop
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
肖sir__自动化面试题
6.神经网络剖析
How many moments have you experienced the collapse of electronic engineers?
正则表达式与绕过案例
【生物素叠氮化物|cas:908007-17-0】价格_厂家
Mysql如何建立索引实现语句优化
mysql 创建索引的三种方式
【Harmony OS】【ARK UI】Date 基本操作
多肽介导PEG磷脂——靶向功能材料之DSPE-PEG-RGD/TAT/NGR/APRPG
v-text指令:设置标签内容
CobalStrike(CS)基础超级详细版
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
在线密码生成工具推荐
unity2D横板游戏教程6-敌人AI以及受击动画
自组织是管理者和成员的双向奔赴
1.一个神经网络示例