Please do it on Java. Project: The Sleeping Teaching Assistant A university comp
ID: 3605114 • Letter: P
Question
Please do it on Java.
Project: The Sleeping Teaching Assistant
A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TA’s office is rather small and has room for only one desk with a chair and computer. There are three chairs in the hallway outside the office where students can sit and wait if the TA is currently helping another student. When there are no students who need help during office hours, the TA sits at the desk and takes a nap. If a student arrives during office hours and finds the TA sleeping, the student must awaken the TA to ask for help. If a student arrives and finds the TA currently helping another student, the student sits on one of the chairs in the hallway and waits. If no chairs are available, the student will come back at a later time.
Using threads and semaphores, implement a solution in JAVA that coordinates the activities of the TA and the students.
1) Create n students, each will run as a separate thread.
2) Student threads will alternate between programming for a period of time and seeking help from the TA.
3) If the TA is available, students will obtain help
4) If the TA is not available, students will sit in a chair in the hallway (waitList)
5) If no chairs are available, students will resume programming and seek help at a later time
6) If the TA is sleeping, the student will notify TA with a semaphore
7) Use a semaphore to indicate a student in help mode
8) When a TA finishes helping a student, the TA must check to see if there are students waiting for help in the hallway.
9) The TA must help the students waiting in the hallway in the order that they started waiting.
10) If no students are waiting, then the TA can resume napping.
The best option for simulating students programming – as well as the TA providing help to a student – is to have appropriate threads sleep for a random period of time.
Make sure your output includes when the semaphores acquire permits and release permits, displays current mode (programming or getting help), and TA is sleeping or helping which student.
Explanation / Answer
import java.util.concurrent.Semaphore;
import java.util.Random;
public class Main
{
public static void main(String[] args)
{
// Number of students.
int numberofStudents = 5;
// Create semaphores.
SignalSemaphore wakeup = new SignalSemaphore();
Semaphore chairs = new Semaphore(3);
Semaphore available = new Semaphore(1);
// Used for randomly generating program time.
Random studentWait = new Random();
// Create student threads and start them.
for (int i = 0; i < numberofStudents; i++)
{
Thread student = new Thread(new Student(studentWait.nextInt(20), wakeup, chairs, available, i+1));
student.start();
}
// Create and start TA Thread.
Thread ta = new Thread(new TeachingAssistant(wakeup, chairs, available));
ta.start();
}
}
/**
* This semaphore implementation is used to "wakeup" the TA.
*/
class SignalSemaphore {
private boolean signal = false;
// Used to send the signal.
public synchronized void take() {
this.signal = true;
this.notify();
}
// Will wait until it receives a signal before continuing.
public synchronized void release() throws InterruptedException{
while(!this.signal) wait();
this.signal = false;
}
}
/**
This is the student thread. It will alternate between programming and seeking help from the TA.
*/
class Student implements Runnable
{
// Time to program before asking for help (in seconds).
private int programTime;
// Student number.
private int studentNum;
// Semaphore used to wakeup TA.
private SignalSemaphore wakeup;
// Semaphore used to wait in chairs outside office.
private Semaphore chairs;
// Mutex lock (binary semaphore) used to determine if TA is available.
private Semaphore available;
// A reference to the current thread.
private Thread t;
// Non-default constructor.
public Student(int program, SignalSemaphore w, Semaphore c, Semaphore a, int num)
{
programTime = program;
wakeup = w;
chairs = c;
available = a;
studentNum = num;
t = Thread.currentThread();
}
/**
* The run method will infinitely loop between programming and
* asking for help until the thread is interrupted.
*/
@Override
public void run()
{
// Infinite loop.
while(true)
{
try
{
// Program first.
System.out.println("Student " + studentNum + " has started programming for " + programTime + " seconds.");
t.sleep(programTime * 1000);
// Check to see if TA is available first.
System.out.println("Student " + studentNum + " is checking to see if TA is available.");
if (available.tryAcquire())
{
try
{
// Wakeup the TA.
wakeup.take();
System.out.println("Student " + studentNum + " has woke up the TA.");
System.out.println("Student " + studentNum + " has started working with the TA.");
t.sleep(5000);
System.out.println("Student " + studentNum + " has stopped working with the TA.");
}
catch (InterruptedException e)
{
// Something bad happened.
continue;
}
finally
{
available.release();
}
}
else
{
// Check to see if any chairs are available.
System.out.println("Student " + studentNum + " could not see the TA. Checking for available chairs.");
if (chairs.tryAcquire())
{
try
{
// Wait for TA to finish with other student.
System.out.println("Student " + studentNum + " is sitting outside the office. "
+ "He is #" + ((3 - chairs.availablePermits())) + " in line.");
available.acquire();
System.out.println("Student " + studentNum + " has started working with the TA.");
t.sleep(5000);
System.out.println("Student " + studentNum + " has stopped working with the TA.");
available.release();
}
catch (InterruptedException e)
{
// Something bad happened.
continue;
}
}
else
{
System.out.println("Student " + studentNum + " could not see the TA and all chairs were taken. Back to programming!");
}
}
}
catch (InterruptedException e)
{
break;
}
}
}
}
/**
*
*
*/
class TeachingAssistant implements Runnable
{
// Semaphore used to wakeup TA.
private SignalSemaphore wakeup;
// Semaphore used to wait in chairs outside office.
private Semaphore chairs;
// Mutex lock (binary semaphore) used to determine if TA is available.
private Semaphore available;
// A reference to the current thread.
private Thread t;
public TeachingAssistant(SignalSemaphore w, Semaphore c, Semaphore a)
{
t = Thread.currentThread();
wakeup = w;
chairs = c;
available = a;
}
@Override
public void run()
{
while (true)
{
try
{
System.out.println("No students left. The TA is going to nap.");
wakeup.release();
System.out.println("The TA was awoke by a student.");
t.sleep(5000);
// If there are other students waiting.
if (chairs.availablePermits() != 3)
{
do
{
t.sleep(5000);
chairs.release();
}
while (chairs.availablePermits() != 3);
}
}
catch (InterruptedException e)
{
// Something bad happened.
continue;
}
}
}
}
/* Sample Output: