Develop java program with given requirements such as threads , semaphores and Runnable class methods?

Develop java program with given requirements such as threads , semaphores and Runnable class methods?

1. Tea Drinkers Problem: A group of n tea drinkers sit around a table to drink tea and talk. To drink tea, each tea drinker requires a cup, a saucer, and a spoon and tea drinker cannot drink tea until the tea drinker has all three. There are only n-1 cups, n-1 saucers, and n-1 spoons but any tea drinker can take any cup, saucer, or spoon as long as it is free. Each tea drinker is represented by a runnable class that looks like:

import java.util.concurrent.*;

public class TeaParty {

class TeaDrinker implements Runnable {

private int myID;

private int mySleepTimeNS;

public TeaDrinker(int id, int sleep) {

myID = id;

mySleepTimeNS = sleep;

}

private void sleep(int t) {

try {

Thread.sleep(t);

}

catch (InterruptedException ie) {

System.out.println(“Sleep interrupted; should not happen”);

}

}

public void run() {

System.out.println(“Tea drinker ” + myID + ” starting after sleep of ” + mySleepTimeNS + “ns”);

sleep(mySleepTimeNS);

while (true) { // run for ever sleep

(mySleepTimeNS);

System.out.println(“Tea drinker ” + myID + ” getting cup, saucer, and spoon”);

// ADD CODE TO GET THE CUP, SAUCER, AND SPOON HERE

System.out.println(“Tea drinker ” + myID + ” drinking tea for ” + mySleepTimeNS + “ns”);

sleep(mySleepTimeNS);

System.out.println(“Tea drinker ” + myID + ” finished drinking tea, releasing cup, saucer, and spoon”);

// ADD CODE TO RELEASE THE CUP, SAUCER, AND SPOON HERE

System.out.println(“Tea drinker ” + myID + ” will talk for ” + mySleepTimeNS + “ns”);

sleep(mySleepTimeNS);

}

}

}

public static void main(String [] args) { }

}

You will use this class as an inner class of a public class called TeaParty. This class will also define a set of semaphores that are used to synchonize the tea drinker threads in their use of the cups, saucers, and spoons. The main method of TeaParty will initialize the semaphore counts (you will use counting semaphores, one for each of the three items the tea drinkers need) and then the main method will create and start n TeaDrinker threads (you main method should get the value for n from the String array parameter of the main method.

Add code in the highlighted area to use the semaphores to synchronize the tea drinkers to use the n-1 sets of cup, saucers, and spoons. Submit your Java program TeaParty.java for this part of the project as part of your zip archive file. The Java Semaphore class is defined in the java.util.concurrent package. The semaphore methods that you will need to use are:

• Semaphore(int permits), the constructor. permits is the intial value for the semaphore count.

• void acquire() Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted. acquire() throws the InterruptedException and must be used in a try-catch block.

• void release() Releases a permit, returning it to the semaphore. No exception is thrown by this method.

2. In this program, you’ll five separate Runnable classes:

• one class, Reader, reads a line of text from System.in and places it in a shared String variable

• one class, CCounter, looks at the shared String and places the number of characters in a shared character, VCounter, count variable • one class, DCounter, looks at the shared String and places the number of vowels in the string in a shared vowel count variable

• one class looks at the shared String and places the number of digits in the string in a share digit count variable

• one class uses the shared String , character count, vowel count, and digit count variable to display (using System.out.println) the original string, the total number of characters, the number of vowels and digits in the string

The threads have a dependency graph that looks like this:

PLEASE LOOK AT UPLOADED FILE TO SEE THE GRAPH.

This graph means that CCounter, VCounter, adn DCounter cannot not begin work until Reader has put a new string in the share String variable. Displayer cannot begin work until all three of CCounter, VCounter, and DCounter have placed their respective counts in the three shared variables. An after it has read the first string to begin the sequence, Reader must wait until Displayer has displayer the current string and counts before it reads the next string.

The run methods of each of the Runnable classes should have a forever while loop and should use semaphores to sequence the threads properly. Use the semaphore version of the the circle circumference program as an example.

Write a public class called WorkFlow with the five Runnable classes above as inner classes. The main method of WorkFlow should create and start each of the threads. You should then be able to type lines and see the proper display for each line.

You can code each of the run methods to break out of the while loop if the string is QUIT, but be careful to release the necessary semaphores in the break condition to allow the other threads to quit.

Submit your Java program WorkFlow.java for this part of the project as part of your zip archive file.

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper