-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreading.java
More file actions
133 lines (120 loc) · 3.4 KB
/
Copy pathMultiThreading.java
File metadata and controls
133 lines (120 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
public class MultiThreading {
static int jumlah = 0;
public static synchronized void tambahJumlah() {
jumlah++;
}
public static void main(String[] args) throws InterruptedException {
// Thread pekerja = new Thread(() -> {
// System.out.println("thread pekerja lagi jalan");
// });
//
// pekerja.start();
// pekerja.run();
//
// System.out.println("thread dari utamanya tetap juga jalan");
// Runnable tugasPertama = () -> {
// for (int nomor = 1; nomor <= 5; nomor++) {
// System.out.println("[tugas pertama] memproses data dengan nomor: " + nomor);
// }
// };
//
// Runnable tugasKedua = () -> {
// for (int nomor = 1; nomor <= 5; nomor++) {
// System.out.println("[tugas kedua] memproses data dengan nomor: " + nomor);
// }
// };
//
// Thread pekerjaPertama = new Thread(tugasPertama);
// Thread pekerjaKedua = new Thread(tugasKedua);
//
// pekerjaPertama.start();
// pekerjaKedua.start();
// concurrency
// parallelism
// core 1 -> thread 1
// core 2 -> thread 2
// Thread downloadGambar = new Thread(() -> {
// System.out.println("mulai download gambar");
//
// try {
// Thread.sleep(3000);
// } catch (InterruptedException error) {
// System.out.println("download gambar gagal, error: " + error);
// }
//
// System.out.println("download gambar kelar");
// });
//
// Thread ambilDataGambar = new Thread(() -> {
// System.out.println("mulai ambil data gambar");
//
// try {
// Thread.sleep(2000);
// } catch (InterruptedException error) {
// System.out.println("fetching data gambar gagal, error: " + error);
// }
//
// System.out.println("fetching data gambar kelar");
// });
//
// Thread ambilDataLog = new Thread(() -> {
// System.out.println("mulai ambil data log");
//
// try {
// Thread.sleep(1000);
// } catch (InterruptedException error) {
// System.out.println("ambil data logging error, error: " + error);
// }
//
// System.out.println("ambil data logging aman");
// });
//
// downloadGambar.start();
// ambilDataGambar.start();
// ambilDataLog.start();
//
//
// Thread pekerja = new Thread(() -> {
// System.out.println("memproses data faktual excel");
// });
//
// pekerja.start();
//
// Thread pekerja = new Thread(() -> {
// System.out.println("memproses data faktual excel");
//
// try {
// Thread.sleep(2000);
// } catch (InterruptedException error) {
// Thread.currentThread().interrupt();
// }
//
// System.out.println("proses data faktual excel sudah siap");
// });
//
// pekerja.start();
// pekerja.join();
//
// System.out.println("program kelar");
Thread pekerjaPertama = new Thread(() -> {
for (int nomor = 0; nomor < 1_000_000; nomor++) {
tambahJumlah();
}
});
Thread pekerjaKedua = new Thread(() -> {
for (int nomor = 0; nomor < 1_000_000; nomor++) {
tambahJumlah();
}
});
pekerjaPertama.start();
pekerjaKedua.start();
pekerjaPertama.join();
pekerjaKedua.join();
System.out.println("jumlah hasil adalah: " + jumlah);
}
// jumlah = 10;
// thread pertama = baca 10
// thread kedua = baca 10
// thread pertama = 10 + 1 = 11
// thread kedua = 10 + 1 = 11
}