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();}}} }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education