Pablo Glez
Pablo Glez

Reputation: 306

How to make an infinite Thread instances loop?

I would like to create threads to manipulate them (run, wait, stop). I know how to do it making an instance one by one

Class that implements runnable

public class ThreadRunner implements Runnable{ .....

Class where I create the objects

ThreadRunner thread1 = new ThreadRunner ("thread1");
ThreadRunner thread2 = new ThreadRunner ("thread2"); 
.
.
.
thread1.start()
.
.

I would like to have a loop that only ends when the user wants to (maybe with a boolean Flag). How can I create the loop that instances a Thread dynamically every 10 seconds?

Upvotes: 1

Views: 898

Answers (1)

deyur
deyur

Reputation: 567

boolean stop = false;
List<Thread> threads = new ArrayList<Thread>();
while (!stop) {
    Thread t = new ThreadRunner("Thread " + threads.size());
    t.start();
    threads.add(t);
    // Do something here to check whether 'stop' should be updated.
    // And wait for 10 seconds here
} 

Upvotes: 0

Related Questions