-
Notifications
You must be signed in to change notification settings - Fork 0
/
Office_4.java
309 lines (239 loc) · 9.63 KB
/
Office_4.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import java.util.concurrent.*;
import java.util.*;
public class Office_4 {
public void ques1_trial1(){
class CustomThreadPool {
private int numThreads;
private WorkerThread[] threads;
private BlockingQueue<Runnable> taskQueue;
public CustomThreadPool(int numThreads) {
this.numThreads = numThreads;
this.threads = new WorkerThread[numThreads];
this.taskQueue = new LinkedBlockingQueue<>();
for (int i = 0; i < numThreads; i++) {
threads[i] = new WorkerThread();
threads[i].start();
}
}
public void submit(Runnable task) {
try {
taskQueue.put(task);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class WorkerThread extends Thread {
@Override
public void run() {
while(true){
try {
Runnable task = taskQueue.take();
task.run();
} catch (InterruptedException e) {
//e.printStackTrace();
}
}
}
}
public void shutdown() {
for (int i = 0; i < numThreads; i++) {
threads[i].interrupt();
}
}
}
// Driver code
Scanner input = new Scanner(System.in);
System.out.print("Enter number of threads: ");
int threads= input.nextInt();
System.out.print("Enter number of tasks: ");
int tasks= input.nextInt();
CustomThreadPool threadPool = new CustomThreadPool(threads);
// Submit tasks to the thread pool
for (int i = 1; i <= tasks; i++) {
int taskId = i;
threadPool.submit(() -> {
System.out.println("Task " + taskId + " executed by thread " + Thread.currentThread().getName());
});
}
// Shutdown the thread pool after submitting all tasks
threadPool.shutdown();
}
public void ques1_trial2(){
class CustomThreadPool {
private int numThreads;
private ExecutorService executor;
private BlockingQueue<Runnable> taskQueue;
public CustomThreadPool(int numThreads) {
this.numThreads = numThreads;
this.executor = Executors.newFixedThreadPool(numThreads);
this.taskQueue = new LinkedBlockingQueue<>();
for (int i = 0; i < numThreads; i++) {
executor.execute(new WorkerThread());
}
}
public void submit(Runnable task) {
try {
taskQueue.put(task);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class WorkerThread implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Runnable task = taskQueue.take();
task.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
public void shutdown() {
executor.shutdown();
}
}
// Driver code
CustomThreadPool threadPool = new CustomThreadPool(5);
// Submit tasks to the thread pool
for (int i = 1; i <= 10; i++) {
int taskId = i;
threadPool.submit(() -> {
System.out.println("Task " + taskId + " executed by thread " + Thread.currentThread().getName());
});
}
// Shutdown the thread pool after submitting all tasks
threadPool.shutdown();
}
public void ques1_trial3(){
class CustomThreadPool {
private int numThreads;
private WorkerThread[] threads;
private BlockingQueue<Runnable> taskQueue;
// Constructor
public CustomThreadPool(int numThreads) {
this.numThreads = numThreads;
this.threads = new WorkerThread[numThreads];
this.taskQueue = new LinkedBlockingQueue<>();
for (int i = 0; i < numThreads; i++) {
threads[i] = new WorkerThread();
threads[i].start();
}
}
// Custom thread implementation
private class WorkerThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
try {
Runnable task = taskQueue.take();
task.run();
} catch (InterruptedException e) {
// Thread interrupted, exit the loop and terminate
break;
}
}
}
}
public void submit(Runnable task) {
try {
taskQueue.put(task);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void shutdown() {
for (int i = 0; i < numThreads; i++) {
threads[i].interrupt();
}
}
}
class FibonacciTask implements Runnable {
private int n;
private int[] memo;
public FibonacciTask(int lim) {
this.n = lim;
this.memo = new int[n + 1];
}
@Override
public void run() {
int result = calculateFibonacci(n);
System.out.println("Fibonacci(" + n + ") = " + result + " (Executed by thread " + Thread.currentThread().getName() + ")");
}
private int calculateFibonacci(int n) {
if (n <= 1){return n;}
if (memo[n] == 0) {
memo[n] = calculateFibonacci(n - 1) + calculateFibonacci(n - 2);
}
return memo[n];
}
}
class FactorialTask implements Runnable {
private int n;
public FactorialTask(int val) {
this.n = val;
}
@Override
public void run() {
long result = calculateFactorial(n);
System.out.println("Factorial(" + n + ") = " + result + " (Executed by thread " + Thread.currentThread().getName() + ")");
}
private long calculateFactorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * calculateFactorial(n - 1);
}
}
class PermutationTask implements Runnable {
private String str;
private HashSet<String> permutations;
public PermutationTask(String str) {
this.str = str;
this.permutations = new HashSet<>();
}
@Override
public void run() {
generatePermutations("", str);
System.out.println("Permutations of " + str + ": " + permutations + " (Executed by thread " + Thread.currentThread().getName() + ")");
}
private void generatePermutations(String prefix, String remaining) {
int n = remaining.length();
if (n == 0) {
permutations.add(prefix);
} else {
for (int i = 0; i < n; i++) {
generatePermutations(prefix + remaining.charAt(i), remaining.substring(0, i) + remaining.substring(i + 1));
}
}
}
}
// Driver code
Scanner input = new Scanner(System.in);
System.out.print("Enter number of threads: ");
int threads = input.nextInt();
System.out.print("Enter number of tasks: ");
int tasks = input.nextInt();
System.out.print("Enter string for which permutations need to be found: ");
String str = input.next();
System.out.print("Enter number for which factorial is to be calculated: ");
int numfact = input.nextInt();
System.out.print("Enter number for which fibonacci is to be calculated: ");
int numfib = input.nextInt();
CustomThreadPool threadPool = new CustomThreadPool(threads);
// Submit tasks to the thread pool
for (int i = 1; i <= tasks; i++) {
threadPool.submit(new FibonacciTask(numfib));
threadPool.submit(new FactorialTask(numfact));
threadPool.submit(new PermutationTask(str));
}
// Shutdown the thread pool after submitting all tasks
threadPool.shutdown();
}
public static void main(String[] args) {
Office_4 wrk = new Office_4();
wrk.ques1_trial3();
System.out.println("Wow");
}
}