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

  • Home
  • SEARCH
  • 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 7488099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:44:19+00:00 2026-05-29T14:44:19+00:00

I am writing a multithreaded program in which i am getting exception java.lang.IllegalThreadStateException .

  • 0

I am writing a multithreaded program in which i am getting exception java.lang.IllegalThreadStateException.

Any help would be welcomed

here is my stack trace

Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at GeoMain.main(GeoMain.java:18)

here is my code for main class

    public class TMain {

    public static void main(String[] args) {

        String Batchid="1,2,3";
        String batch[]=StringUtils.split(Batchid,",");

        MultiThread gt=new MultiThread();
        for(int i=0;i<batch.length;i++){
            gt.setBatch(batch[i]);                  
            gt.start();
            System.out.println("Thread started for "+batch[i]);
        }

        System.out.println("mainfinish");

    }

}

and hereis my multi thread class

public class MultiThread extends Thread {

    private static Queue<String> queue = new LinkedList<String>();
    private static Boolean isInUse = false;

    private void runcoder()
    {
        String batchid=null;
        BatchIdCreator bid=null;
        while(isInUse)
        {
            try {
                Thread.sleep(60000);
            } catch (InterruptedException e) {
                System.out.println("exception");
                e.printStackTrace();
            }
        }
        isInUse=true;

        synchronized(isInUse)
        {

            isInUse=true;
            batchid=queue.poll();
            System.out.println(batchid);
            System.out.println(batchid);
            bid=new BatchIdCreator(batchid);
// get a list from database
            bid.getList();
// print on console
            bid.printList();
            isInUse=false;
        }
    }

    @Override
    public void run() {
        runcoder();
    }

    public void setBatch(String batchid)
    {
        queue.add(batchid);
    }

    public static Boolean getIsInUse() {
        return isInUse;
    }


}
  • 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-05-29T14:44:21+00:00Added an answer on May 29, 2026 at 2:44 pm

    In this snippet:

    MultiThread gt=new MultiThread();
    for(int i=0;i<batch.length;i++){
        gt.setBatch(batch[i]);                  
    
        gt.start();              <--- Same thread object as in previous iteration
    
        System.out.println("Thread started for "+batch[i]);
    }
    

    you’re calling start() over and over again on the same thread. As described in the documentation, this is illegal:

    It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

    You may want to move the new MultiThread() into the loop to avoid this:

                                         ----------.
    for(int i=0;i<batch.length;i++){               |
                                                   |           
        MultiThread gt=new MultiThread();       <--'
    
        gt.setBatch(batch[i]);                  
        gt.start();
        System.out.println("Thread started for "+batch[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a multithreaded program, which would crash when a particular exception was thrown.
I'm writing a Java program involving a multithreaded worker pool of Process es. Each
I am writing a multithreaded program in java. I have written something like this
I'm writing a Java program which uses a lot of CPU because of the
All, I am writing a multithread program. The class which extends Thread has a
I am writing a multithreaded program where I want to handle a possible Ctrl-C
So, I've got a multithreaded python program, which is currently suffering from deadlock. I
I am writing a multithreaded application and would like to pass around pointers to
In a multi-threaded program I'm writing a custom print function which accepts a variable
I'm profiling a multithreaded program running with different numbers of allowed threads. Here are

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.