0% found this document useful (0 votes)
23 views

Concurrent Stack Aim

The document describes a program to implement a concurrent stack. It uses an ExecutorService with a fixed thread pool of 3 threads to submit 300 Improper runnable tasks. Each task increments a static counter 300 times using a synchronized getAndIncrement method. The main method submits all the tasks and shuts down the executor service. It then prints the final counter value, which should be 90000, showing the tasks were successfully executed concurrently on the stack.

Uploaded by

bhagyagr8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Concurrent Stack Aim

The document describes a program to implement a concurrent stack. It uses an ExecutorService with a fixed thread pool of 3 threads to submit 300 Improper runnable tasks. Each task increments a static counter 300 times using a synchronized getAndIncrement method. The main method submits all the tasks and shuts down the executor service. It then prints the final counter value, which should be 90000, showing the tasks were successfully executed concurrently on the stack.

Uploaded by

bhagyagr8
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 java.util.concurrent.*;
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 = Executors.newFixedThreadPool(3);
for (int i = 0; i < 300; i++) {

executorService.submit(new Improper());
}
executorService.shutdown();
System.out.println(counter);
}
}

OUTPUT:

RESULT:
For a given input, the program successfully executed concurrent stack

You might also like