简体   繁体   中英

How to run three threads simultaneously with different sleep time in a while loop?

I want to run three different threads simultaneously in a while loop with different wait times. Here is my sample code.

static void someFunction(){

    while(true){

        Thread t1 = new Thread(){
            public void run(){
                System.out.println("Thread 1");
                try {
                    Thread.currentThread();
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                System.out.println("Thread 2");
                try {
                    Thread.currentThread();
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };

        Thread t3 = new Thread(){
            public void run(){
                System.out.println("Thread 3");
                try {
                    Thread.currentThread();
                    Thread.sleep(8000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };


    }

}

Is it possible to run three threads in a while loop simultaneously? Any other ways to implement this?

Expected output : Thread1 Thread2 Thread3 Thread1(After 5 SEC) Thread2(After 7 sec) Thread3(After 8 sec)

You can't control execution in a thread from outside the thread's run() method (or code that its run() method calls). The outer loop is not the way to do what you want. It's just going to endlessly generate new threads.

Since the threads are identical except for the message and the delay, this is a perfect situation to make those data into variables and have a single piece of code:

public class MyThread extends Thread {
    private final long interval;
    private final String message;
    public MyThread(long interval, String message) {
        this.interval = interval;
        this.message = message;
    }

    @Override
    public void run() {
        while (!isInterrupted()) {
            System.out.println(message);
            try {
                sleep(interval);
            } catch (InterruptedException e) {
                return;
            }
        }
    }
}

Note that I've replaced while(true) with while(!isInterrupted()) . This is to give you a way to terminate your threads in an orderly manner: just interrupt them.

The way you would use this might be:

static Thread t1, t2, t3;

static void someFunction() {
    t1 = new MyThread(5000, "Thread 1");
    t2 = new MyThread(7000, "Thread 2");
    t3 = new MyThread(8000, "Thread 3");
    // now start them all
    t1.start();
    t2.start();
    t3.start();
}

When you want the threads to end, just call:

t1.interrupt();
t2.interrupt();
t3.interrupt();

You might also want to look into using a ThreadGroup . It's a convenient way to handle a collection of threads as (surprise!) a single group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM