11111111188u9u98
11111111188u9u98

Reputation: 1

How to reuse a thread multiple times?

I have this Pseducode:

  1. Get user input until he/she enters "0"
  2. Call a method encode(input) with that input
  3. encode method is called by this code:

Code:

encrypt eddd = new encrypt();
while (!answer.equals("0")) {
    System.out.println("Enter String ,or enter 0");
    e.text = input.next();
    System.out.println("Please wait...");
    e.t.start();
}

Where t is t = new Thread(this, "Encode");, the problem is that once a user gives the input, e can't accept more inputs. I want that a user may enter 20 inputs in 30 seconds, encode takes 30 seconds to return output, So i want that user can enter 30 inputs and they will process in parallel threads.

  1. It gives output
  2. This continues till 0 is entered Like: Input

    Apple Cat Dog

output (as encodes return , time by time)

OUTPUT1
OUTPUT2
OUTPUT3

Error: Exception in thread "main" java.lang.IllegalThreadStateException

EDIT: i use this class encrypt implements Runnable

Upvotes: 0

Views: 909

Answers (4)

Eranda
Eranda

Reputation: 1467

Use the same thread won't work here, if you think so better not to use a different thread but execute the encrypt method in the current thread. That is because here the program may start the thread again before that threat finish its work, which is why you see such errors. Best approach is using a thread pool. Where if no thread is available to run (all the threads are used) you the program will wait for the next thread to be available. Simply you can use ExecutorService to create a thread pool. See the code below. If you need to use single thread then use Executors.newSingleThreadExecutor().

        ExecutorService executorService = Executors.newFixedThreadPool(10);
//      ExecutorService executorService = Executors.newSingleThreadExecutor();


        while (!answer.equals("0")) {
            System.out.println("Enter String ,or enter 0");
            e.text = input.next();
            System.out.println("Please wait...");
            Encrypt e = new Encrypt();
            executorService.submit(e);
        }

        executorService.shutdown();
        executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

Upvotes: 1

11111111188u9u98
11111111188u9u98

Reputation: 1

This worked for me:

String answer="";
                encrypt e=new encrypt();


                while (!answer.equals("0")){
                    System.out.println("Enter String to Encode , else enter 0");
                    System.out.println("Please wait...");
                    e.t=new Thread(new encrypt(input.next()));
                    e.t.start();
                }

Encrypt Constructor:

encrypt(String en) {
        t = new Thread(this, "Thread_Encode");
        this.text = en; // text to be encoded

    }

Upvotes: 0

Ozgen
Ozgen

Reputation: 1092

You try to start an already started Thread which gives IllegalThreadStateException. Make a new instance of Thread for different threads. For example :

class ClassA extends Thread{

  public void run(){
  }

}

class ClassB{

  public static void main(String arg[]){
     List<Thread> threadList=new ArrayList<Thread>();
     for (int i=0;i<10;i++){
        threadList.add(new ClassA());
     }
     for (int i=0;i<10;i++){
        threadList.get(i).start();             
     }
  }
}

Upvotes: 0

Neeraj Jain
Neeraj Jain

Reputation: 7720

You are starting the same thread each time

while (!answer.equals("0")) {
    System.out.println("Enter String ,or enter 0");
    e.text = input.next();
    System.out.println("Please wait...");
    e.t.start(); ------> Same thread instance
}

Instead try to create a new thread

while (!answer.equals("0")) {
    System.out.println("Enter String ,or enter 0");
    e.text = input.next();
    System.out.println("Please wait...");
    e.t=new MyCustomThread(); ----> You can instantiate your own thread
     e.t.start();------> Same thread instance
}

Upvotes: 0

Related Questions