import java.util.concurrent.*;
import java.util.*;

class MyRunnable implements Runnable {
    Semaphore sem;
    
    MyRunnable(Semaphore sem) {
        this.sem = sem;
    }
    
    public void run() {
        try {
            this.sem.acquire();
            String name = Thread.currentThread().getName();
            System.out.println(name + " acquired thread");
            Thread.sleep(100);
            this.sem.release();
            System.out.println(name + " released thread");
        } catch (InterruptedException e) {
            System.out.println("interrupted!");
        }
    }
}

class Main {
    public static void main(String[] args) throws InterruptedException {
        Semaphore sem = new Semaphore(3);
        Thread[] thread = new Thread[10];
        for (int i = 0; i < 10; i++) {
            thread[i] = new Thread(new MyRunnable(sem));
            thread[i].start();
        }
        for (int i = 0; i < 10; i++) {
            thread[i].join();
        }
    }
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: