我有这个代码。
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();
}
}
}
}
正确的输出是:
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!
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 Checking ...
I'm Working ...
I'm Checking ...
I'm Working ...
I'm Checking ...
I'm Working ...
I'm Checking ...
I'm -> Worker. I left to work!
I'm -> Boss The work end!
工作正常!
注意:我不想ProcessStep
按需阻止所有属性!
我想在更改此代码的属性上使用Wait
和Notify
机制(在Boss
类中):
while (!this.psWorker.isFinished()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
和这段代码(在Worker
课堂上)
while (!psBoss.isRunning()) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
当我使用此代码更改Worker
和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();
}
}
}
}
输出是
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!
剩下的Worker
等待!
是否可以在属性上使用同步?
另一个具有相同结果的版本是:
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!");
}
}