Last active
September 28, 2020 01:38
-
-
Save ashishlahoti/2cd740b50652f0b3cfb1bc6a2bcc99a6 to your computer and use it in GitHub Desktop.
Semaphore is used when you want to limit maximum number of concurrent calls to a particular resource in multi-threaded environment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.thread; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Semaphore; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.IntStream; | |
/** | |
* Semaphore is used when you want to limit maximum number of concurrent calls | |
* to a particular resource in multi-threaded environment. | |
* | |
* @author aklahoti | |
*/ | |
public class SemaphoreTest { | |
public static void main(String[] args) throws InterruptedException { | |
// ThreadPool of 50 threads | |
ExecutorService service = Executors.newFixedThreadPool(50); | |
// Semaphore to rum max 3 concurrent calls | |
Semaphore semaphore = new Semaphore(3); | |
// Execute 1000 tasks (initiate 1000 threads) to test | |
IntStream.of(1000).forEach(i -> service.execute(new Task(semaphore))); | |
// Shutdown the ExecutorService gracefully | |
service.shutdown(); | |
service.awaitTermination(1, TimeUnit.MINUTES); | |
} | |
static class Task implements Runnable { | |
private Semaphore semaphore; | |
Task(Semaphore semaphore) { | |
this.semaphore = semaphore; | |
} | |
@Override | |
public void run() { | |
// Start code -> can be executed by 50 threads concurrently | |
// since thread pool size is 50 | |
try { | |
// Only 3 threads can acquire lock at a time since semaphore count is 3 | |
// Other threads wait here. | |
semaphore.acquire(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
// Slow Code -> Heavy operations like IO | |
// can be executed by only 3 threads | |
// Release the lock for other threads to come in Slow code | |
semaphore.release(); | |
// End code -> can be executed by 50 threads concurrently | |
// since thread pool size is 50 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment