当前位置:网站首页>Scala parallel collections, parallel concurrency, thread safety issues, ThreadLocal
Scala parallel collections, parallel concurrency, thread safety issues, ThreadLocal
2022-08-03 08:32:00 【But do good things, don't ask about the future】
1. scala并行集合
ScalaProvide parallel collection,用于多核环境的并行计算,充分使用多核CPU
object T34 {
def main(args: Array[String]): Unit = {
val result = (0 to 20).par.map {
x => Thread.currentThread.getName }
println(result)
}
}
并行
:Two or more events occur at the same time
(多核CPU)并发
:两个或多个事件在同一时间段内发生
(Multithreaded competition resources)
2. 线程安全问题
Multi-threaded concurrent modification,To modify a Shared object attribute in the Shared memory
所导致的数据冲突问题
public class T45 {
public static void main(String[] args) {
User user = new User();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
user.name = "zs";
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(user.name);
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
user.name = "lisi";
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(user.name);
}
});
thread1.start();
thread2.start();
System.out.println("main");
}
}
class User {
public String name;
}
3. ThreadLocal
ThreadLocal中填充的变量属于当前线程,For other threads is to isolate the.ThreadLocal为变量在每个线程中都创建了一个副本
,Copies of each thread access to their internal variables.因此不存在线程安全问题
public class T45 {
public static void main(String[] args) {
User user = new User();
//线程安全问题: Multi-threaded concurrent modification,Shared in the Shared memory object properties caused by modified data conflict
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
user.name.set("zs");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(user.name.get());
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
user.name.set("lisi");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(user.name.get());
}
});
thread1.start();
thread2.start();
System.out.println("main");
}
}
class User {
public ThreadLocal<String> name=new ThreadLocal<>();
}
边栏推荐
- mysql5.7服务器The innodb_system data file 'ibdata1' must be writable导致无法启动服务器
- wordpress: 裁剪您的图片时发生错误
- 如何在安装GBase 8c数据库的时候,报错显示“Host ips belong to different cluster?
- 关于Unity自定义Inspector面板的一些自定义编辑器扩展
- greenplum role /user 管理
- Guava的Service
- IDEA的database使用教程(使用mysql数据库)
- 开发工具之版本控制
- Mysql的in和exists用法区别
- Using pipreqs export requirements needed for the project. TXT (rather than the whole environment)
猜你喜欢
随机推荐
HCIA实验(07)
用diskpart的offline命令弹出顽固硬盘
实时目标检测新高地之#YOLOv7#更快更强的目标检测器
线程介绍与使用
Nacos使用实践
NFT到底有哪些实际用途?
牛客 - 鼠标的天选(字符串哈希)
流行和声基础大笔记
第十二天&接口和协议
并发之固定运行和交替运行方案
PowerShell:执行 Install-Module 时,不能从 URI 下载
HCIP练习(OSPF)
HCIP练习03(重发布)
LINGO 18.0软件安装包下载及安装教程
Transformer、BERT、GPT 论文精读笔记
内存模型之可见性
ArcEngine(六)用tool工具实现拉框放大缩小和平移
计算机网络之网络安全
Add Modulo 10 (规律循环节,代码实现细节)
LeetCode 每日一题——622. 设计循环队列