易截截图软件、单文件、免安装、纯绿色、仅160KB

JAVA and C++版信号量(PV操作)

import java.util.concurrent.Semaphore;
/**
* 抽象任务,具体的执行任务,归实现类负责
*
* @author Administrator
*
*/
public abstract class Task {
public abstract void run();
private Semaphore s;
private boolean hasExisted = false;
public void P(final Semaphore s) throws InterruptedException {
if (s == null) { // 申请空的信号量
throw new InterruptedException("不能为空");
}
if (hasExisted) {// 已经申请了一个资源,还没有释放
throw new InterruptedException("已经占用一个资源");
}
s.acquire();// 阻塞
this.s = s;
hasExisted = true;
}
public boolean V() {
if (!hasExisted) {
return false;// 没沾有资源就不能说释放了
}
s.release();//释放资源
hasExisted = false;
s = null;
return true;
}
}
import java.util.concurrent.Semaphore;
/**
* 用信号量实现互斥
*
* @author Administrator
*
*/
public class Mutex extends Semaphore {
private static final long serialVersionUID = 1L;
public Mutex() {
super(1);//互斥是1
}
} import java.util.concurrent.Semaphore;
public class Test {
public static void main(String[] args) {
// final Semaphore s = new Semaphore(2);//信号量,执行结果run the task1 run the
// task2 run the task3
final Semaphore s = new Mutex();// 互斥 run the task1 run the task2 run
// the task3
new Thread() {
public void run() {
Task1 task2 = new Task1();
try {
task2.P(s);// 申请资源
task2.run();// 执行任务
task2.V();// 释放资源
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
Task2 task2 = new Task2();
try {
task2.P(s);
task2.run();
task2.V();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
Task3 task2 = new Task3();
try {
task2.P(s);
task2.run();
task2.V();
} catch (InterruptedException e)


相关文档:

java 常用工具类

DateTimeHelper 时间组件
/**
 *
 */
package com.ibm08001.bbs.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 *
 */
public class DateTimeHelper {
        private static SimpleDateFormat FULL_SDF = new SimpleDateFormat ......

java 相关的书 Rubicon

《Java how to program》
《Core java 2》
《Thinking in java》
《程序设计实践》
《代码大全》
《设计模式》
《java 网络编程》
serverlet jsp javabean spring habernate mysql等。 ......

java高级多线程编程 关于线程的停止问题(转)

多线程是java的一个优势,java使得程序员可以很方便的进行多线程程序开发。获得更好的性能。
关于多线程的概念以及一般的多线程编程,比如如何以及为何实现runnable接口,为何stop()会被Deprecated掉等等,这个请看matrix之前的多线程编程基础或者sun的java文档。
关于多线程编程,有几点这里要提到的:
1。既然stop( ......

java加密和数字签名2 私钥加密

近初步接触了下Java加密和数字签名的相关内容,我学习的过程大概分五步:
1)消息摘要
2)私钥加密
3)公钥加密
4)数字签名
5) 数字证书
下面的代码是第二部分:私钥加密
希望能为刚刚接触这个的朋友们省点事
package security;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号