Take the 2-minute tour ×

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;
        Random rand = new Random();
        int r = (int)(rand.nextDouble()*6+0);
        if(r < 1)
        else if ((r>1)&&(r<3))
        if (mueble == 30)
            b = false;


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)

public void run()
    try {
        System.out.println("Llega 1 clavo");
        System.out.println("Hay " + (3-sclavo.availablePermits()) + " clavo(s)");
                    System.out.println("Se ha creado 1 mueble");
                    System.out.println("Hay" + Main.mueble + "mueble(s)");
    } catch (InterruptedException ex) {
        Logger.getLogger(Clavo.class.getName()).log(Level.SEVERE, null, ex);

Any help would be great!

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

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
Ah, I see... I'll make some changes and post results... Rookie mistake I suppose –  iVikD Oct 12 '12 at 18:32
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? –  iVikD Oct 12 '12 at 18:48
Nevermind that, I found what I was looking for (System.exit()) –  iVikD Oct 12 '12 at 19:02
add comment

Your Answer


