Thread Synchronization - How to Run Threads Alternatively

I am trying to solve the problem of thread linking with wait () and notify (). Basically, I have 2 threads T1 and T2, and I want them to execute in the following order.

T1, T2, T1, T2 ..... How can I achieve this?

Actual problem: There are 2 streams T1 - which prints odd numbers (say, 1 - 100) and T2 - which prints even numbers (1 - 100). Now the output should be 1, 2, 3, 4, 5, ... 100

+3
source share
4 answers

You are describing a Producer-Consumer sample.

java-, java-, M.Grand " Java. I" "Java 2: " Naughton and Schildt.

: 1 (.. synchronized(monitor) {}). , , .

, , , . , , , . , .

+2

java.util.concurrent, Exchanger

+2

? , . . ExecutorService ( ) .

Runnable . ExecutorService ExecutorService, Runnable .. , , .

: EDIT:

, Exchanger, . , 4- ( 8-) . .

+1

T1 T2 Runnable, T1 , (1,3,...) T2, , (1,2.....), , wait() notify() . , . ;

//The shared monitor
public class Mutex {
public static boolean oddFlag;

}

//The Thread that is supposed to print Odd numbers (assuming an upper limit of 99)
public class OddPrinter implements Runnable {
private Mutex mutex;

public OddPrinter(Mutex mutex) {
    this.mutex = mutex;
}

public synchronized void run() {
    System.out.println("Started Thread: OddPrinter");
    int i;
    for(i=1; i<100; i+=2 ) {
        synchronized (mutex) {
            while(!Mutex.oddFlag) {
                try {
                    mutex.wait();
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupted();
                }
            }

            if(Mutex.oddFlag == true) {
                System.out.println("Print from OddPrinter: "+i);
                Mutex.oddFlag = false;
                mutex.notify();
            }


        }
    }
    System.out.println("Finished Thread: OddPrinter: "+i);
}

}

//The Thread that is supposed to print Odd numbers (assuming an upper limit of 98)
public class EvenPrinter implements Runnable {
private Mutex mutex;

public EvenPrinter(Mutex mutex) {
    this.mutex = mutex;
}

public synchronized void run() {
    System.out.println("Started Thread: EvenPrinter");
    int i;
    for(i=2; i<100; i+=2) {
        synchronized (mutex) {
            while(Mutex.oddFlag) {
                try {
                    mutex.wait();
                } catch (InterruptedException ie) {
                    Thread.currentThread().interrupted();
                }
            }

            if(!(Mutex.oddFlag == true)) {
                System.out.println("Print from EvenPrinter: "+i);
                Mutex.oddFlag = true;
                mutex.notify();
            }

        }
    }
    System.out.println("Finished Thread: EvenPrinter: "+i);
}

}

//The test harness that executes the threads
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class NumberPrinterTest {

public static void main(String[] args) throws Exception{
    ExecutorService es = Executors.newFixedThreadPool(2);

    Mutex mutex = new Mutex();
    OddPrinter op = new OddPrinter(mutex);
    EvenPrinter ep = new EvenPrinter(mutex);
    Mutex.oddFlag = true;
    es.execute(op);
    es.execute(ep);

    if(null != es){
        es.shutdown();
        try {
            es.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupted();
        }
    }

}

}
0
source

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


All Articles