Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:20:23+00:00 2026-06-12T18:20:23+00:00

I’m trying to make a multithread program that acts as a furniture factory… For

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:20:24+00:00Added an answer on June 12, 2026 at 6:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.