Semaphore
package com.qqjx.thread;
import java.util.concurrent.Semaphore;
public class T11_TestSemaphore {
public static void main(String[] args) {
Semaphore s = new Semaphore(2, true);
new Thread(()->{
try {
s.acquire();
System.out.println("T1 running...");
Thread.sleep(200);
System.out.println("T1 running...");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
s.release();
}
}).start();
new Thread(()->{
try {
s.acquire();
System.out.println("T2 running...");
Thread.sleep(200);
System.out.println("T2 running...");
s.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
Semaphore s = new Semaphore(2, true);
T1 running...
T2 running...
T1 running...
T2 running...
Semaphore s = new Semaphore(1, true);
T1 running...
T1 running...
T2 running...
T2 running...