浏览 3186 次
锁定老帖子 主题:请教:java多线程问题
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-03-20
比如,java中lock和conditon问题,假如有一个lock,有此lock生成了多个condition,c1,c2,...cn,假如存在两个线程,其中一个线程按如下等待条件,如 c1.wait(); c2.wait(); ... cn.wait(); 而另一个线程如下等待条件 cn.wait(); ... c2.wait(); c1.wait(); 这样就容易造成死锁,随着condition的增多和线程数量的增多,越是容易造成死锁,有没有个办法,要么可以同时获得c1,c2,...,cn,要么都不获得,即使有个别condition可以获得? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-03-20
不是很明白你的意思,你可以试试看java.util.concurrent.CountDownLatch是不是满足你的需求
|
|
返回顶楼 | |
发表时间:2008-03-20
通过Object类里的notify能不能得到解决呢?
好像另外还有关于线程的设计模式,我也记不大清了,希望这个链接可以有所帮助:http://hi.baidu.com/hibydu/blog/item/54e8d61f6bc99362f724e415.html 另处补充一句,那个博客是我刚粟出来的。 |
|
返回顶楼 | |
发表时间:2008-03-20
一个用CountDownLatch实现的MultiThreadManager
http://www.google.com/codesearch?hl=en&q=+MultiThreadManager+show:ZtTNYs-zcMM:m7pjry2CcK8:xoFWM7LeQ8c&sa=N&cd=1&ct=rc&cs_p=http://jira.codehaus.org/secure/attachment/17644/modules.zip&cs_f=utils/utils-lang-jar/src/main/com/barcoview/mis/utils/lang/thread/MultiThreadManager.java#first 使用示例 try { // runables 是要运行的线程列表 MultiThreadManager manager = new MultiThreadManager( runnables, "MyRunnables-" ); manager.runAndWait(); } catch( MultiThreadManagerException e ) { MyLogicalException ex = (MyLogicalException)e.getCause(); } |
|
返回顶楼 | |
发表时间:2008-03-20
一下是我的测试程序,如下所示,这个程序很快就会死锁,假如执行add函数的那个线程等待到了c1,c2,c3,c4,而执行rem()函数的那个线程等到了c7,c6,c5,这时就产生了死锁,我想问的是java中有没有一种机制保证,要么c1,c2,c3,c4,c5,c6,c7全部同时得到,要么就同时放弃,这样就避免了死锁的发生?
public class Test { private int[] elems = new int[5]; Lock lock1 = new ReentrantLock(true); Condition c1 = lock1.newCondition(); Condition c2 = lock1.newCondition(); Condition c3 = lock1.newCondition(); Condition c4 = lock1.newCondition(); Condition c5 = lock1.newCondition(); Condition c6 = lock1.newCondition(); Condition c7 = lock1.newCondition(); private int addIndex = 0; private int remIndex = 0; private int count = 0; public void add(int i) { lock1.lock(); try { while (elems.length == count) { try { c1.await(); c2.await(); c3.await(); c4.await(); c5.await(); c6.await(); c7.await(); } catch (Exception e) { e.printStackTrace(); } } elems[addIndex++] = i; count++; addIndex = addIndex % elems.length; System.out.println(Thread.currentThread().getName() + " add count=" + count); c1.signal(); c2.signal(); c3.signal(); c4.signal(); c5.signal(); c6.signal(); c7.signal(); } finally { lock1.unlock(); } } public int rem() { int res = 0; lock1.lock(); try { while (0 == count) { try { c7.await(); c6.await(); c5.await(); c4.await(); c3.await(); c2.await(); c1.await(); } catch (Exception e) { e.printStackTrace(); } } res = elems[remIndex++]; count--; remIndex = remIndex % elems.length; System.out.println(Thread.currentThread().getName() + " remove count=" + count); c7.signal(); c6.signal(); c5.signal(); c4.signal(); c3.signal(); c2.signal(); c1.signal(); } finally { lock1.unlock(); } return res; } public void start() { for (int i = 0; i < 3000; i++) { Thread t = new Thread(new producer(this)); t.start(); } for (int i = 0; i < 3; i++) { Thread t = new Thread(new consumer(this)); t.start(); } } /** * @param args */ public static void main(String[] args) { Test test = new Test(); test.start(); } } class producer implements Runnable { Test t; producer(Test t) { this.t = t; } public void run() { // TODO Auto-generated method stub Random r = new Random(); while (true) { int temp = Math.abs(r.nextInt() % 100); if (temp < 10) { // break; } t.add(temp); try { Thread.sleep(Math.abs(r.nextInt()) % 100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class consumer implements Runnable { Test t; consumer(Test t) { this.t = t; } public void run() { Random r = new Random(); while (true) { int temp = t.rem(); try { Thread.sleep(Math.abs(r.nextInt()) % 100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } |
|
返回顶楼 | |
发表时间:2008-03-20
请大家讨论讨论,谢了。
|
|
返回顶楼 | |