当前位置:网站首页>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导致无法启动服务器
![[Kaggle combat] Prediction of the number of survivors of the Titanic (from zero to submission to Kaggle to model saving and restoration)](/img/2b/d2f565d9221da094a9ccc30f506dc8.png)
[Kaggle combat] Prediction of the number of survivors of the Titanic (from zero to submission to Kaggle to model saving and restoration)

Nacos使用实践

Redisson实现分布式锁

进程的创建

AI中台序列标注任务:三个数据集构造过程记录

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
redis stream 实现消息队列

Dapr 与 NestJs ,实战编写一个 Pub & Sub 装饰器

“==”和equals的区别
随机推荐
数仓4.0(二)------ 业务数据采集平台
sqlite 日期字段加一天
HCIA实验(07)
qt使用mysql数据库(自学笔记)
内存模型之有序性
HCIP实验(06)
dflow入门4——recurse&reuse&conditional
【收获合辑】k-NN与检索任务的异同+jupyter转pdf
Charles抓包工具学习记录
进程信息
dflow入门1——HelloWorld!
Docker starts mysql
HCIP练习02(OSPF)
行业 SaaS 微服务稳定性保障实战
mysql 8.0.12 安装配置方法并--设置修改密码
数据监控平台
AI mid-stage sequence labeling task: three data set construction process records
ArcEngine(四)MapControl_OnMouseDown的使用
vim 折叠函数
WPF 学习笔记《WPF样式基础》