本文章由azibug编写,转载请注明出处,请勿用于商业用途
文章链接:http://blog.csdn.net/owillll/article/details/8882562
作者:azibug 邮箱: azibug#163.com
信号量(Semaphares)
概述
用来控制同时访问某个资源池的操作数量。当permits为零时使用acquire会一直阻塞,直至permits不为零;如果permits不为零,使用acquire则会将permits减一。使用完资源可以使用release释放许可,这样permits就会加一
用法:
- 使用new Semaphare(permits) 初始化可同时操作自由的数量
- 使用semaphare.acquire() 获取一个允许,如果permits值为零,则会一直阻塞,直至permits不为零
- 工作完使用semaphare.release()(一般在finally里面),释放许可,使其他线程可以使用资源。
使用场所:
通常用于限制可以访问某些资源的线程数量。比如打印机中将permits设为1,则多个线程打印时,打印队列也只会有一个在工作。
代码示例
PrintQueue.java打印队列,执行打印的操作
package com.chaokuaidi.java.concurrency.utils.semaphores;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* This class implements the PrintQueue using a Semaphore to control the
* access to it.
*
*/
public class PrintQueue {
/**
* Semaphore to control the access to the queue
*/
private final Semaphore semaphore;
/**
* Constructor of the class. Initializes the semaphore
*/
public PrintQueue(){
semaphore=new Semaphore(1);
}
/**
* Method that simulates printing a document
* @param document Document to print
*/
public void printJob (Object document){
try {
// Get the access to the semaphore. If other job is printing, this
// thread sleep until get the access to the semaphore
semaphore.acquire();
Long duration=(long)(Math.random()*10);
System.out.printf("%s: PrintQueue: Printing a Job during %d seconds\n",Thread.currentThread().getName(),duration);
Thread.sleep(duration);
TimeUnit.SECONDS.sleep(duration);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// Free the semaphore. If there are other threads waiting for this semaphore,
// the JVM selects one of this threads and give it the access.
semaphore.release();
}
}
}
Job.java 工作线程,调用打印操作
package com.chaokuaidi.java.concurrency.utils.semaphores;
/**
* This class simulates a job that send a document to print.
*
*/
public class Job implements Runnable {
/**
* Queue to print the documents
*/
private PrintQueue printQueue;
/**
* Constructor of the class. Initializes the queue
* @param printQueue
*/
public Job(PrintQueue printQueue){
this.printQueue=printQueue;
}
/**
* Core method of the Job. Sends the document to the print queue and waits
* for its finalization
*/
@Override
public void run() {
System.out.printf("%s: Going to print a job\n",Thread.currentThread().getName());
printQueue.printJob(new Object());
System.out.printf("%s: The document has been printed\n",Thread.currentThread().getName());
}
}
Main.java 初始化打印机并开始打印线程
package com.chaokuaidi.java.concurrency.utils.semaphores;
/**
* Main class of the example.
*
*/
public class Main {
/**
* Main method of the class. Run ten jobs in parallel that
* send documents to the print queue at the same time.
*/
public static void main (String args[]){
// Creates the print queue
PrintQueue printQueue=new PrintQueue();
// Creates ten Threads
Thread thread[]=new Thread[10];
for (int i=0; i<10; i++){
thread[i]=new Thread(new Job(printQueue),"Thread "+i);
}
// Starts the Threads
for (int i=0; i<10; i++){
thread[i].start();
}
}
}
注:java同步工具类的所有代码全部托管在github上:https://github.com/azibug/concurrency 你可以很容易地构建并测试他
参考资料:
1. 《Java 7 Concurrency Cookbook》,绝大部分示例来源于此
2. 《Java 并发编程实战》
3. 《Thinking in Java》