开发者

Thread Synchronization - How to execute threads alternatively

I have been trying to solve a problem involving thread communication using wait() and notify(). Basically i have 2 threads T1 and T2 and i want them to be executed in the following order

T1 , T2, T1, T2 ..... How can i achieve that?

Actual Problem: There are 2 threads 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 ,开发者_Go百科 5 , .... 100


You describe a Producer-Consumer pattern.

It's java implementations described in numerous java books including M.Grand "Patterns in Java. Volume I" and "Java 2: The Complete Reference" by Naughton and Schildt.

Basic idea: both threads should use 1 monitor (i.e. their code should be inside synchronized(monitor) {} blocks). You also need some flag variable which should indicate which of two threads should work at the moment.

When one of your threads is inside synchronized block it should check flag variable whether it's his turn to do the job. If yes, let it work and then change flag value and then notify all waiting threads. If no, then it should wait.


Look at the java.util.concurrent package, specifically the Exchanger


You're trying to parallelize a multistep process right? If so, see my answer here for an approach and some working code to do that. The answer involves an ExecutorService (or two) and one or more work queues.

For this approach, your processing needs to be able to fit into a Runnable, along with intermediate state information for the processing. You feed each step to the ExecutorService as a Runnable, which will add a second Runnable to perform the next step. This maintains the order of execution, but lets you effectively run as many threads as you wish in parallel.

:EDIT:

As another has suggested, the Exchanger library class can be used for this if you explicitly want to limit processing to 2 threads. I prefer the above approach because it maintains order of execution and allows you to use the modern 4-core (and 8-core) systems fully. It should also reduce synchronization a bit.


If T1 and T2 are 2 different implementations of the Runnable interface, with T1 being a thread that prints just odd numbers (1,3,...) and T2 being one that prints even number (1,2.....), this can be done by using the wait() and notify() methods on a shared monitor. The important thing is for each thread to check for a shared flag before printing its value. The below code works;

//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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜