1

I am currently working on modified version of Cigarette Smoker problem. Below you can find my agent class. What I need to do in order to have three threads instead of one? So there will be three outputs instead of one.

public class agent extends Thread {

    private table smokingtable;

    public agent(table pSmokingtable)
    {
        smokingtable = pSmokingtable;
    }

    @Override
    public void run()
    {
        while(true)
        {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {}
            smokingtable.setAgentElements();
            // this triggers the smoker-threads to look at the table
            output("The Agent puts " + smokingtable.getAgentElements() + table.");
            // pause the agent while one smoker thread is running
        }
    }


    public synchronized void wake()
    {
        try
        {
            notify();
        } catch(Exception e){}
    }


    public synchronized void pause()
    {
        try
        {
            this.wait();
        } catch (Exception e) {}
    }

    private void output(String pOutput)
    {
        System.out.println(pOutput);
    }
}

I have done something like this but surely this is wrong.

public class agent extends Thread {

    private table smokingtable;

    public agent(table pSmokingtable)
    {
        smokingtable = pSmokingtable;
    }

    @Override
    public void run()
    {
        while(true)
        {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {}
            smokingtable.setAgent1Elements();

            output("The Agent 1 puts " + smokingtable.getAgent1Elements());

            smokingtable.setAgent2Elements();
            output("The Agent 2 puts " + smokingtable.getAgent2Elements());

            smokingtable.setAgent3Elements();
            output("The Agent 3 puts " + smokingtable.getAgent3Elements());
            pause();
        }
    }


    public synchronized void wake()
    {
        try
        {
            notify();
        } catch(Exception e){}
    }


    public synchronized void pause()
    {
        try
        {
            this.wait();
        } catch (Exception e) {}
    }

    private void output(String pOutput)
    {
        System.out.println(pOutput);
    }
}
9
  • 1. Do not extend Thread, implement Runnable. 2. Do not use wait/notify, but one of the java.util.concurrency synchronization objects Commented Dec 13, 2011 at 18:58
  • Thanks a lot. Anything else I need to be aware of? Commented Dec 13, 2011 at 19:00
  • What are you trying to achieve exactly ? (I don't know this Smokers problem) Commented Dec 13, 2011 at 19:06
  • he cigarette smoker’s problem is a well known concurrency problem, originally described in 1971 by S. S. Patil. Assume there are three compulsive smokers around a table, each of whom has an infinite supply one of the three necessary ingredients – one smoker has an infinite supply of tobacco, another has an infinite supply of paper, and the third has an infinite supply of matches. Assume there is also a non-smoking agent with an infinite supply of all three ingredients. The agent selects two of the ingredients, and places them on table. Commented Dec 13, 2011 at 19:09
  • This allows one of the smokers to obtain their supply and smoke cigarette for a while. Meanwhile, the agent, seeing the table empty, again chooses two ingredients at random and places them on the table. This process continues forever. Commented Dec 13, 2011 at 19:09

2 Answers 2

2

In order to have 3 threads instead of 1 you need to create 3 threads and start them.

In your case, the simplest approach is this:

Thread agent1 = new agent( );
Thread agent2 = new agent( );
Thread agent3 = new agent( );

agent1.start( );
agent2.start( );
agent3.start( );

agent1.join( );
agent2.join( );
agent3.join( );

Better way of doing things would be to use ExecutorService framework, e.g. ThreadPoolExecutor.

ExecutorService pool = Executors.newFixedThreadPool( 3 );

for ( int i = 0; i < 3; ++i )
{
    pool.execute( new agent( ) );
}

// This will wait for your agents to execute
pool.shutdown( );
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe I completely misunderstood your question, but it looks like you need to review again the basics of working with treads in java. this would be a good place to start

In the second example it looks like you are trying to run all three agents from the same thread, and i guess this is not what you intended to do.

In the first code extract you gave, add an agent id as field and to the agent's constructor, and append this Id to the output message. now all you need to do is to create three agent instances from somewhere (probably your main method) and call their run method from there.

public static void main(String[] args) {
for(int i = 0; i < 3; i++)
  new agent(i).start();
}

have a look at this simple example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.