Java Null PointerException。试图实现信号量

时间:2016-09-28 02:53:05

标签: java semaphore

我一直在研究这段代码。我想弄清楚问题是什么。我正在实现信号量以及它们如何在读取和写入过程之间进行选择,对于这种情况,读取mu进程更重要,因此具有更高的优先级。而且我认为我的Semaphore课程提出了问题而不是主要方法。异常位于Semaphore类的第29行,其中包含此代码for (int i = 0; i < updatedProcessArray.length; i++)

我有随机的0和1,0是读进程,而1是写进程。我的程序中的假设是所有进程同时出现,并且因为读者0总是具有更高的优先级,所以它们将始终首先被处理。当一个进程完成时,-1将标记它们的数组索引。程序循环遍历数组,直到数组中的每个东西都为负-1,这就是目标。

信号量类

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   
    }

1 个答案:

答案 0 :(得分:1)

在构造函数Semaphore中,您必须使用关键字引用class属性,这不应该在使用数组updatedProcessArray时创建新数组

更改

Semaphore(int[] processArray)
    {
        int[] updatedProcessArray = processArray.clone(); // copy array     
    }

Semaphore(int[] processArray)
    {
        this.updatedProcessArray = processArray.clone(); // copy array     
    }

另一个建议可以使用开关结构来避免许多

     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
            }