好理解的,操作系统课上信号灯
package pcenshao.thread;import java.util.concurrent.Semaphore;public class SemaphoreABC {
static class T extends Thread{
Semaphore me;
Semaphore next;
public T(String name,Semaphore me,Semaphore next){
super(name);
this.me = me;
this.next = next;
}
@Override
public void run(){
for(int i = 0 ; i < 10 ; i ++){
try {
me.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName());
next.release();
}
}
}
public static void main(String[] args) {
Semaphore aSem = new Semaphore(1);
Semaphore bSem = new Semaphore(0);
Semaphore cSem = new Semaphore(0);
T a = new T("A",aSem,bSem);
T b = new T("B",bSem,cSem);
T c = new T("C",cSem,aSem);
a.start();
b.start();
c.start();
}
}
package pcenshao.thread;import java.util.concurrent.Semaphore;public class SemaphoreABC {
static class T extends Thread{
Semaphore me;
Semaphore next;
public T(String name,Semaphore me,Semaphore next){
super(name);
this.me = me;
this.next = next;
}
@Override
public void run(){
for(int i = 0 ; i < 10 ; i ++){
try {
me.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName());
next.release();
}
}
}
public static void main(String[] args) {
Semaphore aSem = new Semaphore(1);
Semaphore bSem = new Semaphore(0);
Semaphore cSem = new Semaphore(0);
T a = new T("A",aSem,bSem);
T b = new T("B",bSem,cSem);
T c = new T("C",cSem,aSem);
a.start();
b.start();
c.start();
}
}
static class T extends Thread{
Semaphore me;
Semaphore next;
public T(String name,Semaphore me,Semaphore next){
super(name);
this.me = me;
this.next = next;
}
@Override
public void run(){
for(int i = 0 ; i < 10 ; i ++){
try {
me.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName());
next.release();
}
}
}
public static void main(String[] args) {
Semaphore aSem = new Semaphore(1);
Semaphore bSem = new Semaphore(0);
Semaphore cSem = new Semaphore(0);
T a = new T("A",aSem,bSem);
T b = new T("B",bSem,cSem);
T c = new T("C",cSem,aSem);
a.start();
b.start();
c.start();
}
}
看看 acquire 和release的用法