Java 空指针异常。尝试实现信号量
Java Null PointerException. Trying to implement semaphores
几天来我一直在研究这段代码。我正试图找出问题所在。我正在实现信号量以及它们如何在读取和写入进程之间进行选择,对于这种情况,读取 mu
进程更重要,因此具有更高的优先级。而且我认为我的 Semaphore
class 给出了问题而不是主要方法。异常是在信号量 class 的第 29 行,它具有此代码 for (int i = 0; i < updatedProcessArray.length; i++)
我有随机的 0 和 1,0 是读取进程,1 是写入进程。我程序中的假设是所有进程同时出现,并且因为读者 0
总是有更高的优先级,所以他们总是首先被处理。当一个进程完成时,-1 将标记它们的数组索引。程序循环遍历数组,直到数组中的每一项都是负数 -1,这就是 objective.
信号量class
class Semaphore
{
public int [] updatedProcessArray; // initializing array for update
public int readcount = 0; // keep track of sharedData process
public int signal; // process signal on 00 for reader, on for 01 writer, 10 wait for reader, 11 wait for writer.
//public int priortyReader = 0; // priorty check for readers, with 1 being priorty
public int processesCompleted = 0;
//Semaphore constructor
Semaphore(int[] processArray)
{
int[] updatedProcessArray = processArray.clone(); // copy array
}
public int signalQueue(int muxtex, int wrt, int index)
{
int mu = muxtex; // assgining all process semaphore
int wr = wrt; // assigning writer semahpore
int currentIndex = index; // index for current process
int priorityIndex = 0; // index for priority
int priorityReader = 0;
int minimumWriterIndex = 0; // index of lowest writer (writer with the highest priority)
//int array[];
// loop through array
for (int i = 0; i < updatedProcessArray.length; i++)
{
// Checking the top priority in the queue
//independent if statement
if (wr == updatedProcessArray[i] && currentIndex == i)
{
if (minimumWriterIndex != 1)
minimumWriterIndex = 1; // record first index with writer
}
if (mu == updatedProcessArray[i] && currentIndex == i)
{
//priorityIndex = 0;
//signal = 00; // the priority has been found, reader.
// As soon as the first priority is found reader priorty is found,
// there is no greater priorty in the queue
//priorityReader = 1; // all reader processes have priority over writer processes
//priorityIndex = i; // recording the priority index
return (updatedProcessArray[i] = -1); // marking off to show that job being processed.
}
else
{
if (mu == updatedProcessArray[i])
// keeping track of readers
priorityReader = 1; // to show all reader processes have priority over writer processes
}
}
if (priorityReader != 1 && minimumWriterIndex == 1)
return (updatedProcessArray[minimumWriterIndex] = -1);
return 0;
}
public int waitQueue(int mutex, int wrt, int index)
{
// declaring variables
int mu = mutex;
int wr = wrt;
int currentIndex = index;
if (readcount < 1)
{
// comparing current index value with semaphore mutex
if (mu == updatedProcessArray[currentIndex])
{
signal = 10;
return signal; //reader process waiting
}
// comparing current index value with semaphore wrt
if (wr == updatedProcessArray[currentIndex])
{
signal = 11; //writer process waiting
return signal;
}
return 0;
}
return 0;
}
// for our purpose Signal method will be used for wait as well
public int signal()
{
if (signal == 00 || signal == 01)
{
readcount++;
return signal;
}
if (signal == 10 || signal == 11)
{
readcount--;
return signal;
}
return 0;
}
public int getProcessesCompleted()
{
int sum = 0;
for (int i = 0; i < updatedProcessArray.length; i++)
{
if (updatedProcessArray[i] == -1)
sum++; // update sum
}
return sum;
}
public int[] updateArray()
{
return updatedProcessArray;
}
}
主要方法
import java.util.Random;
class SemaphoreTest
{
public static void main(String args[])
{
Random rn = new Random(); // creating a new random object
int processArraySize = rn.nextInt(11); // size of processArray
int[] processArray = new int[processArraySize]; // giving array size
int mutex = 0; // semaphore for readers
int wrt = 1; // semaphore for writers
// Now we will initialize populate processArray
for (int i = 0; i < processArraySize; i++)
{
int x = rn.nextInt(2); // we will denote 0 as reader and 1 as writer
processArray[i] = x; // assigning x to index i
}
Semaphore semaphore = new Semaphore(processArray); // creating a new semaphore object
int signal = 0;
int processCompleted = 0;
while(processCompleted < processArraySize)
{
// return the new and updated processArray each time
// a -1 will be where the processes have been completed
// the process will continue untill all process have been completed only -1's in the array
for (int i = 0; i < processArraySize; i++)
{
semaphore.signalQueue(mutex, wrt, i); // calling mutex semaphore and wrt semaphore
signal = semaphore.signal();
if (signal == 00)
{
System.out.println("reader: using shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
if (signal == 01)
{
System.out.println("writer: using shared data \t index: " + i
+ "\tnumber of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
// wait semaphore
semaphore.waitQueue(mutex, wrt, i); // calling mutex semaphore and wrt semaphore
if (signal == 10)
{
System.out.println("reader: waiting to access shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
if (signal == 10)
{
System.out.println("writer: waiting to access shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
}//end of for loop
// updating process completed
processCompleted = semaphore.getProcessesCompleted();
}//end of while loop
}//end of method
}
在构造函数信号量中,您必须使用关键字引用 class 属性 this should not create a new array as you work with the array updatedProcessArray
改变
Semaphore(int[] processArray)
{
int[] updatedProcessArray = processArray.clone(); // copy array
}
到
Semaphore(int[] processArray)
{
this.updatedProcessArray = processArray.clone(); // copy array
}
额外的建议可以使用 switch 结构来避免许多 if
switch(signal)
{
case 00: System.out.println("reader: using shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );break;
case 01:instruction;break;
case 02: instruction;break;
default: not option
}
几天来我一直在研究这段代码。我正试图找出问题所在。我正在实现信号量以及它们如何在读取和写入进程之间进行选择,对于这种情况,读取 mu
进程更重要,因此具有更高的优先级。而且我认为我的 Semaphore
class 给出了问题而不是主要方法。异常是在信号量 class 的第 29 行,它具有此代码 for (int i = 0; i < updatedProcessArray.length; i++)
我有随机的 0 和 1,0 是读取进程,1 是写入进程。我程序中的假设是所有进程同时出现,并且因为读者 0
总是有更高的优先级,所以他们总是首先被处理。当一个进程完成时,-1 将标记它们的数组索引。程序循环遍历数组,直到数组中的每一项都是负数 -1,这就是 objective.
信号量class
class Semaphore
{
public int [] updatedProcessArray; // initializing array for update
public int readcount = 0; // keep track of sharedData process
public int signal; // process signal on 00 for reader, on for 01 writer, 10 wait for reader, 11 wait for writer.
//public int priortyReader = 0; // priorty check for readers, with 1 being priorty
public int processesCompleted = 0;
//Semaphore constructor
Semaphore(int[] processArray)
{
int[] updatedProcessArray = processArray.clone(); // copy array
}
public int signalQueue(int muxtex, int wrt, int index)
{
int mu = muxtex; // assgining all process semaphore
int wr = wrt; // assigning writer semahpore
int currentIndex = index; // index for current process
int priorityIndex = 0; // index for priority
int priorityReader = 0;
int minimumWriterIndex = 0; // index of lowest writer (writer with the highest priority)
//int array[];
// loop through array
for (int i = 0; i < updatedProcessArray.length; i++)
{
// Checking the top priority in the queue
//independent if statement
if (wr == updatedProcessArray[i] && currentIndex == i)
{
if (minimumWriterIndex != 1)
minimumWriterIndex = 1; // record first index with writer
}
if (mu == updatedProcessArray[i] && currentIndex == i)
{
//priorityIndex = 0;
//signal = 00; // the priority has been found, reader.
// As soon as the first priority is found reader priorty is found,
// there is no greater priorty in the queue
//priorityReader = 1; // all reader processes have priority over writer processes
//priorityIndex = i; // recording the priority index
return (updatedProcessArray[i] = -1); // marking off to show that job being processed.
}
else
{
if (mu == updatedProcessArray[i])
// keeping track of readers
priorityReader = 1; // to show all reader processes have priority over writer processes
}
}
if (priorityReader != 1 && minimumWriterIndex == 1)
return (updatedProcessArray[minimumWriterIndex] = -1);
return 0;
}
public int waitQueue(int mutex, int wrt, int index)
{
// declaring variables
int mu = mutex;
int wr = wrt;
int currentIndex = index;
if (readcount < 1)
{
// comparing current index value with semaphore mutex
if (mu == updatedProcessArray[currentIndex])
{
signal = 10;
return signal; //reader process waiting
}
// comparing current index value with semaphore wrt
if (wr == updatedProcessArray[currentIndex])
{
signal = 11; //writer process waiting
return signal;
}
return 0;
}
return 0;
}
// for our purpose Signal method will be used for wait as well
public int signal()
{
if (signal == 00 || signal == 01)
{
readcount++;
return signal;
}
if (signal == 10 || signal == 11)
{
readcount--;
return signal;
}
return 0;
}
public int getProcessesCompleted()
{
int sum = 0;
for (int i = 0; i < updatedProcessArray.length; i++)
{
if (updatedProcessArray[i] == -1)
sum++; // update sum
}
return sum;
}
public int[] updateArray()
{
return updatedProcessArray;
}
}
主要方法
import java.util.Random;
class SemaphoreTest
{
public static void main(String args[])
{
Random rn = new Random(); // creating a new random object
int processArraySize = rn.nextInt(11); // size of processArray
int[] processArray = new int[processArraySize]; // giving array size
int mutex = 0; // semaphore for readers
int wrt = 1; // semaphore for writers
// Now we will initialize populate processArray
for (int i = 0; i < processArraySize; i++)
{
int x = rn.nextInt(2); // we will denote 0 as reader and 1 as writer
processArray[i] = x; // assigning x to index i
}
Semaphore semaphore = new Semaphore(processArray); // creating a new semaphore object
int signal = 0;
int processCompleted = 0;
while(processCompleted < processArraySize)
{
// return the new and updated processArray each time
// a -1 will be where the processes have been completed
// the process will continue untill all process have been completed only -1's in the array
for (int i = 0; i < processArraySize; i++)
{
semaphore.signalQueue(mutex, wrt, i); // calling mutex semaphore and wrt semaphore
signal = semaphore.signal();
if (signal == 00)
{
System.out.println("reader: using shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
if (signal == 01)
{
System.out.println("writer: using shared data \t index: " + i
+ "\tnumber of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
// wait semaphore
semaphore.waitQueue(mutex, wrt, i); // calling mutex semaphore and wrt semaphore
if (signal == 10)
{
System.out.println("reader: waiting to access shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
if (signal == 10)
{
System.out.println("writer: waiting to access shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );
}
}//end of for loop
// updating process completed
processCompleted = semaphore.getProcessesCompleted();
}//end of while loop
}//end of method
}
在构造函数信号量中,您必须使用关键字引用 class 属性 this should not create a new array as you work with the array updatedProcessArray
改变
Semaphore(int[] processArray)
{
int[] updatedProcessArray = processArray.clone(); // copy array
}
到
Semaphore(int[] processArray)
{
this.updatedProcessArray = processArray.clone(); // copy array
}
额外的建议可以使用 switch 结构来避免许多 if
switch(signal)
{
case 00: System.out.println("reader: using shared data \t index: " + i
+ "number of processe(s) completed: " + semaphore.getProcessesCompleted() );break;
case 01:instruction;break;
case 02: instruction;break;
default: not option
}