`
BradyZhu
  • 浏览: 249193 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java多线程~~~使用信号量来控制资源获取

 
阅读更多

在多线程开发中,有一个很经典的名词,那就是信号量。信号量就是用来衡量一个资源的可利用数目的,根据信号

量的多少来控制在多线程中各个资源之间的冲突问题,在Java中也提供了对信号量的支持。


而且在创建信号量的时候,第二个参数用来指定采取何种分配策略,比如当有很多线程被阻塞,但有一个机会的时

候,信号量应该选择谁去运行呢,如果选择true,就采用公平模式,到时候看哪个线程等待的时间最久,就把机会给那

个等待最久的线程,这样的就是公平分配策略。


下面就用代码来说明一下问题


package com.bird.concursey.charpet4;

import java.util.concurrent.Semaphore;

public class PrintQueue {
	// It initializes the semaphore object that will protect the access from the
	// print queue.
	private final Semaphore semaphore = new Semaphore(1,true);

	public void printJob(Object document) {
		//you must acquire the semaphore calling
		try {
			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);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			//free the semaphore by calling the release() method of the semaphore.
			semaphore.release();
		}
	}
}



package com.bird.concursey.charpet4;

public class Job implements Runnable {
	
	private PrintQueue printQueue;
	
	public Job(PrintQueue printQueue) {
		this.printQueue = printQueue;
	}

	@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());
	}
	
	public static void main(String[] args) {
		PrintQueue printQueue = new PrintQueue();
		Thread thread[] = new Thread[10];
		for(int i = 0; i < 10; i++) {
			thread[i] = new Thread(new Job(printQueue), "Thread " + i);
		}
		for(int i = 0; i < 10; i++) {
			thread[i].start();
		}
	}

}


分享到:
评论

相关推荐

    python多线程DAY04.txt

    解决了多个进程或者线程对共享资源的争夺 Event e.set e.clear e.wait Lock lock.acquire() lock.release() 4. 什么是线程 threading Thread() t.start() t.join() t.name t.getName t.setName t.daemon...

    Java并发编程(学习笔记).xmind

    (1)如果设计正确,多线程程序可以通过提高处理器资源的利用率来提升系统吞吐率 (2)建模简单:通过使用线程可以讲复杂并且异步的工作流进一步分解成一组简单并且同步的工作流,每个工作流在一个单独的线程...

    Java并发编程实战

    书中从并发性和线程安全性的基本概念出发,介绍了如何使用类库提供的基本并发构建块,用于避免并发危险、构造线程安全的类及验证线程安全的规则,如何将小的线程安全类组合成更大的线程安全类,如何利用线程来提高...

    JAVA基于局域网的聊天室系统(源代码+论文).zip

    同时,在本方案中,采用了线程来实现语音录制和语音回放,最终实现了通过服务器中转的文字聊天、点对点的语音视频聊天。 关键词:文字聊天;VFW;视频捕获;视频传输;语音录制;语音回放 VFW是Microsoft 1992年...

    带你看看Java的锁(二)-Semaphore

    Semaphore 中文称信号量,它和ReentrantLock 有所区别,ReentrantLock是排他的,也就是只能允许一个线程拥有资源,Semaphore是共享的,它允许多个线程同时拥有资源,是AQS中共享模式的实现,在前面的AQS分析文章中,...

    java核心知识点整理.pdf

    25 JAVA8 与元数据.................................................................................................................................25 2.4. 垃圾回收与算法 .................................

    javaSE代码实例

    17.4 信号量的使用 393 17.4.1 Semaphore类简介 393 17.4.2 Semaphore类的具体使用 394 17.5 队列 396 17.5.1 Queue接口介绍 396 17.5.2 PriorityQueue类的知识与使用 397 17.5.3 BlockingQueue接口...

    JAVA核心知识点整理(有效)

    25 JAVA8 与元数据.................................................................................................................................25 2.4. 垃圾回收与算法 .................................

    c#学习笔记.txt

    另外我发现论坛上学习Java的人都非常的有个性,当有人问起学习哪种语言更好时,他会打出几百个“JAVA”来,填满整个屏幕,也不说是为了什么。我觉得这样做未免有些太霸道了,如果你说这叫偏执狂我也不反对,虽然我...

Global site tag (gtag.js) - Google Analytics