当前位置:网站首页>Processes and threads
Processes and threads
2022-07-26 00:43:00 【Wage earners~】
Catalog
- 1. Processes and threads
- 2. Parallelism and concurrency
- 3. application
- ∗ Asynchronous call of application ( Case study 1 ) \textcolor{Green}{* Asynchronous call of application ( Case study 1)} ∗ Asynchronous call of application ( Case study 1)
- ∗ Application to improve efficiency ( Case study 1 ) \textcolor{Green}{* Application to improve efficiency ( Case study 1) } ∗ Application to improve efficiency ( Case study 1)
1. Processes and threads
process
- A program consists of instructions and data , But these instructions have to run , Read and write data , You have to load the instructions into CPU, Data is loaded into memory . stay Disk is also needed during the operation of the instruction 、 Network and other equipment . Processes are used to load instructions 、 Manage memory 、 management IO Of .
- When a program is run , Load the program's code from disk to memory , At this point, a process begins .
- A process can be considered an instance of a program . Most programs can run multiple instance processes at the same time ( For example, Notepad 、 drawing 、 browser etc. ), Some programs can only start one instance process ( For example, Netease cloud music 、360 Security guard, etc )
Threads
A process can be divided into one or more threads .
A thread is a stream of instructions , Give instructions in a certain order to CPU perform
Java in , Thread as the minimum scheduling unit , Process is the smallest unit of resource allocation . stay windows The middle process is inactive , Just make Container for threads
distinguish between
Processes are basically independent of each other , Threads exist in the process , It's a subset of the process
Processes have shared resources , Such as memory space , For internal threads to share
Inter process communication is more complex
- The process communication of the same computer is called IPC(Inter-process communication)
- Process communication between different computers , Need to go through the Internet , And abide by a common agreement , for example HTTP
Thread communication is relatively simple , Because they share memory in the process , An example is that multiple threads can access the same shared variable
Threads are lighter , The cost of thread context switching is generally lower than that of process context switching
2. Parallelism and concurrency
Single core cpu Next , The thread is actually Serial execution Of . There is a component in the operating system called task scheduler , take cpu Time slice of (windows The next time slice is about 15 millisecond ) It's assigned to different programs , Just because cpu Between the lines ( The time slice is very short ) The switch is very fast , The human feeling is Running at the same time Of . In a word, it is : Micro serial , Macro parallel .
It's usually Threads take turns using CPU This is called concurrency , concurrent
| CPU | Time slice 1 | Time slice 2 | Time slice 3 | Time slice 4 |
|---|---|---|---|---|
| core | Threads 1 | Threads 2 | Threads 3 | Threads 4 |

Multicore cpu Next , Every nucleus (core) Can schedule running threads , At this time, the thread can be parallel .
| CPU | Time slice 1 | Time slice 2 | Time slice 3 | Time slice 4 |
|---|---|---|---|---|
| core1 | Threads 1 | Threads 2 | Threads 3 | Threads 4 |
| core2 | Threads 4 | Threads 4 | Threads 2 | Threads 2 |

quote Rob Pike A description of :
Concurrent (concurrent) At the same time (dealing with) Ability to do many things .
parallel (parallel) It's doing it at the same time (doing) Ability to do many things .
3. application
∗ Asynchronous call of application ( Case study 1 ) \textcolor{Green}{* Asynchronous call of application ( Case study 1)} ∗ Asynchronous call of application ( Case study 1)
We need to wait for the results
At this time, you can use synchronization , You can also use asynchrony to handle
join Realization ( Sync )
static int result = 0;
private static void test1() throws InterruptedException {
log.debug(" Start ");
Thread t1 = new Thread(() -> {
log.debug(" Start ");
sleep(1);
log.debug(" end ");
result = 10;
}, "t1");
t1.start();
t1.join();
log.debug(" The result is :{}", result);
}
Output
20:30:40.453 [main] c.TestJoin - Start
20:30:40.541 [Thread-0] c.TestJoin - Start
20:30:41.543 [Thread-0] c.TestJoin - end
20:30:41.551 [main] c.TestJoin - The result is :10
evaluation
- External shared variables are required , It does not conform to the idea of object-oriented encapsulation
- Must wait for the thread to end , Cannot be used with thread pool
Future Realization ( Sync )
private static void test2() throws InterruptedException, ExecutionException {
log.debug(" Start ");
FutureTask<Integer> result = new FutureTask<>(() -> {
log.debug(" Start ");
sleep(1);
log.debug(" end ");
return 10;
});
new Thread(result, "t1").start();
log.debug(" The result is :{}", result.get());
}
Output
10:11:57.880 c.TestSync [main] - Start
10:11:57.942 c.TestSync [t1] - Start
10:11:58.943 c.TestSync [t1] - end
10:11:58.943 c.TestSync [main] - The result is :10
evaluation
- Avoid using join Previous shortcomings
- It can be easily used with thread pool
private static void test3() throws InterruptedException, ExecutionException {
ExecutorService service = Executors.newFixedThreadPool(1);
log.debug(" Start ");
Future<Integer> result = service.submit(() -> {
log.debug(" Start ");
sleep(1);
log.debug(" end ");
return 10;
});
log.debug(" The result is :{}, result The type of :{}", result.get(), result.getClass());
service.shutdown();
}
Output
10:17:40.090 c.TestSync [main] - Start
10:17:40.150 c.TestSync [pool-1-thread-1] - Start
10:17:41.151 c.TestSync [pool-1-thread-1] - end
10:17:41.151 c.TestSync [main] - The result is :10, result The type of :class java.util.concurrent.FutureTask
evaluation
- Still main The thread receives the result
- get The method is to make the calling thread wait synchronously
Custom implementation ( Sync )
See mode : Protective pause mode
CompletableFuture Realization ( asynchronous )
private static void test4() {
// Thread pool for calculation
ExecutorService computeService = Executors.newFixedThreadPool(1);
// The thread pool that receives the results
ExecutorService resultService = Executors.newFixedThreadPool(1);
log.debug(" Start ");
CompletableFuture.supplyAsync(() -> {
log.debug(" Start ");
sleep(1);
log.debug(" end ");
return 10;
}, computeService).thenAcceptAsync((result) -> {
log.debug(" The result is :{}", result);
}, resultService);
}
Output
10:36:28.114 c.TestSync [main] - Start
10:36:28.164 c.TestSync [pool-1-thread-1] - Start
10:36:29.165 c.TestSync [pool-1-thread-1] - end
10:36:29.165 c.TestSync [pool-2-thread-1] - The result is :10
evaluation
- You can let the calling thread process the results asynchronously , In fact, other threads wait synchronously
- It is convenient to separate thread pools with different responsibilities
- Task Centered , Not thread centric
BlockingQueue Realization ( asynchronous )
private static void test6() {
ExecutorService consumer = Executors.newFixedThreadPool(1);
ExecutorService producer = Executors.newFixedThreadPool(1);
BlockingQueue<Integer> queue = new SynchronousQueue<>();
log.debug(" Start ");
producer.submit(() -> {
log.debug(" Start ");
sleep(1);
log.debug(" end ");
try {
queue.put(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
consumer.submit(() -> {
try {
Integer result = queue.take();
log.debug(" The result is :{}", result);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
No need to wait for the results
At this time, it is best to use asynchronous processing
Common thread implementation
@Slf4j(topic = "c.FileReader")
public class FileReader {
public static void read(String filename) {
int idx = filename.lastIndexOf(File.separator);
String shortName = filename.substring(idx + 1);
try (FileInputStream in = new FileInputStream(filename)) {
long start = System.currentTimeMillis();
log.debug("read [{}] start ...", shortName);
byte[] buf = new byte[1024];
int n = -1;
do {
n = in.read(buf);
} while (n != -1);
long end = System.currentTimeMillis();
log.debug("read [{}] end ... cost: {} ms", shortName, end - start);
} catch (IOException e) {
e.printStackTrace();
}
}
}
When threads are not used , Method calls are synchronized :
@Slf4j(topic = "c.Sync")
public class Sync {
public static void main(String[] args) {
String fullPath = "E:\\1.mp4";
FileReader.read(fullPath);
log.debug("do other things ...");
}
}
Output
18:39:15 [main] c.FileReader - read [1.mp4] start ...
18:39:19 [main] c.FileReader - read [1.mp4] end ... cost: 4090 ms
18:39:19 [main] c.Sync - do other things ...
After using thread , Method is called asynchronously :
private static void test1() {
new Thread(() -> FileReader.read(Constants.MP4_FULL_PATH)).start();
log.debug("do other things ...");
}
Output
18:41:53 [main] c.Async - do other things ...
18:41:53 [Thread-0] c.FileReader - read [1.mp4] start ...
18:41:57 [Thread-0] c.FileReader - read [1.mp4] end ... cost: 4197 ms
Thread pool implementation
private static void test2() {
ExecutorService service = Executors.newFixedThreadPool(1);
service.execute(() -> FileReader.read(Constants.MP4_FULL_PATH));
log.debug("do other things ...");
service.shutdown();
}
Output
11:03:31.245 c.TestAsyc [main] - do other things ...
11:03:31.245 c.FileReader [pool-1-thread-1] - read [1.mp4] start ...
11:03:33.479 c.FileReader [pool-1-thread-1] - read [1.mp4] end ... cost: 2235 ms
CompletableFuture Realization
private static void test3() throws IOException {
CompletableFuture.runAsync(() -> FileReader.read(Constants.MP4_FULL_PATH));
log.debug("do other things ...");
System.in.read();
}
Output
11:09:38.145 c.TestAsyc [main] - do other things ...
11:09:38.145 c.FileReader [ForkJoinPool.commonPool-worker-1] - read [1.mp4] start ...
11:09:40.514 c.FileReader [ForkJoinPool.commonPool-worker-1] - read [1.mp4] end ... cost: 2369 ms
From the caller's point of view ,
- If Need to wait for the result to return , To keep running is to synchronize
- There is no need to wait for the result to return , To be able to continue is asynchronous
1. Design
Multithreading can make method execution asynchronous ( That is, don't wait )、 For example, when reading disk files , Suppose that the read operation takes a long time 5 Second , If there is no thread scheduling mechanism , this 5 second cpu Nothing can be done , The rest of the code has to be suspended …
2. Conclusion
- For example, in a project , Video files need to be converted into formats and other operations are time-consuming , At this time, a new thread is opened to process video conversion , Avoid blocking the main thread
- tomcat The asynchronous servlet For a similar purpose , Let the user thread handle long-time operations , Avoid blocking tomcat Worker thread
- ui In the program , Open thread for other operations , Avoid blocking ui Threads
∗ Application to improve efficiency ( Case study 1 ) \textcolor{Green}{* Application to improve efficiency ( Case study 1) } ∗ Application to improve efficiency ( Case study 1)
Make full use of multicore cpu The advantages of , Improve operational efficiency . Imagine the following scenario , perform 3 A calculation , Finally, the calculation results are summarized .
Calculation 1 cost 10 ms
Calculation 2 cost 11 ms
Calculation 3 cost 9 ms
Summary needs 1 ms
If it's a serial execution , So the total time spent is 10 + 11 + 9 + 1 = 31ms
But if it's a quad core cpu, Each core uses threads 1 Perform calculations 1, Threads 2 Perform calculations 2, Threads 3 Perform calculations 3, that 3 individual Threads are parallel , The time spent depends only on the running time of the longest thread , namely 11ms Finally, adding the summary time will only take 12ms
Be careful : Need to be in multi-core cpu To improve efficiency , Single core is still executed in turn
1. Design
Make full use of multithreading CPU
Environment building
Benchmarking tool selection , Used a more reliable JMH, It will execute the program , Perform multiple tests and average
cpu Audit limit , There are two ways of thinking
- Using virtual machines , Assign the appropriate core
- Use msconfifig, Assign the appropriate core , It's troublesome to restart
Choice of parallel computing methods
- At first, I wanted to use parallel stream, Later, it was found that it had its own problems
- Change to manual control thread, Implement simple parallel computing
The test code is as follows
mvn archetype:generate -DinteractiveMode=false -DarchetypeGroupId=org.openjdk.jmh -DarchetypeArtifactId=jmh-java-benchmark-archetype -DgroupId=org.sample -DartifactId=test -Dversion=1.0package org.sample; import java.util.Arrays; import java.util.concurrent.FutureTask; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Fork; import org.openjdk.jmh.annotations.Measurement; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.Warmup; @Fork(1) @BenchmarkMode(Mode.AverageTime) @Warmup(iterations = 3) @Measurement(iterations = 5) public class MyBenchmark { static int[] ARRAY = new int[1000_000_00]; static { Arrays.fill(ARRAY, 1); } @Benchmark public int c() throws Exception { int[] array = ARRAY; FutureTask<Integer> t1 = new FutureTask<>(() -> { int sum = 0; for (int i = 0; i < 250_000_00; i++) { sum += array[0 + i]; } return sum; }); FutureTask<Integer> t2 = new FutureTask<>(() -> { int sum = 0; for (int i = 0; i < 250_000_00; i++) { sum += array[250_000_00 + i]; } return sum; }); FutureTask<Integer> t3 = new FutureTask<>(() -> { int sum = 0; for (int i = 0; i < 250_000_00; i++) { sum += array[500_000_00 + i]; } return sum; }); FutureTask<Integer> t4 = new FutureTask<>(() -> { int sum = 0; for (int i = 0; i < 250_000_00; i++) { sum += array[750_000_00 + i]; } return sum; }); new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).start(); new Thread(t4).start(); return t1.get() + t2.get() + t3.get() + t4.get(); } @Benchmark public int d() throws Exception { int[] array = ARRAY; FutureTask<Integer> t1 = new FutureTask<>(() -> { int sum = 0; for (int i = 0; i < 1000_000_00; i++) { sum += array[0 + i]; } return sum; }); new Thread(t1).start(); return t1.get(); } }
Dual core CPU(4 A logic CPU)
C:\Users\lenovo\eclipse-workspace\test>java -jar target/benchmarks.jar
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: org.sample.MyBenchmark.c
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration 1: 0.022 s/op
# Warmup Iteration 2: 0.019 s/op
# Warmup Iteration 3: 0.020 s/op
Iteration 1: 0.020 s/op
Iteration 2: 0.020 s/op
Iteration 3: 0.020 s/op
Iteration 4: 0.020 s/op
Iteration 5: 0.020 s/op
Result: 0.020 ±(99.9%) 0.001 s/op [Average]
Statistics: (min, avg, max) = (0.020, 0.020, 0.020), stdev = 0.000
Confidence interval (99.9%): [0.019, 0.021]
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: org.sample.MyBenchmark.d
# Run progress: 50.00% complete, ETA 00:00:10
# Fork: 1 of 1
# Warmup Iteration 1: 0.042 s/op
# Warmup Iteration 2: 0.042 s/op
# Warmup Iteration 3: 0.041 s/op
Iteration 1: 0.043 s/op
Iteration 2: 0.042 s/op
Iteration 3: 0.042 s/op
Iteration 4: 0.044 s/op
Iteration 5: 0.042 s/op
Result: 0.043 ±(99.9%) 0.003 s/op [Average]
Statistics: (min, avg, max) = (0.042, 0.043, 0.044), stdev = 0.001
Confidence interval (99.9%): [0.040, 0.045]
# Run complete. Total time: 00:00:20
Benchmark Mode Samples Score Score error Units
o.s.MyBenchmark.c avgt 5 0.020 0.001 s/op
o.s.MyBenchmark.d avgt 5 0.043 0.003 s/op
- You can see under multi-core , The efficiency improvement is still obvious , About twice as fast
Single core CPU
C:\Users\lenovo\eclipse-workspace\test>java -jar target/benchmarks.jar
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: org.sample.MyBenchmark.c
# Run progress: 0.00% complete, ETA 00:00:16
# Fork: 1 of 1
# Warmup Iteration 1: 0.064 s/op
# Warmup Iteration 2: 0.052 s/op
# Warmup Iteration 3: 1.127 s/op
Iteration 1: 0.053 s/op
Iteration 2: 0.052 s/op
Iteration 3: 0.053 s/op
Iteration 4: 0.057 s/op
Iteration 5: 0.088 s/op
Result: 0.061 ±(99.9%) 0.060 s/op [Average]
Statistics: (min, avg, max) = (0.052, 0.061, 0.088), stdev = 0.016
Confidence interval (99.9%): [0.001, 0.121]
# VM invoker: C:\Program Files\Java\jdk-11\bin\java.exe
# VM options: <none>
# Warmup: 3 iterations, 1 s each
# Measurement: 5 iterations, 1 s each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: org.sample.MyBenchmark.d
# Run progress: 50.00% complete, ETA 00:00:11
# Fork: 1 of 1
# Warmup Iteration 1: 0.054 s/op
# Warmup Iteration 2: 0.053 s/op
# Warmup Iteration 3: 0.051 s/op
Iteration 1: 0.096 s/op
Iteration 2: 0.054 s/op
Iteration 3: 0.065 s/op
Iteration 4: 0.050 s/op
Iteration 5: 0.055 s/op
Result: 0.064 ±(99.9%) 0.071 s/op [Average]
Statistics: (min, avg, max) = (0.050, 0.064, 0.096), stdev = 0.018
Confidence interval (99.9%): [-0.007, 0.135]
# Run complete. Total time: 00:00:22
Benchmark Mode Samples Score Score error Units
o.s.MyBenchmark.c avgt 5 0.061 0.060 s/op
o.s.MyBenchmark.d avgt 5 0.064 0.071 s/op
- The performance is almost the same
2. Conclusion
- Single core cpu Next , Multithreading can't actually improve the running efficiency of programs , Just to be able to switch between different tasks , Different threads take turns to use cpu , Not a thread is always occupied cpu, Other threads can't work
- Multicore cpu Can run multiple threads in parallel , But whether we can improve the running efficiency of the program depends on the situation
- Some tasks , Well designed , Split the task , Parallel execution , Of course, it can improve the efficiency of the program . But not all calculations Everything can be split ( Refer to the following 【 Amdal's law 】)
- Not all tasks need to be split , If the purpose of the task is different , There's no point in talking about separation and efficiency
- IO Operation does not occupy cpu, It's just that we usually copy files using 【 Blocking IO】, At this time, it's equivalent to thread, though not cpu, But it takes one Wait for IO end , Not taking full advantage of threads . That's why there's the back 【 Non blocking IO】 and 【 asynchronous IO】 Optimize .
边栏推荐
- [oops framework] network module websocket
- openvino安装踩坑笔记
- Analysis and practice of parameter parser handlermethodargumentresolver
- YOLOV2 YOLO9000
- sql语句练习
- 独家下载|《阿里云MaxCompute百问百答》 解锁SaaS模式云数据仓库尽在本电子手册!
- Comparing the seven distributed transaction schemes, I prefer Alibaba's open source Seata (principle + Practice)
- Nodejs learning resources
- ASP.NET Core配置
- What is the difference between request forwarding and request redirection?
猜你喜欢
![[zero based BLDC series] brushless DC motor control principle based on the zero crossing detection method of back EMF](/img/1d/00165635452245a4d1b557fba17c94.png)
[zero based BLDC series] brushless DC motor control principle based on the zero crossing detection method of back EMF

从另一个角度告诉你单元测试的意义

Redis(八) - Redis企业实战之优惠券秒杀

Oauth2 and JWT

实战演练 | 查找在给定时间范围内购买超过 N 件商品的客户

C # from entry to mastery (III)

Mwec: a new Chinese word discovery method based on multi semantic word vector
![[plaything determination scratch children programming] ride a small motorcycle (dynamic background + camera control operation)](/img/35/5fe27d3f61b41bf4b9434cd4d7d1b1.png)
[plaything determination scratch children programming] ride a small motorcycle (dynamic background + camera control operation)

JVM Tri Color marking and read-write barrier

Tell you the meaning of unit testing from another angle
随机推荐
Azure synapse analytics Performance Optimization Guide (1) -- optimize performance using ordered aggregate column storage indexes
[redis] ③ data elimination strategy, pipeline command, publish and subscribe
2022/7/24 考试总结
前缀异或和,异或差分数组
Binary representation -- power of 2
向左旋转k个字符串(细节)
Leetcode 笔记 20. 有效的括号
Hcip day 12
参数解析器HandlerMethodArgumentResolver分析与实战
After seven years of testing, the interview with Huawei finally negotiated a salary of 10000. HR said that I didn't respect Huawei and they didn't have such a low salary position~
实战演练 | 查找在给定时间范围内购买超过 N 件商品的客户
HCIP第十二天
测试左移和测试右移的概念
Find the single dog (Li Kou 260)
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
攻防世界web题-favorit_number
openvino安装踩坑笔记
Distributed transaction and at mode principle of Seata
Redis Command Reference Manual - key
Hcip day 11