Write a program where 3 separate threads simulate flipping a coin. The 3 threads should execute concurrently and each thread should do 1000 flips. Keep track of what is currently the record number of consecutive heads/tails (separately) and which thread produced that sequence. Every time a thread produces a longer sequence, display a message on the screen. Make sure other threads can’t interfere when updating the current record. N.B: This is a java object oriented program assignment.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
This code has divided into three classes. CoinSimulation that runs the simulation, Coin class for representing a Coin and CoinFlipThread to represent a Thread that flips a coin 1000 times. You can place all classes in a single file (named CoinSimulation.java) or you can paste them separately
// CoinFlipSimulation.java
public class CoinFlipSimulation {
// current record of longest consecutive sequence
static int longestSequence = 0;
// id of thread that produced the above record
static int idOfThread = -1;
/**
* method to update the longestSequence with the new record. This will
* replace longestSequence with currentSequence if
* currentSequence>longesSequence and prints the message. This method is
* made synchronized so that other threads wont interfere when one thread is
* invoking this method
*
* @param id
* - numeric id of the thread (1, 2 and 3)
* @param currentSequence
* current sequence that thread with id 'id' created
*/
static synchronized void update(int id, int currentSequence) {
// checking if currentSequence breaks the record
if (currentSequence > longestSequence) {
// updating current records
longestSequence = currentSequence;
idOfThread = id;
// displaying message letting user know that a new record has been
// created
System.out.println("Thread " + idOfThread
+ " has created a new record "
+ "of consecutive heads/tails (" + longestSequence
+ " times)");
}
}
public static void main(String[] args) {
// creating a Coin
Coin coin = new Coin();
// creating three threads labeled 1, 2 and 3, passing the above coin
// instance
CoinFlipThread thread1 = new CoinFlipThread(1, coin);
CoinFlipThread thread2 = new CoinFlipThread(2, coin);
CoinFlipThread thread3 = new CoinFlipThread(3, coin);
// starting threads
thread1.start();
thread2.start();
thread3.start();
}
}
// class to represent a Coin
class Coin {
// current face - 0 for heads, 1 for tails
int face;
// constructor flipping coin initially
public Coin() {
flip();
}
// method to flip the coin, made synchronized to prevent interference
public synchronized void flip() {
// generating a value 0 or 1 and assigning to face
face = (int) (Math.random() * 2);
}
// method to get the current face,made synchronized to prevent interference
public synchronized String getFace() {
// returning HEADS if face is 0, else TAILS
if (face == 0) {
return "HEADS";
} else {
return "TAILS";
}
}
}
// a class to represent one Thread that flips a coin 1000 timess
class CoinFlipThread extends Thread {
// attributes
Coin coin;
int id;
String currentFace = "";
// constructor taking thread id and a Coin instance
public CoinFlipThread(int id, Coin coin) {
this.id = id;
this.coin = coin;
}
@Override
public void run() {
// current consecutive sequence count this thread has produced
int sequence = 0;
// looping for 1000 times
for (int i = 0; i < 1000; i++) {
// flipping coin
coin.flip();
// if current face is same as coin's face, incrementing sequence
if (coin.getFace().equals(currentFace)) {
sequence++;
// updating the records
CoinFlipSimulation.update(id, sequence);
} else {
//resetting sequence to 0
sequence = 0;
//updating currentFace
currentFace = coin.getFace();
}
}
System.out.println("Thread " + id + " finished execution.");
}
}
/*OUTPUT*/
Thread 2 has created a new record of consecutive heads/tails (1 times)
Thread 2 has created a new record of consecutive heads/tails (2 times)
Thread 1 has created a new record of consecutive heads/tails (3 times)
Thread 1 has created a new record of consecutive heads/tails (4 times)
Thread 2 has created a new record of consecutive heads/tails (5 times)
Thread 2 has created a new record of consecutive heads/tails (6 times)
Thread 2 has created a new record of consecutive heads/tails (7 times)
Thread 3 has created a new record of consecutive heads/tails (8 times)
Thread 2 has created a new record of consecutive heads/tails (9 times)
Thread 2 has created a new record of consecutive heads/tails (10 times)
Thread 3 finished execution.
Thread 2 finished execution.
Thread 1 finished execution.
Write a program where 3 separate threads simulate flipping a coin. The 3 threads should execute concurrently and each thread should do 1000 flips. Keep track of what is currently the record number of...