代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
信号量计数
作者:
人生如歌
/ 发布于
2015/7/17
/
495
package com.javapapers.thread; public class CountingSemaphore { private int value = 0; private int waitCount = 0; private int notifyCount = 0; public CountingSemaphore(int initial) { if (initial > 0) { value = initial; } } public synchronized void waitForNotify() { if (value <= waitCount) { waitCount++; try { do { wait(); } while (notifyCount == 0); } catch (InterruptedException e) { notify(); } finally { waitCount--; } notifyCount--; } value--; } public synchronized void notifyToWakeup() { value++; if (waitCount > notifyCount) { notifyCount++; notify(); } } } Binary Semaphore package com.javapapers.thread; public class BinarySemaphore { private boolean locked = false; BinarySemaphore(int initial) { locked = (initial == 0); } public synchronized void waitForNotify() throws InterruptedException { while (locked) { wait(); } locked = true; } public synchronized void notifyToWakeup() { if (locked) { notify(); } locked = false; } } 测试类 SemaphoreABC.java package com.javapapers.thread; import java.util.Random; public class SemaphoreABC { protected static final BinarySemaphore binarySemaphore0 = new BinarySemaphore( 0); protected static final BinarySemaphore binarySemaphore1 = new BinarySemaphore( 1); protected static final CountingSemaphore countingSemaphore = new CountingSemaphore( 0); protected static final Random random = new Random(); public static void main(String args[]) throws InterruptedException { new Thread(new ProcessA()).start(); new Thread(new ProcessB()).start(); new Thread(new ProcessC()).start(); Thread.sleep(9000); System.exit(0); } } ProcessA.java package com.javapapers.thread; public class ProcessA extends SemaphoreABC implements Runnable { public void run() { while (true) { try { Thread.sleep(1 + (int) (random.nextDouble() * 500)); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.print("A"); countingSemaphore.notifyToWakeup(); } } } ProcessB.java package com.javapapers.thread; public class ProcessB extends SemaphoreABC implements Runnable { public void run() { while (true) { try { Thread.sleep(1 + (int) (random.nextDouble() * 800)); binarySemaphore1.waitForNotify(); } catch (InterruptedException e) { e.printStackTrace(); } countingSemaphore.waitForNotify(); System.out.print("B"); binarySemaphore0.notifyToWakeup(); } } } ProcessC.java package com.javapapers.thread; public class ProcessC extends SemaphoreABC implements Runnable { public void run() { while (true) { try { Thread.sleep(1 + (int) (random.nextDouble() * 800)); binarySemaphore0.waitForNotify(); } catch (InterruptedException e) { e.printStackTrace(); } countingSemaphore.waitForNotify(); System.out.print("C"); binarySemaphore1.notifyToWakeup(); } } } 输出 AABCABACAABCAABCAAABCABCABACAABCABACABACABACABAC
试试其它关键字
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
人生如歌
贡献的其它代码
(
31
)
.
base64编码和图片互相转换
.
生成一个XElement字典
.
实现连接两个url
.
判断字符串中是否包含某个字符
.
获得进程内存使用量的脚本
.
实现ftp传输
.
获得web页面信息
.
获得系统属性和系统时间
.
获得进程号和进程的参数信息
.
读取邮件帐户信息
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3