forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistributedSystemSum.java
More file actions
114 lines (101 loc) · 1.94 KB
/
DistributedSystemSum.java
File metadata and controls
114 lines (101 loc) · 1.94 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
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/03/distributed-binary-tree-sum.html
*/
package com.dsalgo;
public class DistributedSystemSum
{
public static volatile boolean startFlag = false;
public static void main(String[] args)
{
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
Node d = new Node(4);
Node e = new Node(5);
Node f = new Node(6);
Node g = new Node(7);
Node h = new Node(8);
a.left = b;
b.parent = a;
a.right = c;
c.parent = a;
b.left = d;
d.parent = b;
c.left = e;
e.parent = c;
c.right = f;
f.parent = c;
f.left = g;
g.parent = f;
f.right = h;
h.parent = f;
startFlag = true;
}
private static class Node
{
Node parent;
Node left;
Node right;
int value;
int sum = 0;
int receiveCount = 0;
public Node(int value)
{
this.value = value;
NodeRunner nodeRunner = new NodeRunner(this, value);
new Thread(nodeRunner).start();
}
public synchronized void send(Integer data)
{
sum += data;
receiveCount++;
}
public synchronized int getReceivedCount()
{
return receiveCount;
}
}
private static class NodeRunner implements Runnable
{
Node node;
int id;
NodeRunner(Node node, int id)
{
this.node = node;
this.id = id;
}
@Override
public void run()
{
while (!DistributedSystemSum.startFlag)
{
}
int childCount = 0;
if (node.left != null)
childCount++;
if (node.right != null)
childCount++;
while (node.getReceivedCount() != childCount)
{
}
int sum = node.sum + node.value;
node.sum = 0;
if (node.parent != null)
{
node.parent.send(sum);
while (node.getReceivedCount() != childCount + 1)
{
}
} else
{
node.sum = sum;
}
if (node.left != null)
node.left.send(node.sum);
if (node.right != null)
node.right.send(node.sum);
System.out.println("Thread id=" + id + " sum=" + node.sum);
}
}
}