forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http://www.dsalgo.com/2013/03/distributed-circular-linked-list-sum.html
- Loading branch information
0 parents
commit b6ffe86
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
http://www.dsalgo.com/2013/03/distributed-circular-linked-list-sum.html | ||
*/ | ||
|
||
package com.dsalgo; | ||
|
||
class DistributedCircularListSum | ||
{ | ||
public static volatile boolean startFlag = false; | ||
private static Boolean flag = 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); | ||
a.next = b; | ||
b.next = c; | ||
c.next = d; | ||
d.next = e; | ||
e.next = a; | ||
startFlag = true; | ||
} | ||
|
||
private static class Node | ||
{ | ||
Node next; | ||
int value; | ||
Integer data; | ||
|
||
public Node(int value) | ||
{ | ||
this.value = value; | ||
new Thread(new NodeRunner(this, value)).start(); | ||
} | ||
|
||
public synchronized void send(int data) | ||
{ | ||
this.data = data; | ||
} | ||
|
||
} | ||
|
||
private static class NodeRunner implements Runnable | ||
{ | ||
Node node; | ||
int id; | ||
|
||
public NodeRunner(Node node, int id) | ||
{ | ||
this.node = node; | ||
this.id = id; | ||
} | ||
|
||
@Override | ||
public void run() | ||
{ | ||
while (!startFlag) | ||
{ | ||
} | ||
|
||
boolean isFirst = false; | ||
synchronized (flag) | ||
{ | ||
if (flag == false) | ||
{ | ||
flag = true; | ||
isFirst = true; | ||
} | ||
} | ||
if (isFirst) | ||
node.next.send(node.value); | ||
else | ||
{ | ||
while (node.data == null) | ||
{ | ||
try | ||
{ | ||
Thread.sleep(10); | ||
} catch (InterruptedException e) | ||
{ | ||
|
||
} | ||
} | ||
int sum = node.value + node.data; | ||
node.data = null; | ||
node.next.send(sum); | ||
} | ||
while (node.data == null) | ||
{ | ||
try | ||
{ | ||
Thread.sleep(10); | ||
} catch (InterruptedException e) | ||
{ | ||
|
||
} | ||
} | ||
System.out.println("id:" + id + "sum:" + node.data); | ||
node.next.send(node.data); | ||
} | ||
} | ||
} | ||
|