-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multithreading.java
211 lines (93 loc) · 2.53 KB
/
Multithreading.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
//Anushka Bhandari
class recurse extends RecursiveAction {
private volatile static Map<String, recurse> instances =
new HashMap<String, recurse>();
public static synchronized recurse getInstance(long x, long y)
{
String key = x + ", " + y;
if (!instances.containsKey(key)) {
instances.put(key, new recurse(x, y));
}
return instances.get(key);
}
public static Map<String, recurse> getInstances() {
return instances;
}
public static void setInstances(Map<String, recurse> instances) {
recurse.instances = instances;
}
public long getAns() {
return ans;
}
private final long n;
private final long k;
long ans;
public recurse(long n,long k) {
this.n=n;
this.k=k;
}
@Override
protected void compute() {
if (n <= 0 || k <=0 || n == k) {
ans= 1;
}
else {
recurse left = recurse.getInstance(n - 1, k - 1);
recurse right = recurse.getInstance(n - 1, k);
left.fork();
right.compute();
left.join();
ans= (left.ans + right.ans);
}
}
//4324678919983428184
}
class recursePP extends RecursiveAction{
long n;
long k;
long ans;
public recursePP(long n,long k) {
this.n=n;
this.k=k;
}
@Override
protected void compute() {
if (n <= 0 || k <=0 || n == k) {
ans= 1;
}
else {
recursePP left = new recursePP(n - 1, k - 1);
recursePP right = new recursePP(n - 1, k);
left.fork();
right.compute();
left.join();
ans= (left.ans + right.ans);
}
}
//4324678919983428184
}
// 1//61 sec//64
// 2//70 sec//87
// 3//80 sec//256
// 4//84 sec//97
public class lab8{
public static void main(String[] args) throws NumberFormatException, IOException {
System.out.println("Enter the number of Threads");
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
// int r=Integer.parseInt(reader.readLine());
final long startTime = System.currentTimeMillis();
ForkJoinPool pool =new ForkJoinPool(4);
recurse s= recurse.getInstance(50,25);
pool.invoke(s);
final long finaltime = System.currentTimeMillis();
System.out.println(s.ans);
System.out.println(finaltime-startTime+" "+ "TIME");
}
}