8000 add multithreading example in Java · fishercoder1534/RandomJava@0a5b06a · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a5b06a

Browse files
add multithreading example in Java
1 parent 5a7cc87 commit 0a5b06a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package multithread.singlevsmultiplethreads;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.Future;
9+
10+
/**
11+
* This folder has two classes which is a good illustration to show the power of multithreading:
12+
* it dramatically improves throughput and speeds up workload!
13+
*/
14+
public class MultiThreadedApp {
15+
private static final int THREAD_POOL_SIZE = 5;
16+
private static final int TOTAL_TASKS = 10;
17+
private static final int MILLISECONDS_TO_FINISH_A_TASK = 1000;
18+
19+
public static void main(String[] args) throws ExecutionException, InterruptedException {
20+
21+
long start = System.currentTimeMillis();
22+
ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
23+
List<Future> futures = new ArrayList<>();
24+
for (int i = 0; i < TOTAL_TASKS; i++) {
25+
Runnable worker = new Worker("Worker" + i);
26+
Future<?> future = executorService.submit(worker);
27+
futures.add(future);
28+
}
29+
executorService.shutdown();
30+
while (!executorService.isTerminated()) {
31+
}
32+
long end = System.currentTimeMillis();
33+
System.out.println("Multi-threaded app finished, it took " + (end - start) / 1000 +
34+
" seconds for a thread pool of size " + THREAD_POOL_SIZE + " to finish " +
35+
TOTAL_TASKS + " tasks, with each task takes " + MILLISECONDS_TO_FINISH_A_TASK / 1000 + " seconds.");
36+
executorService.shutdown();
37+
}
38+
39+
static class Worker implements Runnable {
40+
private String workerName;
41+
42+
public Worker(String workerName) {
43+
this.workerName = workerName;
44+
}
45+
46+
@Override
47+
public void run() {
48+
System.out.println(Thread.currentThread().getName() + " starting worker: " + workerName);
49+
doWork();
50+
System.out.println(Thread.currentThread().getName() + " ended for worker: " + workerName);
51+
}
52+
53+
private void doWork() {
54+
try {
55+
Thread.sleep(MILLISECONDS_TO_FINISH_A_TASK);
56+
} catch (InterruptedException e) {
57+
throw new RuntimeException(e);
58+
}
59+
}
60+
}
61+
62+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package multithread.singlevsmultiplethreads;
2+
3+
public class SingleThreadedApp {
4+
5+
private static final int TOTAL_TASKS = 10;
6+
private static final int MILLISECONDS_TO_FINISH_A_TASK = 1000;
7+
8+
public static void main(String[] args) throws InterruptedException {
9+
long start = System.currentTimeMillis();
10+
work(TOTAL_TASKS);
11+
long end = System.currentTimeMillis();
12+
System.out.println("Single-threaded app took " + (end - start) / 1000 +
13+
" seconds to finish a total of " + TOTAL_TASKS +
14+
" tasks, with each task takes " + MILLISECONDS_TO_FINISH_A_TASK / 1000 + " seconds.");
15+
}
16+
17+
private static void work(int n) {
18+
for (int i = 0; i < n; i++) {
19+
doWork(i);
20+
}
21+
}
22+
23+
private static void doWork(int workNumber) {
24+
System.out.println("Task " + workNumber + " is being worked on.");
25+
try {
26+
Thread.sleep(MILLISECONDS_TO_FINISH_A_TASK);
27+
} catch (InterruptedException e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)
0