title: how to achieve mutual exclusion using synchronized method in this application
body: <p>i have run into a little problem in here. I am doing a concurrent program in Java. Problem is: There are 4 people (students) that are trying to access printer, to print 5 documents. But only one can print at the time (kind of obvious) 5 documents. When they finish they notify other that they done and other thread accesses the resource. i have a Main class, student class and Monitor (laser printer), Document class that holds info about the document like (number of pages, name user id etc)+ few interfaces for printer. I have managed to run successfully threads but they are not synchronized (mutual exclusion)<br>
  So the question is how do i achieve mutual exclusion ( that only one person can print at the time his number of docs)<br>
  Thank you for looking, time and hints :) </p>
  
  <p>Main class </p>
  
  <pre><code>    String S1Name = "bob";
      String S2Name = "klara";
      String S3Name = "John";
      String S4Name = "Iga";
  
      String T1Name = "Man";
      String T2Name = "Woman";
  
  
      final int NoOfDocs = 5;
      ServicePrinter sp = new LaserPrinter();
  
      ThreadGroup groupA = new ThreadGroup("Group A"); 
      ThreadGroup groupB = new ThreadGroup("Group B");
  
      Student student1 = new Student(sp,NoOfDocs,S1Name, groupA);
      Student student2 = new Student(sp,NoOfDocs,S2Name, groupA);
      Student student3 = new Student(sp,NoOfDocs,S3Name, groupA);
      Student student4 = new Student(sp,NoOfDocs,S4Name, groupA);
  
      TonerTechnician TT = new TonerTechnician(groupB);
      PaperTechnician PT = new PaperTechnician(groupB);
  
      /*
       * Start Student Threads
       */
      student1.start();
      student2.start();
      student3.start();
      student4.start();
  
      /*
       * Start Technician threads
       */
      TT.start();
      PT.start();
  </code></pre>
  
  <p>Student Class</p>
  
  <pre><code> private final ServicePrinter serviceprinter;
      private final int NoOfDocs;
      private final String Name;
      private final ThreadGroup threadgroup;
  
      public Student(ServicePrinter serviceprinter, int NoOfDocs, String Name, ThreadGroup threadgroup)
      {
          this.serviceprinter = serviceprinter;
          this.NoOfDocs = NoOfDocs; 
          this.Name = Name;
          this.threadgroup = threadgroup;
      }
      @Override
      public void run()
      {
          /*
           * each students prints 5 documents (different name and length)
           */
          final LaserPrinter lp = new LaserPrinter();
          //sleep from 1 to 5 sec random time
          final Random random = new Random();       
          char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
          StringBuilder sb = new StringBuilder();
          /*
           * Create random document name 10 characters long
           */
          for (int i = 0; i &lt; 10; i++) 
          {
              char c = chars[random.nextInt(chars.length)];
              sb.append(c);
          }
          String docName  = sb.toString();
          /*
           * print 5 documents (random sleep time between printing)
           */
          for(int i = 0; i &lt; NoOfDocs; i++)
          {
              try 
              {
                  Document coursework = new Document(Name,docName,random.nextInt(90)+10);
                  lp.printDocument(coursework);
                  Thread.sleep(random.nextInt(1000)+4000);                
              } 
              catch (InterruptedException ex) 
              {
                  Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
              }            
          }
          System.out.println("User: " + Name+ " completed printing");
  </code></pre>
  
  <p>Monitor class </p>
  
  <pre><code>    int tonerLevel = 500;
      int paperLevel = 250;
  
      private final String PrinterName = "HP";
      private final String PrinterID = "LX-440";
      private int CurrentPaperLevel;
      private int CurrentTonerLevel;
      private int NoOfDocsPrinted;
  
      @Override
      public synchronized void printDocument(Document document) {
          System.out.println(document);
  
      }
  </code></pre>