How to find out which thread completes first from two threads

I have two threads A and B. If A ends first, then I need to execute function1 else, if B ends first, I need to execute function 2. HOw do I know which of the two threads finished first.

+3
source share
3 answers

You can use the following, which will only be set if the previous value was zero. (This can be used even if you have only one thread to ensure that the value does not change after installation)

AtomicReference<ValueType> ref = new AtomicReference<ValueType>();

ref.compareAndSet(null, value);
+2
source
import java.util.concurrent.Semaphore;

public class ThreadTest {
    private Semaphore semaphore = new Semaphore(0);
    private String winner;

    private synchronized void finished(String threadName) {
        if (winner == null) {
            winner = threadName;
        }
        semaphore.release();
    }

    public void run() {
        Runnable r1 = new Runnable() {
            public void run() {
                try {
                    Thread.sleep((long) (5000 * Math.random()));
                }
                catch (InterruptedException e) {
                    // ignore
                }
                finally {
                    finished("thread 1");
                }
            }
        };

        Runnable r2 = new Runnable() {
            public void run() {
                try {
                    Thread.sleep((long) (5000 * Math.random()));
                }
                catch (InterruptedException e) {
                    // ignore
                }
                finally {
                    finished("thread 2");
                }
            }
        };

        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
        try {
            semaphore.acquire();
            System.out.println("The winner is " + winner);
        }
        catch (InterruptedException e) {
            System.out.println("No winner");
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args) {
        new ThreadTest().run();
    }
}

, , , , , .

EDIT:

Aa, jtahlborn, CountDownLatch . :

import java.util.concurrent.CountDownLatch;

public class ThreadTest {
    private CountDownLatch latch = new CountDownLatch(1);
    private String winner;

    private synchronized void finished(String threadName) {
        if (winner == null) {
            winner = threadName;
        }
        latch.countDown();
    }

    public void run() {
        Runnable r1 = new Runnable() {
            public void run() {
                try {
                    Thread.sleep((long) (5000 * Math.random()));
                }
                catch (InterruptedException e) {
                    // ignore
                }
                finally {
                    finished("thread 1");
                }
            }
        };

        Runnable r2 = new Runnable() {
            public void run() {
                try {
                    Thread.sleep((long) (5000 * Math.random()));
                }
                catch (InterruptedException e) {
                    // ignore
                }
                finally {
                    finished("thread 2");
                }
            }
        };

        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
        try {
            latch.await();
            System.out.println("The winner is " + winner);
        }
        catch (InterruptedException e) {
            System.out.println("No winner");
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args) {
        new ThreadTest().run();
    }
}

, 2 1.

+2

() .

import java.util.concurrent.atomic.AtomicInteger;

final AtomicInteger winner = new AtomicInteger(0);

run()

winner.compareAndSet(0, 1);

winner.compareAndSet(0, 2);

, , compareAndSet.

winner.get()

In production code, I would suggest using some constants for the initial value and stream indices.

+2
source

Source: https://habr.com/ru/post/1792768/


All Articles