mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 22:59:53 +00:00
Uses synchronised keyword.
This commit is contained in:
parent
4f751a011e
commit
c7437d90bc
@ -12,16 +12,17 @@ public class Consumer implements Runnable {
|
|||||||
private Queue<UnitOfWork> store;
|
private Queue<UnitOfWork> store;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (store.size() > 0) {
|
synchronized (store) {
|
||||||
store.remove();
|
if (store.size() > 0) {
|
||||||
System.out.println("Consumed: " + name + " Left units :" + store.size());
|
store.remove();
|
||||||
|
System.out.println("Consumed: " + name + " Left units :" + store.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,16 +13,17 @@ public class Producer implements Runnable {
|
|||||||
private int maxSize;
|
private int maxSize;
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (store.size() < maxSize) {
|
synchronized (store) {
|
||||||
store.add(new UnitOfWork());
|
if (store.size() < maxSize) {
|
||||||
System.out.println("Produced: " + name + " Left units :" + store.size());
|
store.add(new UnitOfWork());
|
||||||
|
System.out.println("Produced: " + name + " Left units :" + store.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user