Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 8 同步通知,等待属性布尔对象Java,IllegalMonitorStateException_Java 8_Wait_Synchronized_Notify - Fatal编程技术网

Java 8 同步通知,等待属性布尔对象Java,IllegalMonitorStateException

Java 8 同步通知,等待属性布尔对象Java,IllegalMonitorStateException,java-8,wait,synchronized,notify,Java 8,Wait,Synchronized,Notify,我有这个密码 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public clas

我有这个密码

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class SyncProt0 {

  public static void main(String... args) {
    ExecutorService executorService = Executors.newCachedThreadPool();

    ProcessStep psWorker = new ProcessStep();
    ProcessStep psBoss = new ProcessStep();

    Worker worker = new Worker(executorService, psWorker, psBoss);
    Boss boss = new Boss(executorService, psBoss, psWorker);
  }

  public static class Worker implements Runnable {

    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      executorService.submit(this);
    }

    @Override
    public void run() {
      System.out.println("I'm -> Worker. I wait until Boss can see me!");
      while (!psBoss.isRunning()) {
        try {
          Thread.sleep(100);
        } catch (Exception e) { }
      }
      System.out.println("I'm -> Worker. Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        psWorker.setRunning(true);
        System.out.println("I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
        counter++;
      }
      psWorker.setRunning(false);
      psWorker.setFinished(true);
      System.out.println("I'm -> Worker. I left to work!");
    }

  }

  public static class Boss implements Runnable {
    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      executorService.submit(() -> {
        System.out.println("Boss. I have Runnable for turn of me when is needed");
        while (!this.psWorker.isFinished()) {
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) { }
        }
        this.running = false;
      });
      executorService.submit(this);
    }

    @Override
    public void run() {
      System.out.println("I'm -> Boss. I'm delayed 4 Secs");
      try {
        Thread.sleep(4000);
      } catch (Exception e) { }

      System.out.println("I'm -> Boss. Let's go to check the work!");
      psBoss.setRunning(true);
      running = true;
      while (running) {
        System.out.println("I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
      }
      psBoss.setRunning(false);
      psBoss.setFinished(true);
      System.out.println("I'm -> Boss The work end!");
    }

  }

  public static class ProcessStep {

    private final ReadWriteLock rwLock;
    private Boolean running;
    private Boolean finished;

    public ProcessStep() {
      this.rwLock = new ReentrantReadWriteLock();
      running = false;
      finished = false;
    }

    public Boolean isRunning() {
      Lock readLock = rwLock.readLock();
      readLock.lock();
      try {
        return running;
      } finally {
        readLock.unlock();
      }
    }

    public void setRunning(Boolean running) {
      Lock writeLock = rwLock.writeLock();
      writeLock.lock();
      try {
        this.running = running;
      } finally {
        writeLock.unlock();
      }
    }

    public Boolean isFinished() {
      Lock readLock = rwLock.readLock();
      readLock.lock();
      try {
        return finished;
      } finally {
        readLock.unlock();
      }
    }

    public void setFinished(Boolean finished) {
      Lock writeLock = rwLock.writeLock();
      writeLock.lock();
      try {
        this.finished = finished;
      } finally {
        writeLock.unlock();
      }
    }

  }

}
很好用

注意:我不想阻止所有的
过程步骤
只按需阻止某些属性

我想对更改此代码的属性使用
Wait
Notify
机制(在
Boss
类中):

以及此代码(在
工人
类中)

当我用此代码更改
工人
老板
类时:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class SyncProt1 {

  public static void main(String... args) {
    ExecutorService executorService = Executors.newCachedThreadPool();

    ProcessStep psWorker = new ProcessStep();
    ProcessStep psBoss = new ProcessStep();

    Worker worker = new Worker(executorService, psWorker, psBoss);
    Boss boss = new Boss(executorService, psBoss, psWorker);
  }

  public static class Worker implements Runnable {
    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      executorService.submit(this);

    }

    @Override
    public void run() {
      System.out.println("I'm -> Worker. I wait until Boss can see me!");
      synchronized (this.psBoss.isRunning()) {
        try {
          psBoss.isRunning().wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("I'm -> Worker. Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        synchronized (this.psWorker.isRunning()) {
          psWorker.setRunning(true);
          psWorker.isRunning().notify();
        }
        System.out.println("I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
        counter++;
      }
      synchronized (this.psWorker.isRunning()) {
        psWorker.setRunning(false);
        psWorker.isRunning().notify();
      }
      synchronized (this.psWorker.isFinished()) {
        psWorker.setFinished(true);
        psWorker.isFinished().notify();
      }
      System.out.println("I'm -> Worker. I left to work!");
    }

  }

  public static class Boss implements Runnable {
    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      executorService.submit(() -> {
        System.out.println("Boss. I have Runnable for turn of me when is needed");
        synchronized (this.psWorker.isFinished()) {
          try {
            psWorker.isFinished().wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        this.running = false;
      });
      executorService.submit(this);
    }

    @Override
    public void run() {
      System.out.println("I'm -> Boss. I'm delayed 4 Secs");
      try {
        Thread.sleep(4000);
      } catch (Exception e) { }

      System.out.println("I'm -> Boss. Let's go to check the work!");
      synchronized (this.psBoss.isRunning()) {
        psBoss.setRunning(true);
        psBoss.isRunning().notifyAll();
      }
      running = true;
      while (running) {
        System.out.println("I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
      }
      synchronized (this.psBoss.isRunning()) {
        psBoss.setRunning(false);
        psBoss.isRunning().notify();
      }
      synchronized (this.psBoss.isFinished()) {
        psBoss.setFinished(true);
        psBoss.isFinished().notify();
      }
      System.out.println("I'm -> Boss The work end!");
    }

  }

  public static class ProcessStep {

    private final ReadWriteLock rwLock;
    private Boolean running;
    private Boolean finished;

    public ProcessStep() {
      this.rwLock = new ReentrantReadWriteLock();
      running = false;
      finished = false;
    }

    public Boolean isRunning() {
      Lock readLock = rwLock.readLock();
      readLock.lock();
      try {
        return running;
      } finally {
        readLock.unlock();
      }
    }

    public void setRunning(Boolean running) {
      Lock writeLock = rwLock.writeLock();
      writeLock.lock();
      try {
        this.running = running;
      } finally {
        writeLock.unlock();
      }
    }

    public Boolean isFinished() {
      Lock readLock = rwLock.readLock();
      readLock.lock();
      try {
        return finished;
      } finally {
        readLock.unlock();
      }
    }

    public void setFinished(Boolean finished) {
      Lock writeLock = rwLock.writeLock();
      writeLock.lock();
      try {
        this.finished = finished;
      } finally {
        writeLock.unlock();
      }
    }

  }

}
工人仍在等待

可以通过属性使用同步吗?

另一个结果相同的版本是:

  public static class Worker implements Runnable {
    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      executorService.submit(this);

    }

    @Override
    public void run() {
      System.out.println("I'm -> Worker. I wait until Boss can see me!");
      synchronized (psBoss) {
        try {
          psBoss.isRunning().wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("I'm -> Worker. Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        synchronized (psWorker) {
          psWorker.setRunning(true);
          psWorker.isRunning().notify();
        }
        System.out.println("I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
        counter++;
      }
      synchronized (psWorker) {
        psWorker.setRunning(false);
        psWorker.isRunning().notify();
      }
      synchronized (psWorker) {
        psWorker.setFinished(true);
        psWorker.isFinished().notify();
      }
      System.out.println("I'm -> Worker. I left to work!");
    }

  }

  public static class Boss implements Runnable {
    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      executorService.submit(() -> {
        System.out.println("Boss. I have Runnable for turn of me when is needed");
        synchronized (psWorker) {
          try {
            psWorker.isFinished().wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        this.running = false;
      });
      executorService.submit(this);
    }

    @Override
    public void run() {
      System.out.println("I'm -> Boss. I'm delayed 4 Secs");
      try {
        Thread.sleep(4000);
      } catch (Exception e) { }

      System.out.println("I'm -> Boss. Let's go to check the work!");
      synchronized (psBoss) {
        psBoss.setRunning(true);
        psBoss.isRunning().notifyAll();
      }
      running = true;
      while (running) {
        System.out.println("I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
      }
      synchronized (psBoss) {
        psBoss.setRunning(false);
        psBoss.isRunning().notify();
      }
      synchronized (psBoss) {
        psBoss.setFinished(true);
        psBoss.isFinished().notify();
      }
      System.out.println("I'm -> Boss The work end!");
    }

  }
公共静态类工作程序实现可运行{
私人最终加工工人;
私人最终处理步骤psBoss;
公共工作者(ExecutorService ExecutorService、ProcessStep psWorker、ProcessStep psBoss){
this.psWorker=psWorker;
this.psBoss=psBoss;
执行人服务。提交(本);
}
@凌驾
公开募捐{
System.out.println(“我是->工人,我一直等到老板看到我!”);
已同步(psBoss){
试一试{
psBoss.isRunning().wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
System.out.println(“我是->工人,老板在看着我!”);
int计数器=0;
while(计数器<6){
已同步(psWorker){
psWorker.setRunning(true);
psWorker.isRunning().notify();
}
System.out.println(“我在工作…”);
试一试{
睡眠(1000);
}捕获(例外e){}
计数器++;
}
已同步(psWorker){
psWorker.setRunning(false);
psWorker.isRunning().notify();
}
已同步(psWorker){
psWorker.setFinished(true);
psWorker.isFinished().notify();
}
System.out.println(“我->工人,我去工作了!”);
}
}
公共静态类Boss实现Runnable{
私人最终处理步骤psBoss;
私人最终加工工人;
私有布尔运行;
公共Boss(ExecutorService ExecutorService、ProcessStep psBoss、ProcessStep psWorker){
this.psBoss=psBoss;
this.psWorker=psWorker;
executorService.submit(()->{
System.out.println(“老板,我可以在需要时轮到我运行”);
已同步(psWorker){
试一试{
psWorker.isFinished().wait();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
this.running=false;
});
执行人服务。提交(本);
}
@凌驾
公开募捐{
System.out.println(“我->老板,我被耽搁了4秒”);
试一试{
睡眠(4000);
}捕获(例外e){}
System.out.println(“我->老板,我们去检查一下工作吧!”);
已同步(psBoss){
psBoss.setRunning(true);
psBoss.isRunning().notifyAll();
}
运行=真;
(跑步时){
System.out.println(“我正在检查…”);
试一试{
睡眠(1000);
}捕获(例外e){}
}
已同步(psBoss){
psBoss.setRunning(false);
psBoss.isRunning().notify();
}
已同步(psBoss){
psBoss.setFinished(true);
psBoss.isFinished().notify();
}
System.out.println(“我->老板工作结束!”);
}
}
我想,我明白了

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class SyncProt1 {

  public static void main(String... args) {
    List<Future<?>> futureList = new ArrayList<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    ProcessStep psWorker = new ProcessStep();
    ProcessStep psBoss = new ProcessStep();

    Worker worker = new Worker(futureList, executorService, psWorker, psBoss);
    Boss boss = new Boss(futureList, executorService, psBoss, psWorker);
  }

  public static class Worker implements Runnable {

    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm -> Worker. I wait until Boss can see me!");
      synchronized (this.psBoss.getRunning()) {
        try {
          this.psBoss.getRunning().wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("I'm -> Worker. Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        synchronized (this.psWorker.getRunning()) {
          this.psWorker.getRunning().setStep(Boolean.TRUE);
          psWorker.getRunning().notify();
        }
        System.out.println("I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
        counter++;
      }
      synchronized (this.psWorker.getRunning()) {
        this.psWorker.getRunning().setStep(Boolean.FALSE);
        this.psWorker.getRunning().notify();
      }
      synchronized (this.psWorker.getFinished()) {
        this.psWorker.getFinished().setStep(Boolean.TRUE);
        this.psWorker.getFinished().notify();
      }
      System.out.println("I'm -> Worker. I left to work!");
    }

  }

  public static class Boss implements Runnable {

    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      futureList.add(executorService.submit(() -> {
        System.out.println("Boss. I have Runnable for turn of me when is needed");
        synchronized (this.psWorker.getFinished()) {
          try {
            this.psWorker.getFinished().wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("Boss. The worker Finished!");
        this.running = false;
      }));
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm -> Boss. I'm delayed 4 Secs. " + new Date());
      try {
        Thread.sleep(4000);
      } catch (Exception e) {
      }

      System.out.println("I'm -> Boss. Let's go to check the work at " + new Date());
      synchronized (this.psBoss.getRunning()) {
        this.psBoss.getRunning().setStep(Boolean.TRUE);
        this.psBoss.getRunning().notify();
      }
      running = true;
      while (running) {
        System.out.println("I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
      }
      synchronized (this.psBoss.getRunning()) {
        this.psBoss.getRunning().setStep(Boolean.FALSE);
        this.psBoss.getRunning().notify();
      }
      synchronized (this.psBoss.getFinished()) {
        this.psBoss.getFinished().setStep(Boolean.TRUE);
        this.psBoss.getFinished().notify();
      }
      System.out.println("I'm -> Boss The work end!");
    }

  }

  public static class ProcessStep {

    private final Step created = new Step();
    private final Step running = new Step();
    private final Step finished = new Step();

    public Step getCreated() {
      return created;
    }

    public Step getRunning() {
      return running;
    }

    public Step getFinished() {
      return finished;
    }

  }

  public static class Step {

    private final ReadWriteLock rwLock;
    private Boolean step;

    public Step() {
      this.rwLock = new ReentrantReadWriteLock();
      step = false;
    }

    public Boolean getStep() {
      Lock readLock = rwLock.readLock();
      readLock.lock();
      try {
        return step;
      } finally {
        readLock.unlock();
      }
    }

    public void setStep(Boolean step) {
      Lock writeLock = rwLock.writeLock();
      writeLock.lock();
      try {
        this.step = step;
      } finally {
        writeLock.unlock();
      }
    }

  }

}
编辑删除
读写锁
可重入写锁
。执行synchronized和编辑
getStep
setStep
方法

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SyncProt4 {

  public static void main(String... args) {
    List<Future<?>> futureList = new ArrayList<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    ProcessStep psWorker = new ProcessStep();
    ProcessStep psBoss = new ProcessStep();

    Worker worker = new Worker(futureList, executorService, psWorker, psBoss);
    Boss boss = new Boss(futureList, executorService, psBoss, psWorker);

  }

  public static class Worker implements Runnable {

    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm Worker -> I wait until Boss can see me!");
      while (!this.psBoss.getRunning().getStep());

      System.out.println("I'm Worker -> Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        this.psWorker.getRunning().setStep(Boolean.TRUE);
        System.out.println("I'm Worker -> I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
        counter++;
      }
      this.psWorker.getRunning().setStep(Boolean.FALSE);
      this.psWorker.getFinished().setStep(Boolean.TRUE);
      System.out.println("I'm Worker -> I left to work!");
    }

  }

  public static class Boss implements Runnable {

    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      futureList.add(executorService.submit(() -> {
        System.out.println("I'm Boss -> I have Runnable for turn of me when is needed");
        while (!this.psWorker.getFinished().getStep());
        System.out.println("I'm Boss -> The Worker Finished!");
        this.running = false;
      }));
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm Boss -> I'm delayed 4 Secs. " + new Date());
      try {
        Thread.sleep(4000);
      } catch (Exception e) {
      }

      System.out.println("I'm Boss -> Let's go to check the work at " + new Date());
      this.psBoss.getRunning().setStep(Boolean.TRUE);
      running = true;
      while (running) {
        System.out.println("I'm Boss -> I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
      }
      this.psBoss.getRunning().setStep(Boolean.FALSE);
      this.psBoss.getFinished().setStep(Boolean.TRUE);
      System.out.println("I'm Boss -> The work ends!");
    }

  }

  public static class ProcessStep {

    private final Step created = new Step();
    private final Step running = new Step();
    private final Step finished = new Step();

    public Step getCreated() {
      return created;
    }

    public Step getRunning() {
      return running;
    }

    public Step getFinished() {
      return finished;
    }

  }

  public static class Step {

    private Boolean step;

    public Step() {
      step = false;
    }

    public synchronized Boolean getStep() {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return step;
    }

    public synchronized void setStep(Boolean step) {
      this.step = step;
      notifyAll();
    }

  }

}

你能缩小范围吗?这是很多代码,正在删除
list还有很多代码,你知道吗?@Eugene很遗憾,要删除的代码将是System.out.println(…),但是如何检查状态呢?您将删除什么代码?不,不可能“在属性上使用同步”。
synchronized
语句适用于对象,因此当您使用
synchronized(this.psBoss.isRunning())
时,您是在当前值上进行同步,该值正在更改。由于属性不是对象,因此无法对其进行同步。您可以创建一个专用于属性的对象,以便在其上进行同步。或者使用
this.psBoss
。使同步工作的关键是,对于所有操作,它必须是相同的对象。当然,您正在调用的
wait
notify
对象也必须是同一个对象。@霍尔格我理解这个错误,谢谢,我找到了一个解决方案。在
Step
对象上同步并调用其
setStep
方法,然后调用
notify()
的逻辑,已在代码中重复五次。考虑一下它的逻辑如何移动到<代码>步骤类中,从而简化了代码。只需创建
Step.setStep
一个
synchronized
方法,该方法在最后调用
notifyAll()
。然后,向它添加一个
waitForTrue()
方法,也就是
synchronized
,其中包含简单的
while(!step)wait()逻辑,所有使用不同步骤的代码都变得简单得多。然后,当您还使用
getStep()
a
synchronized
方法时,您可以删除冗余的
ReadWriteLock
I'm -> Worker. I wait until Boss can see me!
Boss. I have Runnable for turn of me when is needed
I'm -> Boss. I'm delayed 4 Secs
I'm -> Boss. Let's go to check the work!
  public static class Worker implements Runnable {
    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      executorService.submit(this);

    }

    @Override
    public void run() {
      System.out.println("I'm -> Worker. I wait until Boss can see me!");
      synchronized (psBoss) {
        try {
          psBoss.isRunning().wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("I'm -> Worker. Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        synchronized (psWorker) {
          psWorker.setRunning(true);
          psWorker.isRunning().notify();
        }
        System.out.println("I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
        counter++;
      }
      synchronized (psWorker) {
        psWorker.setRunning(false);
        psWorker.isRunning().notify();
      }
      synchronized (psWorker) {
        psWorker.setFinished(true);
        psWorker.isFinished().notify();
      }
      System.out.println("I'm -> Worker. I left to work!");
    }

  }

  public static class Boss implements Runnable {
    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      executorService.submit(() -> {
        System.out.println("Boss. I have Runnable for turn of me when is needed");
        synchronized (psWorker) {
          try {
            psWorker.isFinished().wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        this.running = false;
      });
      executorService.submit(this);
    }

    @Override
    public void run() {
      System.out.println("I'm -> Boss. I'm delayed 4 Secs");
      try {
        Thread.sleep(4000);
      } catch (Exception e) { }

      System.out.println("I'm -> Boss. Let's go to check the work!");
      synchronized (psBoss) {
        psBoss.setRunning(true);
        psBoss.isRunning().notifyAll();
      }
      running = true;
      while (running) {
        System.out.println("I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) { }
      }
      synchronized (psBoss) {
        psBoss.setRunning(false);
        psBoss.isRunning().notify();
      }
      synchronized (psBoss) {
        psBoss.setFinished(true);
        psBoss.isFinished().notify();
      }
      System.out.println("I'm -> Boss The work end!");
    }

  }
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class SyncProt1 {

  public static void main(String... args) {
    List<Future<?>> futureList = new ArrayList<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    ProcessStep psWorker = new ProcessStep();
    ProcessStep psBoss = new ProcessStep();

    Worker worker = new Worker(futureList, executorService, psWorker, psBoss);
    Boss boss = new Boss(futureList, executorService, psBoss, psWorker);
  }

  public static class Worker implements Runnable {

    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm -> Worker. I wait until Boss can see me!");
      synchronized (this.psBoss.getRunning()) {
        try {
          this.psBoss.getRunning().wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      System.out.println("I'm -> Worker. Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        synchronized (this.psWorker.getRunning()) {
          this.psWorker.getRunning().setStep(Boolean.TRUE);
          psWorker.getRunning().notify();
        }
        System.out.println("I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
        counter++;
      }
      synchronized (this.psWorker.getRunning()) {
        this.psWorker.getRunning().setStep(Boolean.FALSE);
        this.psWorker.getRunning().notify();
      }
      synchronized (this.psWorker.getFinished()) {
        this.psWorker.getFinished().setStep(Boolean.TRUE);
        this.psWorker.getFinished().notify();
      }
      System.out.println("I'm -> Worker. I left to work!");
    }

  }

  public static class Boss implements Runnable {

    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      futureList.add(executorService.submit(() -> {
        System.out.println("Boss. I have Runnable for turn of me when is needed");
        synchronized (this.psWorker.getFinished()) {
          try {
            this.psWorker.getFinished().wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        System.out.println("Boss. The worker Finished!");
        this.running = false;
      }));
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm -> Boss. I'm delayed 4 Secs. " + new Date());
      try {
        Thread.sleep(4000);
      } catch (Exception e) {
      }

      System.out.println("I'm -> Boss. Let's go to check the work at " + new Date());
      synchronized (this.psBoss.getRunning()) {
        this.psBoss.getRunning().setStep(Boolean.TRUE);
        this.psBoss.getRunning().notify();
      }
      running = true;
      while (running) {
        System.out.println("I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
      }
      synchronized (this.psBoss.getRunning()) {
        this.psBoss.getRunning().setStep(Boolean.FALSE);
        this.psBoss.getRunning().notify();
      }
      synchronized (this.psBoss.getFinished()) {
        this.psBoss.getFinished().setStep(Boolean.TRUE);
        this.psBoss.getFinished().notify();
      }
      System.out.println("I'm -> Boss The work end!");
    }

  }

  public static class ProcessStep {

    private final Step created = new Step();
    private final Step running = new Step();
    private final Step finished = new Step();

    public Step getCreated() {
      return created;
    }

    public Step getRunning() {
      return running;
    }

    public Step getFinished() {
      return finished;
    }

  }

  public static class Step {

    private final ReadWriteLock rwLock;
    private Boolean step;

    public Step() {
      this.rwLock = new ReentrantReadWriteLock();
      step = false;
    }

    public Boolean getStep() {
      Lock readLock = rwLock.readLock();
      readLock.lock();
      try {
        return step;
      } finally {
        readLock.unlock();
      }
    }

    public void setStep(Boolean step) {
      Lock writeLock = rwLock.writeLock();
      writeLock.lock();
      try {
        this.step = step;
      } finally {
        writeLock.unlock();
      }
    }

  }

}
I'm -> Worker. I wait until Boss can see me!
Boss. I have Runnable for turn of me when is needed
I'm -> Boss. I'm delayed 4 Secs. Mon Aug 19 20:30:44 COT 2019
I'm -> Boss. Let's go to check the work at Mon Aug 19 20:30:48 COT 2019
I'm Checking ...
I'm -> Worker. Boss is seeing to me!
I'm Working ...
I'm Checking ...
I'm Working ...
I'm Checking ...
I'm Working ...
I'm Working ...
I'm Checking ...
I'm Checking ...
I'm Working ...
I'm Checking ...
I'm Working ...
I'm Checking ...
I'm -> Worker. I left to work!
Boss. The worker Finished!
I'm -> Boss The work end!
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SyncProt4 {

  public static void main(String... args) {
    List<Future<?>> futureList = new ArrayList<>();
    ExecutorService executorService = Executors.newCachedThreadPool();
    ProcessStep psWorker = new ProcessStep();
    ProcessStep psBoss = new ProcessStep();

    Worker worker = new Worker(futureList, executorService, psWorker, psBoss);
    Boss boss = new Boss(futureList, executorService, psBoss, psWorker);

  }

  public static class Worker implements Runnable {

    private final ProcessStep psWorker;
    private final ProcessStep psBoss;

    public Worker(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psWorker, ProcessStep psBoss) {
      this.psWorker = psWorker;
      this.psBoss = psBoss;
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm Worker -> I wait until Boss can see me!");
      while (!this.psBoss.getRunning().getStep());

      System.out.println("I'm Worker -> Boss is seeing to me!");
      int counter = 0;
      while (counter < 6) {
        this.psWorker.getRunning().setStep(Boolean.TRUE);
        System.out.println("I'm Worker -> I'm Working ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
        counter++;
      }
      this.psWorker.getRunning().setStep(Boolean.FALSE);
      this.psWorker.getFinished().setStep(Boolean.TRUE);
      System.out.println("I'm Worker -> I left to work!");
    }

  }

  public static class Boss implements Runnable {

    private final ProcessStep psBoss;
    private final ProcessStep psWorker;
    private boolean running;

    public Boss(List<Future<?>> futureList, ExecutorService executorService, ProcessStep psBoss, ProcessStep psWorker) {
      this.psBoss = psBoss;
      this.psWorker = psWorker;

      futureList.add(executorService.submit(() -> {
        System.out.println("I'm Boss -> I have Runnable for turn of me when is needed");
        while (!this.psWorker.getFinished().getStep());
        System.out.println("I'm Boss -> The Worker Finished!");
        this.running = false;
      }));
      futureList.add(executorService.submit(this));
    }

    @Override
    public void run() {
      System.out.println("I'm Boss -> I'm delayed 4 Secs. " + new Date());
      try {
        Thread.sleep(4000);
      } catch (Exception e) {
      }

      System.out.println("I'm Boss -> Let's go to check the work at " + new Date());
      this.psBoss.getRunning().setStep(Boolean.TRUE);
      running = true;
      while (running) {
        System.out.println("I'm Boss -> I'm Checking ...");
        try {
          Thread.sleep(1000);
        } catch (Exception e) {
        }
      }
      this.psBoss.getRunning().setStep(Boolean.FALSE);
      this.psBoss.getFinished().setStep(Boolean.TRUE);
      System.out.println("I'm Boss -> The work ends!");
    }

  }

  public static class ProcessStep {

    private final Step created = new Step();
    private final Step running = new Step();
    private final Step finished = new Step();

    public Step getCreated() {
      return created;
    }

    public Step getRunning() {
      return running;
    }

    public Step getFinished() {
      return finished;
    }

  }

  public static class Step {

    private Boolean step;

    public Step() {
      step = false;
    }

    public synchronized Boolean getStep() {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      return step;
    }

    public synchronized void setStep(Boolean step) {
      this.step = step;
      notifyAll();
    }

  }

}
I'm Worker -> I wait until Boss can see me!
I'm Boss -> I have Runnable for turn of me when is needed
I'm Boss -> I'm delayed 4 Secs. Tue Aug 20 21:27:26 COT 2019
I'm Boss -> Let's go to check the work at Tue Aug 20 21:27:30 COT 2019
I'm Boss -> I'm Checking ...
I'm Worker -> Boss is seeing to me!
I'm Worker -> I'm Working ...
I'm Boss -> I'm Checking ...
I'm Worker -> I'm Working ...
I'm Boss -> I'm Checking ...
I'm Worker -> I'm Working ...
I'm Boss -> I'm Checking ...
I'm Worker -> I'm Working ...
I'm Boss -> I'm Checking ...
I'm Worker -> I'm Working ...
I'm Boss -> I'm Checking ...
I'm Worker -> I'm Working ...
I'm Worker -> I left to work!
I'm Boss -> The Worker Finished!
I'm Boss -> I'm Checking ...
I'm Boss -> The work ends!