Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TAs office

A university computer science department has a teaching assistant (TA) who helps undergraduate students with their programming assignments during regular office hours. The TAs 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.

-Is the porgram I wrote correct?

IN JAVA

import java.util.*; import java.util.concurrent.Semaphore; public class TeachingAssistant1 { static Semaphore availableHelp = new Semaphore(4); static Semaphore availableTA = new Semaphore(1); public static void main(String [] args) { try { new TeachingAssistant1(4); Thread.sleep(30000); System.exit(0); } catch(Exception e) { } } public TeachingAssistant1(int number) { new Thread(new TAThread()).start(); new Thread(new StudentThread(" Student1 ")).start(); new Thread(new StudentThread(" Student2")).start(); new Thread(new StudentThread(" Student3 ")).start(); new Thread(new StudentThread(" Student4")).start(); new Thread(new StudentThread(" Student5 ")).start(); } public class TAThread extends Thread { public void run() { while(true) { try { if(!availableTA.hasQueuedThreads()) { System.out.println("The TA is sleeping"); } else { availableTA.release(); System.out.println("The TA is helping a student "); System.out.println("The current number of students waiting or being helped is "+ availableTA.getQueueLength() +1); Thread.sleep(5000); } } catch(Exception e) { } } } }// end of TAThread class public class StudentThread extends Thread { private String name1 =""; public StudentThread(String name) { name1 = name; } public void setStudentName(String name) { name1 = name; } public String getStudentName() { return name1; } public void run() { while(true) { try { Thread.sleep(5000); System.out.println("Student" + getStudentName() + " is programming"); if(availableTA.getQueueLength() >= 3) { System.out.println("The chairs are taken " + getStudentName() +" has to come back later"); } else { availableTA.acquire(); } } catch(Exception e) { } } } public String toString() { return getStudentName(); } }// end of Student class }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Creating A Database In Filemaker Pro Visual QuickProject Guide

Authors: Steven A. Schwartz

1st Edition

0321321219, 978-0321321213

More Books

Students also viewed these Databases questions

Question

Develop successful mentoring programs. page 400

Answered: 1 week ago