All pastes #9131 Raw Edit

Untitled

public java v1 · immutable
#9131 ·published 2005-04-08 20:39 UTC
rendered paste body
// code comes from://http://today.java.net/pub/a/today/2004/09/15/sync2.html// Topic: Thread Synchronization "Producerand consumer class"////I do NOT understand THREADS, studying them//GOAL:  Make sure actually undestand how this code progresses////I understand the concept of "one waits, while one goes"//I just don't understand how it is actually doing it////please see the commens throughout code, that details what I think is going on//// Thank You.//public class PC{   public static void main (String [] args)   {      Shared s = new Shared ();      new Producer (s).start ();         //Producer Thread is started FIRST, hop to line 25      new Consumer (s).start ();   }}class Shared{   private char c = '\u0000';   private boolean writeable = true;   ///////////////// line 25   synchronized void setSharedChar (char c)   {      while (!writeable)  {              ///////////////// since in line 25, writeable was intiantiaed to TRUE, this                                        /////////////////statement evaluates FALSE(because writeable = TRUE)                                                 try                            ///////////////// since while condition was                                        /////////////////NOT MET the try Block is skipped and we go to line 39         {            wait ();                                                                     }         catch (InterruptedException e) {}      }          this.c = c;                     ///////////////// line 39      writeable = false;              ///////////////// writeable now has a VALUE of False      notify ();                      /////////////////   }   synchronized char getSharedChar ()   {      while (writeable)  {           ///////////////// this evaluates FALSE         try                        /////////////////NOT MET the try Block is skipped and we go to line 54         {            wait ();                         }         catch (InterruptedException e) { }   }         writeable = true;         ///////////////// writeable set to TRUE:      notify ();                ///////////////// since no threads have entered the "wait()" stage, no thread can be notified                                ///////////////// the code goes back up top, and repeates itself                                ///////////////// OBVIOUSLY, this is NOT what is happening, I just do not understand                                 ///////////////// how to read the code      return c;   }}class Producer extends Thread{   private Shared s;   Producer (Shared s)   {      this.s = s;   }   public void run ()   {      for (char ch = 'A'; ch <= 'Z'; ch++)      {           s.setSharedChar (ch);           System.out.println (ch + " produced by producer.");      }   }}class Consumer extends Thread{   private Shared s;   Consumer (Shared s)   {      this.s = s;   }   public void run ()   {      char ch;      do      {         ch = s.getSharedChar ();         System.out.println (ch + " consumed by consumer.");      }      while (ch != 'Z');   }}