这对我来说有点复杂,所以请多多包涵。我正在模拟分配 PID 的进程标识符 (PID) 管理器。它已扩展为包含一个多线程程序,该程序将分配 PID,随机休眠一段时间,然后释放 PID。我有三门课;PID 类、Thread 类和驱动程序。我在驱动程序中有一个 PID 数组和一个线程数组。我已经设法将 PID 分配给数组中的每个 Thread 对象,但最后我总是得到一个 java.lang.NullPointerException。这是代码:
public class PID {
private int pid; // Unique process identifier
private boolean availability; // Used to determine PID's availability--1 for available, 0 for unavailable
public PID() {}; // Empty constructor for PID object
public PID(int p, boolean a) { // Constructor for PID object with parameters
pid = p;
availability = a;
};
public void setPID(int pid) { // Sets PID's value
this.pid = pid;
}
public void setAvailability(boolean availability) { // Sets availability of PID
this.availability = availability;
}
public int getPID() { // Gets array of PIDs
return this.pid;
}
public boolean getAvailability() {
return this.availability;
}
public void allocatePID(int pid) { // Will allocate a PID and return PID
this.setPID(pid);
this.setAvailability(false);
}
public void releasePID() { // Will release PID to be available for use
this.setAvailability(true);
}
}
线程类
import java.util.*;
public class MyThread extends PID implements Runnable {
public MyThread() {}; // Constructor for thread object
public void run() {
Random gen = new Random(); // Generates random values
int sleepTime; // Sleep time
sleepTime = gen.nextInt(60000 - 1000) + 1000; // Generates random sleep time between 1 and 60 seconds (1000 ms and 60000 ms)
try {
System.out.println("This thread will sleep for " + sleepTime + " seconds.");
Thread.sleep(sleepTime);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("The thread has been terminated");
}
}
和司机
public class PID_Driver {
public static void main (String[] args)
{
Random gen = new Random(); // Will generate a random numbers
int randomInt; // Random integer values
boolean randomBool; // Random boolean values
final int NUM_OF_PIDS = 100; // Constant number of PIDs
final int NUM_OF_THREADS = 20; // Constant number of threads
int j = 0;
PID[] pids = new PID[NUM_OF_PIDS ]; // Array of PID objects
MyThread[] threads = new MyThread[NUM_OF_PIDS]; // Array of threads
for (int i = 0; i < NUM_OF_PIDS ; i++) {
pids[i] = new PID(); // Creates a new PID object
randomInt = gen.nextInt(5000 - 300) + 300; // Generates a random integer value between 300 and 5000
pids[i].setPID(randomInt); // Sets each PID value with random integer
randomBool = gen.nextBoolean(); // Generates a random boolean value
pids[i].setAvailability(randomBool); // Sets each availability status with a boolean value
}
for (int i = 0; i < NUM_OF_PIDS; i++) {
System.out.printf("\n%-10s ", "PID value ");
System.out.printf("%8d", pids[i].getPID());
System.out.printf("%8s", pids[i].getAvailability());
}
System.out.println();
for (int i = 0; i < NUM_OF_THREADS; i++)
threads[i] = new MyThread(); // Creates new thread
while (threads[NUM_OF_PIDS-1] == null) {
for (int i = 0; i < NUM_OF_PIDS; i++) {
if ((pids[i].getAvailability())) {
threads[j].allocatePID(pids[i].getPID());
System.out.println("This thread has a PID value of " + threads[j].getPID() + " and its availability is now " + threads[j].getAvailability());
++j;
}
}
}
// for (int i = 0; i < NUM_OF_THREADS; i++) {
//threads[i].run(); // Run the thread
// threads[i].releasePID(); // Release the thread
// }
}
}
我知道这与我如何编写 for 循环以根据可用性将 PID 分配给线程有关。我什至尝试在(疯狂的)尝试中添加一个 while 循环条件,以在每个线程都被分配了一个 PID 后停止循环,但我没有成功。
编辑:这是堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at PID_Driver.main(PID_Driver.java:52)
所以基本上 NPE 出现在这一行:
threads[j].allocatePID(pids[i].getPID());