免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2328 | 回复: 0
打印 上一主题 下一主题

discuss the limit on concurrent execution [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-28 17:11 |只看该作者 |倒序浏览
if your programe execute a high-cost-operation under multi-thread

and want to assign the number that the high-cost-operation  can be invoked at the same time...

actually "Thread.sleep()" can't do this, because that your programe is running under multi-thread environment

so check the following simple code, hope it is helpful






  1. public class ConcurrentControler {
  2.        
  3.         private int limit;
  4.         private int size = 0;
  5.         private Object lock = new Object();
  6.        
  7.        
  8.         public ConcurrentControler(){}
  9.        
  10.        
  11.         public int getLimit() {
  12.                 return limit;
  13.         }



  14.         public void setLimit(int limit) {
  15.                 this.limit = limit;
  16.         }

  17.         public void request() throws InterruptedException
  18.         {
  19.                 synchronized(lock)
  20.                 {
  21.                         while (size>=limit)
  22.                                 lock.wait();
  23.                         ++size;
  24.                 }
  25.                
  26.         }

  27.         public void finish()
  28.         {
  29.                 synchronized(lock)
  30.                 {
  31.                         --size;
  32.                         lock.notifyAll();
  33.                 }
  34.         }
  35.        
  36.         public static void main(String[] args) {
  37.                
  38.                 ConcurrentControler controler = new ConcurrentControler();
  39.                 controler.setLimit(3);
  40.                 new Thread( new SimpleTest(controler) ).start();
  41.                 new Thread( new SimpleTest(controler) ).start();
  42.                 new Thread( new SimpleTest(controler) ).start();
  43.                 new Thread( new SimpleTest(controler) ).start();
  44.                 new Thread( new SimpleTest(controler) ).start();
  45.                 new Thread( new SimpleTest(controler) ).start();
  46.                 new Thread( new SimpleTest(controler) ).start();
  47.                 new Thread( new SimpleTest(controler) ).start();
  48.                 new Thread( new SimpleTest(controler) ).start();
  49.                
  50.         }
  51.        
  52.         public static class SimpleTest implements Runnable
  53.         {

  54.                 private ConcurrentControler queue;
  55.                 public SimpleTest(ConcurrentControler obj)
  56.                 {
  57.                         this.queue = obj;
  58.                 }
  59.                


  60.                 public void run() {
  61.                        
  62.                         try{
  63.                                 queue.request();
  64.                         }catch(Exception ignored){ignored.printStackTrace();}
  65.                        
  66.                         System.out.println("execute...");

  67.                         //execute some kind of heavy method here

  68.                         try{
  69.                                 //Thread.sleep(3000);
  70.                         }catch(Exception ignored){ignored.printStackTrace();}

  71.                         queue.finish();
  72.                        
  73.                 }
  74.                
  75.         }

  76. }

复制代码

[ 本帖最后由 iamyess 于 2008-11-29 08:19 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP