Database Reference
In-Depth Information
}
public void
run() {
System.
out
.println("Starting "+name);
Random rng =
new
Random();
do
{
if
(election.isLeader()) {
System.
out
.println(name+": I am becoming
the leader.");
do
{
if
(rng.nextDouble() < 0.30) {
System.
out
.println(name+": I'm about to
fail!");
try
{
election.close();
}
catch
(InterruptedException e) { }
return
;
}
System.
out
.println(name
+": I'm leader and handling a work
element");
try
{
Thread.
sleep
(rng.nextInt(1000));
}
catch
(InterruptedException e) { }
}
while
(election.isLeader());
//If we lose leadership but are still
connected, try again.
if
(election.connected) {
try
{
election.requestLeadership();
}
catch
(Exception e) {
return
;
}
}
}
}
while
(election.connected);