关于迅雷的线程题,再发一贴

pywepe 2010-08-01 12:40:44
好理解的,操作系统课上信号灯
package pcenshao.thread;

import java.util.concurrent.Semaphore;

public class SemaphoreABC {

static class T extends Thread{

Semaphore me;
Semaphore next;

public T(String name,Semaphore me,Semaphore next){
super(name);
this.me = me;
this.next = next;
}

@Override
public void run(){
for(int i = 0 ; i < 10 ; i ++){
try {
me.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName());
next.release();
}
}
}

public static void main(String[] args) {
Semaphore aSem = new Semaphore(1);
Semaphore bSem = new Semaphore(0);
Semaphore cSem = new Semaphore(0);

T a = new T("A",aSem,bSem);
T b = new T("B",bSem,cSem);
T c = new T("C",cSem,aSem);

a.start();
b.start();
c.start();
}
}
...全文
306 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
easyroom 2010-08-09
  • 打赏
  • 举报
回复
mark,回家再看。
看看 acquire 和release的用法
goldyellow34 2010-08-08
  • 打赏
  • 举报
回复
信号量 关键域问题
Ade子夜 2010-08-06
  • 打赏
  • 举报
回复
package pcenshao.thread;

import java.util.concurrent.Semaphore;

public class SemaphoreABC {

static class T extends Thread{

Semaphore me;
Semaphore next;

public T(String name,Semaphore me,Semaphore next){
super(name);
this.me = me;
this.next = next;
}

@Override
public void run(){
for(int i = 0 ; i < 10 ; i ++){
try {
me.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.getName());
next.release();
}
}
}

public static void main(String[] args) {
Semaphore aSem = new Semaphore(1);
Semaphore bSem = new Semaphore(0);
Semaphore cSem = new Semaphore(0);

T a = new T("A",aSem,bSem);
T b = new T("B",bSem,cSem);
T c = new T("C",cSem,aSem);

a.start();
b.start();
c.start();
}
}

vivazhao 2010-08-06
  • 打赏
  • 举报
回复
学习了
lizhongyi188 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dyllove98 的回复:]

java.util.concurrent包就是强悍 多线程开发利器
[/Quote]
回去看看!
qingtianlo 2010-08-02
  • 打赏
  • 举报
回复
学习。。。
kyousuke 2010-08-02
  • 打赏
  • 举报
回复
a no
avalon 2010-08-02
  • 打赏
  • 举报
回复
貌似对这个包里头的东西不太了解,回头得去好好看看!
shehun 2010-08-01
  • 打赏
  • 举报
回复
没有坐上沙发,只能做板凳了,
Jlins 2010-08-01
  • 打赏
  • 举报
回复
java.util.concurrent包就是强悍 多线程开发利器

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧