CodeGym /Java 博客 /随机的 /互斥量、监视器和信号量之间的区别
John Squirrels

San Francisco

# 互斥量、监视器和信号量之间的区别

## 互斥锁

``````
Object myObject = new Object();
Mutex mutex = myObject.getMutex();
mutex.free();
``````

## 监视器

``````
public class Main {

private Object obj = new Object();

public void doSomething() {

// ...some logic, available for all threads

synchronized (obj) {

// Logic available to just one thread at a time
}
}
}
``````

``````
public class Main {

private Object obj = new Object();

public void doSomething() {

// ...some logic, available for all threads

// Logic available to just one thread at a time
synchronized (obj) {

/* Do important work that requires that the object
be accessed by only one thread */
obj.someImportantMethod();
}
}
}
``````

``````
public class Main {

private Object obj = new Object();

public void doSomething() throws InterruptedException {

// ...some logic, available for all threads

// Logic available to just one thread at a time:

/* as long as the object's mutex is busy,
all the other threads (except the one that acquired it) are put to sleep */
while (obj.getMutex().isBusy()) {
}

// Mark the object's mutex as busy
obj.getMutex().isBusy() = true;

/* Do important work that requires that the object
be accessed by only one thread */
obj.someImportantMethod();

// Free the object's mutex
obj.getMutex().isBusy() = false;
}
}
``````

## 信号

``````
Semaphore(int permits)
Semaphore(int permits, boolean fair)
``````

int permits — 计数器的初始值和最大值。也就是说，这个参数决定了有多少线程可以同时访问共享资源；
• boolean fair — 确定线程获得访问权限的顺序。如果fair为真，则按照等待线程请求的顺序授予访问权限。如果为假，则顺序由线程调度程序确定。

``````

private Semaphore sem;

// Did the philosopher eat?
private boolean full = false;

private String name;

Philosopher(Semaphore sem, String name) {
this.sem=sem;
this.name=name;
}

public void run()
{
try
{
// If the philosopher has not eaten
if (!full) {
// Ask the semaphore for permission to run
sem.acquire();
System.out.println(name + " takes a seat at the table");

// The philosopher eats
sleep(300);
full = true;

System.out.println(name + " has eaten! He leaves the table");
sem.release();

// The philosopher leaves, making room for others
sleep(300);
}
}
catch(InterruptedException e) {
System.out.println("Something went wrong!");
}
}
}
``````

``````
public class Main {

public static void main(String[] args) {

Semaphore sem = new Semaphore(2);
new Philosopher(sem, "Socrates").start();
new Philosopher(sem,"Plato").start();
new Philosopher(sem,"Aristotle").start();
new Philosopher(sem, "Thales").start();
new Philosopher(sem, "Pythagoras").start();
}
}
``````

``````
Socrates takes a seat at the table
Plato takes a seat at the table
Socrates has eaten! He leaves the table
Plato has eaten! He leaves the table
Aristotle takes a seat at the table
Pythagoras takes a seat at the table
Aristotle has eaten! He leaves the table
Pythagoras has eaten! He leaves the table
Thales takes a seat at the table
Thales has eaten! He leaves the table
``````

TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION