0

I'm trying to make a multithread program that acts as a furniture factory... For example, it needs to have 3 nails, 2 pieces of wood and 1 piece of glue to make 1 table. I made the glue, wood and nail classes (as extensions of Thread class)

Now, the way I made it, it has no compiling errors, but when it runs, I get an IlegalThreadState exception at the thread.start(). Here's my Main()

public class Main 
{
public static int mueble = 0; 
public static void main(String[] args) throws InterruptedException 
{
    Semaphore smain = new Semaphore(1);
    Madera m = new Madera();
    Formica f = new Formica();
    Clavo c = new Clavo();
    boolean b = true;
    while(b)
    {
        Random rand = new Random();
        int r = (int)(rand.nextDouble()*6+0);
        if(r < 1)
        {
            smain.acquire();
            f.start();
        }
        else if ((r>1)&&(r<3))
        {
            smain.acquire();
            m.start();
        }
        else 
        {
            smain.acquire();
            c.start();
        }
        if (mueble == 30)
        {
            b = false;
        }
        smain.release();
    }

}
}

Don't mind the spanish. I think the problem might be in the run() section of each thread, here it is (it's pretty much the same for the three classes)

@Override
public void run()
{
    try {
        sclavo.acquire();
        System.out.println("Llega 1 clavo");
        System.out.println("Hay " + (3-sclavo.availablePermits()) + " clavo(s)");
        if((sclavo.availablePermits()==0)&&(Madera.smadera.availablePermits()==0)&&(Formica.sformica.availablePermits()==0))
                {
                    System.out.println("Se ha creado 1 mueble");
                    Main.mueble++;
                    System.out.println("Hay" + Main.mueble + "mueble(s)");
                    Formica.sformica.release(1);
                    sclavo.release(3);
                    Madera.smadera.release(2);
                }
    } catch (InterruptedException ex) {
        Logger.getLogger(Clavo.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Any help would be great!

4
  • Presumably the thread is not startable because it's already running/been run. Try issuing a getState() call (and printing result) before each start. Commented Oct 12, 2012 at 18:21
  • Make sure you mention that this is a homework assignment. Commented Oct 12, 2012 at 18:21
  • In case anyone is wondering: clavo = nail, madera = wood, mueble = (piece of) furniture Commented Oct 12, 2012 at 18:27
  • All three of you are correct. Using the answer below I was able to get it running. Commented Oct 12, 2012 at 19:12

1 Answer 1

2

You can't restart a thread. Look at the documentation for Thread.start. It says:

It is never legal to start a thread more than once.

You're starting the same threads over and over in a loop. Instead, you should look at how ExecutorService works and use that instead. A good multi-threaded system is job-based, and each job is either a Runnable or a Callable. you use Runnable when you just want to run some task and it doesn't have any output (specifically, if the job essentially returns void). You use Callable when your job does return something. There's plenty of examples here on SO and elsewhere for how to use ExecutorService and related classes.

If you have no choice but to use threads manually, then you need to create and start a new thread on each loop, such as:

new Formica().start();

As opposed to starting the same Formica instance over and over.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, I see... I'll make some changes and post results... Rookie mistake I suppose
Ok, I've made some changes and the program is running smoothly. The only problem is that after it's reached the 30 tables it's supposed to make, some threads are still receiving materials (and saying so using their sys out) although no more furniture is created (this is good). Is this just because the threads hadn't finished their routine? Also, even after the 30 tables have been made and everything has stopped, the program is still running (but apparently doing nothing) is there something I'm missing, like flushing the memory or anything?
Nevermind that, I found what I was looking for (System.exit())

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.