Monitors in Java . How variable updated iteratively?

I am going through a slide about Java monitors. I can not understand how this the code below works.

    public synchronized void add( int value){
         int position =  writeIndex ;
             try{
                    Thread.sleep(generator.nextInt(500));
                } catch(InterruptedException ex){
                    Thread.currentThread().interrupt();
                 }
               array[position] =  value;
               System.out.print("%s wrote %2d to element %d.%n");
              [![enter image description here][1]][1] Thread.currentThread.getName(),value,position);
                
                 ++writeIndex;

     }


Read more here: https://stackoverflow.com/questions/67213824/monitors-in-java-how-variable-updated-iteratively

Content Attribution

This content was originally published by Perviz Pirizade at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: