SW Jeong
SW Jeong

Reputation: 168

Java Thread, how can I make one thread always stop or end first?

I want to make a thread B end later so that the result always show B9 at the end. How can I do this? Here is my code.

package thread;
import java.lang.*;
import java.lang.Thread;
import java.lang.Runnable;

class Numprintt implements Runnable {
    String myName;
    public Numprintt(String name) {
        myName = name;
    }
    public void run() {
        for(int i = 0; i < 10; i++) {
            System.out.print(myName + i + " ");
        }
    }
}


public class MyRunnableTest {
    public static void main(String[] ar) {

        Numprintt A = new Numprintt("A");
        Numprintt B = new Numprintt("B");

        Thread t1 = new Thread(A);
        Thread t2 = new Thread(B);

        t1.start();
        t2.start();

        try {
            t1.join(); t2.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

My results may show A1, A2, B1, .... , B9 or, B1, B2, A1, A2, ..., A9 I want the result to be always show like the former one.

Upvotes: 1

Views: 80

Answers (2)

Shiksha Verma
Shiksha Verma

Reputation: 130

public class MyRunnableTest {
    public static void main(String[] ar) {

    Numprintt A = new Numprintt("A");
    Numprintt B = new Numprintt("B");

    Thread t1 = new Thread(A);
    Thread t2 = new Thread(B);

    t1.start();


    try {
        t1.join(); 
        t2.start();
        t2.join();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

This will guarantee that thread A will end first

Upvotes: 0

Sudheera
Sudheera

Reputation: 1947

You can leverage the functionality of a CountDownLatch. A CountDownLatch gives you the ability of waiting for a count down happen from some other thread to the same CountDownLatch object. Here's a basic solution for your problem.

import java.lang.*;
import java.lang.Thread;
import java.lang.Runnable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReentrantLock;

class Numprintt implements Runnable {
    private final CountDownLatch latch;
    private String myName;

    public Numprintt(String name, CountDownLatch latch) {
        this.myName = name;
        this.latch = latch;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            if (i == 9 && latch != null) {
                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.print(myName + i + " ");
        }
    }
}


public class MyRunnableTest {

    public static void main(String[] ar) {

        CountDownLatch latch = new CountDownLatch(1);

        Numprintt A = new Numprintt("A", null);
        Numprintt B = new Numprintt("B", latch);


        Thread t1 = new Thread(A);
        Thread t2 = new Thread(B);

        t1.start();
        t2.start();

        try {
            t1.join();
            latch.countDown();
            t2.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 3

Related Questions