mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 23:19:56 +00:00
Uses synchronised keyword.
This commit is contained in:
parent
4f751a011e
commit
c7437d90bc
@ -12,15 +12,16 @@ public class Consumer implements Runnable {
|
||||
private Queue<UnitOfWork> store;
|
||||
private String name;
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
synchronized (store) {
|
||||
if (store.size() > 0) {
|
||||
store.remove();
|
||||
System.out.println("Consumed: " + name + " Left units :" + store.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,15 +13,16 @@ public class Producer implements Runnable {
|
||||
private int maxSize;
|
||||
private String name;
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
synchronized (store) {
|
||||
if (store.size() < maxSize) {
|
||||
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