CONCURRENT STACK
AIM:
Given a program to implement concurrent stack
ALGORITHM:
<Pre-condition>: First to check whether stack is empty or not.
<Post-condition>: found the counter value after increment.
When stack is empty , values are added .
Condition -> for (int i = 0; i < 300; i++)
To increment the counter value by using getAndIncrement()
Exit the program, while condition is exist.
SOURCE CODE:
import [Link].*;
public class Exercise {
static int counter = 0;
static synchronized int getAndIncrement() {
return counter++;
}
static class Improper implements Runnable {
@Override
public void run() {
for (int i = 0; i < 300; i++) {
getAndIncrement();
}
}
}
public static void main(String[] args) {
ExecutorService executorService = [Link](3);
for (int i = 0; i < 300; i++) {
[Link](new Improper());
}
[Link]();
[Link](counter);
}
}
OUTPUT:
RESULT:
For a given input, the program successfully executed concurrent stack