Created
March 11, 2013 15:24
-
-
Save kickroot/5135001 to your computer and use it in GitHub Desktop.
Thread Interleaving example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interleavedexample; | |
import java.util.concurrent.Exchanger; | |
import java.util.logging.Logger; | |
/** | |
* | |
*/ | |
public class InterleavedRunnable implements Runnable { | |
///////////////////////////// Class Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
////////////////////////////// Class Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
//////////////////////////////// Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
private final String name; | |
private final Exchanger<Boolean> exchanger; | |
private Boolean state; | |
/////////////////////////////// Constructors \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
public InterleavedRunnable(String name, Exchanger<Boolean> exchanger, Boolean state) { | |
this.name = name; | |
this.exchanger = exchanger; | |
this.state = state; | |
} | |
////////////////////////////////// Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
//------------------------ Implements: Runnable | |
@Override | |
public void run() { | |
try { | |
while (true) { | |
if (state) { | |
Logger.getLogger(getClass().getName()).info(name + " is running"); | |
} | |
state = exchanger.exchange(state); | |
} | |
} catch (InterruptedException ex) { | |
Logger.getLogger(name).info("Interrupted"); | |
} | |
} | |
//------------------------ Overrides: | |
//---------------------------- Abstract Methods ----------------------------- | |
//---------------------------- Utility Methods ------------------------------ | |
//---------------------------- Property Methods ----------------------------- | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interleavedexample; | |
import java.util.concurrent.Exchanger; | |
/** | |
* | |
*/ | |
public class Main { | |
///////////////////////////// Class Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
////////////////////////////// Class Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
Exchanger<Boolean> exchanger = new Exchanger<Boolean>(); | |
Thread thread1 = new Thread(new InterleavedRunnable("Thread 1", exchanger, true)); | |
Thread thread2 = new Thread(new InterleavedRunnable("Thread 2", exchanger, false)); | |
thread1.start(); | |
thread2.start(); | |
} | |
//////////////////////////////// Attributes \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
/////////////////////////////// Constructors \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
////////////////////////////////// Methods \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ | |
//------------------------ Implements: | |
//------------------------ Overrides: | |
//---------------------------- Abstract Methods ----------------------------- | |
//---------------------------- Utility Methods ------------------------------ | |
//---------------------------- Property Methods ----------------------------- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment