Question: I need to convert this java program to a C ++ or C programming language? import java.util.concurrent.*; public class Office { public static void main(String[]
I need to convert this java program to a C ++ or C programming language?
import java.util.concurrent.*;
public class Office { public static void main(String[] args) { // ArrayBlock size=5 (number of office chairs) BlockingQueue
// Professor and StudentScheduler instance creation StudentScheduler studentScheduler = new StudentScheduler(sharedQueue); Professor professor = new Professor(sharedQueue);
// set thread priority studentScheduler.setPriority(Thread.MAX_PRIORITY); professor.setPriority(Thread.MAX_PRIORITY);
// start threads studentScheduler.start(); professor.start();
try { // studentScheduler thread completion standby studentScheduler.join();
// professor thread completion standby professor.join();
// if studentScheduler thread not active, print exit message if (!studentScheduler.isAlive()) { System.out.println("Professor has locked up and left the building..."); } } catch (InterruptedException err) { err.printStackTrace(); } } } --------------------------------------------------------------------------------------------------------------- import java.util.concurrent.*;
public class Professor extends Thread { // ArrayBlocking instance creation private BlockingQueue
// shared queue creation public Professor(BlockingQueue
// run() method public void run() { try { // declare variables Student student; Boolean flag = Boolean.TRUE; int recObject = 0;
// shared queue message consumption while (true) { // if Student queue empty and bool=true, professor plays Halo... // even though Battlefield is more fun if (sharedQueue.isEmpty() && flag == Boolean.TRUE) { System.out.println("Professor starts playing Halo... "); flag = Boolean.FALSE; } else { // remove Student from queue; add to receiving object student = sharedQueue.take(); recObject++; System.out.println("Professor starts meeting with " + student.getStudName()); sleep(student.getTimerAmt()); flag = Boolean.TRUE; } if (recObject == 301) { break; } } } catch (InterruptedException err) { err.printStackTrace(); } } } ------------------------------------------------------------------------------------------------- public class Student { // declare variables private String studName; private int timerAmt;
// Student set method public void setStudName(String studName) { this.studName = studName; }
// Student get method public String getStudName() { return studName; }
// Timer set method public void setTimerAmt(int timerAmt) { this.timerAmt = timerAmt; }
// Timer get method public int getTimerAmt() { return timerAmt; } } --------------------------------------------------------------------------------------------- import java.util.concurrent.*;
class InsObject { // insert Student object into queue public void insert(BlockingQueue
// create Student object, set name, and set timer; Student student = new Student(); student.setStudName("Student_" +i); student.setTimerAmt(i + randomNum);
try { Thread.sleep(15); // put Student into queue sharedQueue.put(student); System.out.println("Meanwhile, Student_" +i + " sits down in the waiting area..."); } catch (InterruptedException err) { err.printStackTrace(); } } } }
public class StudentScheduler extends Thread { // ArrayBlocking queue object private final BlockingQueue
// InsObject class object InsObject insObject = new InsObject();
// StudentScheduler constructor public StudentScheduler(BlockingQueue
// run() method at start of thread public void run() { // synchronize insObject with synchronized block synchronized (insObject) { insObject.insert(sharedQueue); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
