import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
static Lock lock = new ReentrantLock();
static class MyThread extends Thread {
public void run() {
lock.lock();
System.out.println("Thread " + Thread.currentThread().getId() + " is in critical section.");
lock.unlock();
}
}
public static void main(String[] args) {
final int NUM_THREADS = 5;
MyThread[] threads = new MyThread[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; ++i) {
threads[i] = new MyThread();
threads[i].start();
}
for (int i = 0; i < NUM_THREADS; ++i) {
try {
threads[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}