Abhij
Abhij

Reputation: 1272

IllgalThreadStateException

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;
    }


}

Upvotes: 4

Views: 2974

Answers (3)

Durandal
Durandal

Reputation: 20059

You are attempting to start the same (Multi)Thread instance multiple times. Create a new instance of Multithread inside the loop, so each thread gets its own instance.

Upvotes: 1

AlexR
AlexR

Reputation: 115328

You cannot start the same thread twice. You you want to create several threads move the creation of thread instance into the loop:

    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]);
    }

Upvotes: 1

aioobe
aioobe

Reputation: 420971

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]);
}

Upvotes: 3

Related Questions