RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Java>列表

Java语言深入-对JAVA 的多线程浅析

来源: 作者: 时间:2008-03-03 点击:

}

public synchronized void readUnlock(){

readCnt--;

if(readCnt==0){

state=EMPTY;

notify();

}

}

public synchronized void writeUnlock(){

state=EMPTY;

notify();

}

}

现在是测试信号标志的程序:

class Process extends Thread{

String op;

Semaphore sem;

Process(String name,String op,Semaphore sem){

super(name);

this.op=op;

this.sem=sem;

start();

}

public void run(){

if(op.compareTo("read")==0){

System.out.println("Trying to get readLock:"+getName());

sem.readLock();

System.out.println("Read op:"+getName());

try {sleep((int)(Math.random()*50));}

catch(InterruptedException e){;}

System.out.println("Unlocking readLock:"+getName());

sem.readUnlock();

}

else if(op.compareTo("write")==0){

System.out.println("Trying to get writeLock:"+getName());

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册