mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 23:19:56 +00:00
Updates adder subtractor code.
This commit is contained in:
parent
129277bbed
commit
1387f48f55
@ -10,7 +10,17 @@ public class Adder implements Runnable {
|
||||
public void run() {
|
||||
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
count.getValue().getAndAdd(i);
|
||||
|
||||
int value = count.getValue();
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Something wrong happened");
|
||||
}
|
||||
|
||||
int nextValue = value + i;
|
||||
count.setValue(nextValue);
|
||||
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (Exception e) {
|
||||
|
@ -1,15 +1,13 @@
|
||||
package com.scaler.addersubtractor;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class Count {
|
||||
private AtomicInteger value = new AtomicInteger(0);
|
||||
private volatile int value = 0;
|
||||
|
||||
public AtomicInteger getValue() {
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(AtomicInteger value) {
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.scaler.addersubtractor;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
@ -12,7 +10,17 @@ public class Subtractor implements Runnable {
|
||||
public void run() {
|
||||
|
||||
for (int i = 1; i <= 100; ++i) {
|
||||
count.getValue().getAndAdd(-i);
|
||||
|
||||
int value = count.getValue();
|
||||
try {
|
||||
Thread.sleep(50);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Something wrong happened");
|
||||
}
|
||||
|
||||
int nextValue = value - i;
|
||||
count.setValue(nextValue);
|
||||
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (Exception e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user