Click here to Skip to main content
15,846,899 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I am creating a program wherein I have 2 threads.
Thread A performs something and sends to Thread B the result.
Thread A waits for response from Thread B before processing another task.

How can I do this?
Can anyone give me a sample code?
thanks!
Posted

If you want to wait for the previous thread to finish, before you start the next, you add thread.join() in between:

for eg:-

C#
for(int i = 0; i < 10; i++) {
    thread = new Thread(this);
    thread.start();

    thread.join();    // Wait for it to finish.
}
 
Share this answer
 
You have to assign your thread to a variable and later call join() on this variable like:
Java
Thread t = new Thread() {
    public void run() {
        System.out.println("text");
        // other complex code
    }
 };
 t.start();
 t.join();

This way you will wait until the thread finishes and just then continue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900