Merosansar 0 Newbie Poster

The code for sleeping barber problem. When the status of the barber
is set to busy then the customer cannot have a hair cut and is added to the
waiting queue. when the customer is set to free then the customer have a
hair cut.
Here the problem is that only the first customer have a hair cut and the other
customers added in the queue cannot have a hair cut.
Any help will be appreciated.

customeradd class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * cutomeradd.java
 *
 * Created on Apr 4, 2010, 12:47:17 PM
 */

package gui;

import customer.Customer;

/**
 *
 * @author hp
 */
public class cutomeradd extends javax.swing.JFrame {

    public static int enteredcustomerpos=1;

    /** Creates new form cutomeradd */
    public cutomeradd() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOTe modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        addCusotmer = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addCusotmer.setText("Add");
        addCusotmer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCusotmerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(71, 71, 71)
                .addComponent(addCusotmer)
                .addContainerGap(98, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(addCusotmer)
                .addContainerGap(57, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    @SuppressWarnings("static-access")
    private void addCusotmerActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:

       
        System.out.println("the position of the customer is "+enteredcustomerpos);

         Customer cust=new Customer();
         cust.setPosition(enteredcustomerpos);
         enteredcustomerpos++;
         Thread customer=new Thread(cust);
         customer.start();
        
       


    
 

    }                                           

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new cutomeradd().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton addCusotmer;
    // End of variables declaration                   

}

Customer class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package customer;

import barber.Barber;

/**
 *
 * @author hp
 */
public class Customer extends Thread{

    public  int position=0;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

 


    Barber bar=new Barber();

    @Override
    public void run()
    {

       // Thread currentcust=Thread.currentThread();

        this.bar.cuthair(position,this);
        //  this.bar.cuthair(position,currentcust,this);

       // this.bar.run();



    }

   
}

Barber class

package barber;

import customer.Customer;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hp
 */
public class Barber extends Thread {

    //setting the status of the barber to sleep
   public static int status=0;
   public static List queue=new LinkedList();
  public static int custpos=0;
   //Customer currentcust=new Customer();
   // public static int pos=0;
//    //private Random rand=new Random(System.currentTimeMillis());
//
//
//
    @SuppressWarnings("static-access")
   public synchronized void cuthair(int position,Thread currentcust)
   {


        
       System.out.println("the name of the current thread is "+currentcust.getName());

       if(status==1)
        {
           custpos=position;
            System.out.println("the customer in "+position+" has been added to the queue");
           queue.add(currentcust);
            while(status==1)
            {
                System.out.println("inside the while loop");
                try
                {
                    System.out.println("customer is waiting for the barber to be free");
                    wait();

                }
                catch(InterruptedException ex)
                {}


               
            }
//
//

//
            try {
                process(position);
            } catch (InterruptedException ex) {
                Logger.getLogger(Barber.class.getName()).log(Level.SEVERE, null, ex);
            }
                 run();



        }
       else
       {
           queue.add(currentcust);

           try {
                System.out.println("barber is sleeping");
                process(position);
            } catch (InterruptedException ex) {
                Logger.getLogger(Barber.class.getName()).log(Level.SEVERE, null, ex);
            }

       }


   }

//    @SuppressWarnings("static-access")
    public synchronized void process(int position) throws InterruptedException
    {

       
        this.status=1;



        for(double i=0;i<999999999;i++)
            {


            }
      //  queue.remove(currentcust);
            this.status=0;
            notifyAll();
            System.out.println("the size of the queue is "+queue.size());
            System.out.println("the cusotmer in "+position+" has been served");
            System.out.println("the status of the barber is "+status);


    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.