当前位置:网站首页>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<>();
}

边栏推荐
猜你喜欢

Charles抓包工具学习记录

Using pipreqs export requirements needed for the project. TXT (rather than the whole environment)

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现

MySQL1

Path Prefixes (倍增!树上の二分)

AI mid-stage sequence labeling task: three data set construction process records

【论文笔记】基于动作空间划分的MAXQ自动分层方法

10分钟带你入门chrome(谷歌)浏览器插件开发

pytorch one-hot 小技巧

Docker starts mysql
随机推荐
mysql服务器上的mysql这个实例中表的介绍
WordPress主题-B2美化通用子主题商业运营版
ArcEngine (2) loading the map document
“==”和equals的区别
ArcEngine(一)加载矢量数据
WPF 学习笔记《WPF样式基础》
Laya中关于摄像机跟随人物移动或者点击人物碰撞器触发事件的Demo
001-进程与线程
进程信息
ArcEngine(六)用tool工具实现拉框放大缩小和平移
dflow入门3——dpdispatcher插件
AcWing 3391. 今年的第几天?(简单题)
面渣逆袭:MySQL六十六问,两万字+五十图详解
多线程下的单例模式
LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之二:编码实现
AI mid-stage sequence labeling task: three data set construction process records
day12---接口和协议
【TPC-DS】DF的SQL(Data Maintenance部分)
行业洞察 | 如何更好的实现与虚拟人的互动体验?
LeetCode 每日一题——622. 设计循环队列