Please rewrite the code: Here is the code: class PidManager {// Declare the range of possible pid valuesprivate static final int MIN_PID = 300;private static final int MAX_PID = 5000; // Declare a memory locationprivate static int[] pids; // Create a method named allocatMap().// This method is used to allocate the// memory to store the pids.public static int allocatMap() {pids = new int[MAX_PID - MIN_PID];if (pids == null) {System.out.println("Memory allocation failed.");return -1;} for (int i = 0; i < pids.length; i++) {pids[i] = 0;}System.out.println("Memory allocated successfully.");return 1;} // Create a method named allocatPID().// This method is used to allocates and returns a pid.public static int allocatPID() {if (pids == null) {System.out.println("PID Manager is not initialized ");return -1;} int pidNum = -1; for (int i = 0; i < pids.length; i++) {if (pids[i] == 0) {pids[i] = 1;pidNum = i + MIN_PID;break;}}if (pidNum == -1) {System.out.println("Unable to allocat PID");return -1;}System.out.println("Allocate PID :" + pidNum);return pidNum;} // Create a method named releasePID().// This method is used to release a pid.public static void releasePID(int pidNum) {if (pids == null) {System.out.println("PID Manager is not initialized ");return;}if (pidNum < MIN_PID || pidNum > MAX_PID) {System.out.println("Given PID is out or Range");}int newPid = pidNum - MIN_PID;if (pids[newPid] == 0) {System.out.println("PID " + pidNum + " is already released ");return;} System.out.println("Releasing PID :" + pidNum);pids[newPid] = 0;}} public class PidDemo { static class MyThread extends Thread {public void run() {int pid = PidManager.allocatPID();try {Thread.sleep(1000 * (int) (Math.random() * 10));} catch (InterruptedException e) {} finally {PidManager.releasePID(pid);}}} public static void main(String[] args) {PidManager.allocatMap(); int numThreads = 100;Thread threads[] = new Thread[100];for(int i=0; i<numThreads; i++) {threads[i] = new MyThread();}for(int i=0; i<numThreads; i++) {threads[i].start();}for(int i=0; i<numThreads; i++) {try {threads[i].join();} catch (InterruptedException e) {e.printStackTrace();}}} }

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 13RQ
icon
Related questions
Question

Please rewrite the code:

Here is the code:

class PidManager {
// Declare the range of possible pid values
private static final int MIN_PID = 300;
private static final int MAX_PID = 5000;

// Declare a memory location
private static int[] pids;

// Create a method named allocatMap().
// This method is used to allocate the
// memory to store the pids.
public static int allocatMap() {
pids = new int[MAX_PID - MIN_PID];
if (pids == null) {
System.out.println("Memory allocation failed.");
return -1;
}

for (int i = 0; i < pids.length; i++) {
pids[i] = 0;
}
System.out.println("Memory allocated successfully.");
return 1;
}

// Create a method named allocatPID().
// This method is used to allocates and returns a pid.
public static int allocatPID() {
if (pids == null) {
System.out.println("PID Manager is not initialized ");
return -1;
}

int pidNum = -1;

for (int i = 0; i < pids.length; i++) {
if (pids[i] == 0) {
pids[i] = 1;
pidNum = i + MIN_PID;
break;
}
}
if (pidNum == -1) {
System.out.println("Unable to allocat PID");
return -1;
}
System.out.println("Allocate PID :" + pidNum);
return pidNum;
}

// Create a method named releasePID().
// This method is used to release a pid.
public static void releasePID(int pidNum) {
if (pids == null) {
System.out.println("PID Manager is not initialized ");
return;
}
if (pidNum < MIN_PID || pidNum > MAX_PID) {
System.out.println("Given PID is out or Range");
}
int newPid = pidNum - MIN_PID;
if (pids[newPid] == 0) {
System.out.println("PID " + pidNum + " is already released ");
return;
}

System.out.println("Releasing PID :" + pidNum);
pids[newPid] = 0;
}
}

public class PidDemo {

static class MyThread extends Thread {
public void run() {
int pid = PidManager.allocatPID();
try {
Thread.sleep(1000 * (int) (Math.random() * 10));
} catch (InterruptedException e) {
} finally {
PidManager.releasePID(pid);
}
}
}

public static void main(String[] args) {
PidManager.allocatMap();

int numThreads = 100;
Thread threads[] = new Thread[100];
for(int i=0; i<numThreads; i++) {
threads[i] = new MyThread();
}
for(int i=0; i<numThreads; i++) {
threads[i].start();
}
for(int i=0; i<numThreads; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Knowledge Booster
void method
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,