-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadsDemo.java
More file actions
155 lines (137 loc) · 5.72 KB
/
ThreadsDemo.java
File metadata and controls
155 lines (137 loc) · 5.72 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package java;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
//import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
// import java.util.ArrayList;
// import java.util.List;
public class ThreadsDemo {
public static void main(String[] args) {
/* --------------------------------synchronized & concurrent collections----------------------- */
Collection <Integer> collection = Collections.synchronizedCollection(new ArrayList<>());
Thread thread1 = new Thread(() -> {
collection.addAll(Arrays.asList(1, 2, 3));
});
Thread thread2 = new Thread(() -> {
collection.addAll(Arrays.asList(4, 5, 6));
});
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(collection);
Map <Integer, String> map = new ConcurrentHashMap<>();
map.put(1, "rahul");
/*------------------------------------Atomic Objects & Adders-------------------------------------
var status = new DownloadStatus();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
var thread = new Thread(new DownloadFileTask(status));
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(status.getTotalBytes()); */
/*---------------------------volatile approach-------------------------------------------
var status = new DownloadStatus();
var thread1 = new Thread(new DownloadFileTask(status));
var thread2 = new Thread(() -> {
while(!status.isDone()){
synchronized(status){
try {
status.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println(status.getTotalBytes());
}); //defining run method as anonymus inner class.
thread1.start();
thread2.start(); */
/*----------------------------------Locks & synchronized ----------------------------------
List<Thread> threads = new ArrayList<>();
var status = new DownloadStatus();
for (int i = 0; i < 10; i++) {
var thread = new Thread(new DownloadFileTask(status));
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Total bytes downloaded : " + status.getTotalBytes()); */
/*------------------------Confinement ------------------------------------------
List<Thread> threads = new ArrayList<>();
List<DownloadFileTask> tasks = new ArrayList<>();
for (int i = 0; i < 10; i++) {
var task = new DownloadFileTask();
tasks.add(task);
Thread thread = new Thread(task);
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
var totalBytesDownloaded = tasks.stream()
.map(t -> t.getStatus().getTotalBytes())
.reduce(Integer :: sum);
System.out.println("Total Bytes after confinement : " + totalBytesDownloaded.orElse(0));*/
/* ------------------------Race Condition--------------------------------------------
DownloadStatus status = new DownloadStatus();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new DownloadFileTask(status));
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Total bytes downloaded : " + status.getTotalBytes()); */
/* System.out.println(Thread.activeCount());
System.out.println(Runtime.getRuntime().availableProcessors());
System.out.println(Thread.currentThread().getName()); */
/* ------------------------Interrupted code----------------------------------------
Thread thread = new Thread(new DownloadFileTask());
thread.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt(); // just sends a request - is thread interrupted. doesn't force thread to stop.
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("File is ready to scan."); */
}
}