Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Data {
- private static int value = 0;
- public static synchronized int getValue() {
- return value;
- }
- public static synchronized void setValue(int value) {
- this.value = value;
- }
- }
- class Simulator extends Thread {
- private void waitIfNotAllowed() {
- if (Data.getValue() == 1) {
- Data.setValue(2);
- while (Data.getValue() != 0) {
- sleep(3000);
- }
- }
- }
- public void run() {
- try {
- while (true) {
- waitIfNotAllowed();
- doSomething();
- }
- } catch(Exception ex) {
- LOG.error(null, ex);
- }
- }
- }
- class Scheduler extends Thread {
- private void pauseThread() {
- Data.setValue(1);
- while (Data.getValue() != 2) {
- sleep(3000);
- }
- }
- private void continueThread() {
- Data.setValue(0);
- }
- public void run() {
- try {
- while (true) {
- doSomething();
- if (...) {
- pauseThread();
- doSpecialThing();
- continueThread();
- }
- doSomething();
- }
- } catch(Exception ex) {
- LOG.error(null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement