简体   繁体   中英

How to get one thread to wait for another thread?

I have two threads: C and D. If I want thread C to execute first, then thread D, that is what I must do:

This is code for class ThreadC :

public class ThreadC implements Runnable {

    int isi[] = new int[100];

    public void run () {
        for (int i = 0;i < isi.length; i++) {
            isi[i] = i;
            System.out.print(isi[i] + " ");
        }
    }
}

This is code for class ThreadD :

public class ThreadD implements Runnable {

    int temp=0;

    public void run () {
        for(int i = 0; i < 1000; i++)
            temp += i;
        System.out.println("nilai temp : " + temp);
    }
}

This is the main method:

public class main {
    public static void main (String[] args) {
        ThreadC tc = new ThreadC();
        Thread t1 = new Thread(tc);
        t1.start();
        ThreadD td = new ThreadD();
        Thread t2 = new Thread(td);
        t2.start();
    }
}

EDIT:

I actually have the problem, that I have class. First I call ThreadC. I want thread C is completed execute then thread under for will be executed. Then I call ThreadD. It is like the code below:

for (int j = 0; j < idW.length; j++) {
    webtext = d.getWebText(idW[j]); 
    ThreadPrepo tpo = new ThreadPrepo(webtext, host[j%jumhost], "server", 1099, idW[j]);  
    Thread t1 = new Thread(tpo);
    t1.start();

}
ThreadD td = new ThreadD;
Thread t2 = new Thread(t2);
t2.start();

So ThreadD t2 will executed after thread t1 complete executed, so Thread t2 must wait until t1 hash finished.

How can I solve that?

Use Thread.join() . Joining on a thread will wait for it to complete. If you want t2 to run after t1 , the following snippet will do that:

t1.start();
t1.join(); // blocks until t1 is complete
t2.start();

EDIT: To clarify, if you really only have two threads like this, do not do this - threads are not needed and the runnables can just be run sequentially. The join works well if you have multiple threads running concurrently and you need to wait for all of them to finish before running some other code (the code after the threads finish does not need to be run in a thread).

In addition to Jeff's answer, java.util.concurrent has lots of options.

CountDownLatch or Semaphore would achieve that. Using Runnables, you could use a threadPoolExecutor, with a thread pool size of one.

If you don't want them to run concurrently, it probably makes sense to only create one thread

For example, using a thread pool executor

ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(new ThreadC());
executor.execute(new ThreadD());

Don't use threads at all. Just run your Runnables in the desired order:

    ThreadC tc=new ThreadC();
    tc.run();
    ThreadD td=new ThreadD();
    td.run();

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