import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Main { // Global bir lock tanımlama static Lock lock = new ReentrantLock(); static class MyThread extends Thread { public void run() { // Lock'ı kilitleme lock.lock(); // Kritik bölgeye erişim System.out.println("Thread " + Thread.currentThread().getId() + " is in critical section."); // Lock'ı serbest bırakma lock.unlock(); } } public static void main(String[] args) { final int NUM_THREADS = 5; MyThread[] threads = new MyThread[NUM_THREADS]; // İş parçacıklarını oluşturma ve başlatma for (int i = 0; i < NUM_THREADS; ++i) { threads[i] = new MyThread(); threads[i].start(); } // İş parçacıklarının tamamlanmasını beklemek for (int i = 0; i < NUM_THREADS; ++i) { try { threads[i].join(); } catch (InterruptedException e) { e.printStackTrace(); } } } }