Reputation: 21
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);
}
}
Upvotes: 1
Views: 1348
Reputation: 45596
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( );
Upvotes: 2
Reputation: 5507
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
Upvotes: 0