5 Replies - 912 Views - Last Post: 03 March 2011 - 09:04 AM Rate Topic: -----

#1 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Deadlock Threads, How to prevent

Posted 03 March 2011 - 01:06 AM

I finally did my program to have 1 Check makes 3 Ingredients and 3 Customers take 2 ingredients. However when it runs there is obviously deadlocking. What do I need to add to make the Threads timeout after they take an item so the next thread can get an item...etc... My code below...

/** Chef Thread that Puts each ingradient into the Queue */

public class Chef extends Thread
{
    private Ingredients ingredient;
    private int number;

    public Chef(Ingredients c, int number)
    {
        ingredient = c;
        this.number = number;
    }

    public void run()
    {
		int i = 0;
		int j = 0;
		int k = 0;

		while(true)
		{
			// Gerates a random number to be placed into the ingredients
			i = (i + (int)(Math.random() * 100));
			j = (j + (int)(Math.random() + 5));
			k = (k + (int)(Math.random() * 25));

			// Puts the number into the ingredient
			ingredient.putA(i);
			ingredient.putB(j);
			ingredient.putB(k);

			System.out.println("Chef #" + this.number + " puts: " + i + " Donut Shells, " + j + " Sprinkles and " + k + " Filling.");
			try
			{
			   Thread.sleep(1000);
            } catch (InterruptedException e) { }
		}

        /*
        for (int i = 0; i < 10; i++)
        {
            cubbyhole.putA(i);
            System.out.println("Producer #" + this.number + " put: " + i);
            try
            {
               sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
        */


    }
}


/** Clone Thead that pulls each ingredient */

public class Clone extends Thread
{
    private Ingredients ingredient;
    private int number;

    public Clone(Ingredients c, int number)
    {
        ingredient = c;
        this.number = number;
    }

    public void run()
    {
        int valueA = 0;
        int valueB = 0;
        int valueC = 0;

        //for (int i = 0; i < 10; i++)
        while(true)
        {
			if(this.number == 1)
			{
				valueA = ingredient.getA();
				valueB = ingredient.getB();
				System.out.println("Clone #" + this.number + " got: " + valueA + " Donut Shells and " + valueB + " Sprinkles");
			}
			else if(this.number == 2)
			{
				valueA = ingredient.getA();
				valueC = ingredient.getC();
				System.out.println("Clone #" + this.number + " got: " + valueA + " Donut Shells and " + valueC + " Filling");
			}
			else if(this.number == 3)
			{
				valueB = ingredient.getB();
				valueC = ingredient.getC();
				System.out.println("Clone #" + this.number + " got: " + valueB + " Donut Shells and " + valueC + " Filling");;
			}
			try
			{
				sleep(1000);
			}catch (InterruptedException e) { }
			/*
			switch(this.number)
			{
				case 1: valueA = ingredient.getA();
						valueB = ingredient.getB();
						System.out.println("Clone #" + this.number + " got: " + valueA + " Donut Shells and " + valueB + " Sprinkles");
						break;
				case 2: valueA = ingredient.getA();
						valueC = ingredient.getC();
						System.out.println("Clone #" + this.number + " got: " + valueA + " Donut Shells and " + valueC + " Filling");
						break;
				case 3: valueB = ingredient.getB();
						valueC = ingredient.getC();
						System.out.println("Clone #" + this.number + " got: " + valueB + " Donut Shells and " + valueC + " Filling");
						break;
			}
			*/
        }
    }

}


class Ingredients
{
    private int donut_shell;
    private int sprinkles;
    private int filling;

    private boolean availableA = false;
    private boolean availableB = false;
    private boolean availableC = false;

// Synchronize the available donut shells
    public synchronized int getA()
    {
        while (availableA == false)
        {
            try
            {
                wait();
            } catch (InterruptedException e) { }
        }
        availableA = false;
        notifyAll();
        return donut_shell;
    }
// Synchronize the available sprinkles
    public synchronized int getB()
    {
		while (availableB == false)
		{
			try
			{
				wait();
			}catch (InterruptedException e) { }
		}
		availableB = false;
		notifyAll();
        return sprinkles;

	}
// Synchronize the available  filling
    public synchronized int getC()
    {
		while (availableC == false)
		{
			try
			{
				wait();
			}catch (InterruptedException e) { }
		}
		availableC = false;
		notifyAll();
        return filling;

	}
    public synchronized void putA(int valueA)
    {
        while (availableA == true)
        {
            try
            {
                wait();
            } catch (InterruptedException e) { }
        }
        donut_shell = valueA;
        availableA = true;
        notifyAll();
    }
    public synchronized void putB(int valueB)
    {
		while (availableB == true)
		{
			try
			{
				wait();
			} catch (InterruptedException e) { }
		}
		sprinkles = valueB;
		availableB = true;
        notifyAll();
	}
	public synchronized void putC(int valueC)
	{
		while (availableC == true)
		{
			try
			{
				wait();
			} catch (InterruptedException e) { }
		}
		filling = valueC;
		availableC = true;
		notifyAll();
}
}



// Main - Contains the Threads to start

public class HomerClone
{
    public static void main(String[] args)
    {
        Ingredients c = new Ingredients();

        Chef p1 = new Chef(c, 1);

        Clone c1 = new Clone(c, 1);
        Clone c2 = new Clone(c, 2);
        Clone c3 = new Clone(c, 3);

        p1.start();
        c1.start();
        c2.start();
        c3.start();

    }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Deadlock Threads, How to prevent

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Deadlock Threads, How to prevent

Posted 03 March 2011 - 02:57 AM

Your Ingredients class guarantees a deadlock. See my comments in your code.


// synchronized obtains a lock on the object. No other thread can call a synchronized method on this object while the lock is in place.
    public synchronized void putA(int valueA)
    {
        // You are waiting for this to become false, but since no other method can can be called on this object, that will never be the case. You therefore have an infinite loop here.
        while (availableA == true)
        {
            try
            {
                wait();
            } catch (InterruptedException e) { }
        }
        donut_shell = valueA;
        availableA = true;
        notifyAll();
        // Only now at the end of the method is the lock released.
    }


One way round this it to have the putA() method return a boolean. True if the put succeeded, false it it did not. Have the method caller keep trying until the put is successful. You will need to do something similar for your other methods.

Hope that helps!
Was This Post Helpful? 1
  • +
  • -

#3 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Deadlock Threads, How to prevent

Posted 03 March 2011 - 07:45 AM

So you think I should try an If, else statement?
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Deadlock Threads, How to prevent

Posted 03 March 2011 - 07:52 AM

No. Did you not like my suggestion?

Edit:

Actually yes. :) Use an if..else to see if it is possible to add an ingredient. Sorry, I misunderstood you.

This post has been edited by cfoley: 03 March 2011 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Deadlock Threads, How to prevent

Posted 03 March 2011 - 08:24 AM

Do you mind testing it for me, I tried and it didn't. Maybe I have 2 many boolean values in each class..
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1949
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: Deadlock Threads, How to prevent

Posted 03 March 2011 - 09:04 AM

Post up what you've got now and let's see what's not working. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1