首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁Java ReentrantLock”︁的源代码
←
Java ReentrantLock
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
以前在做2D游戏的时候,对于游戏画面的刷新,实际上都是采用一个死循环去弄的,像这样: <source lang="c"> while(true){ //... update(); sleep(10); } </source> =队列与锁= 后来在做一个socket的后端时,也情不自禁的这样使用了。一直在考虑有没有更好的办法,无奈接触的少,可遇而不可求。最近又遇到了相同的问题:做一个短信发送的队列。先上代码: <source lang="java"> ReentrantLock lock = new ReentrantLock(); Condition notEmpty = lock.newCondition(); final ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>(); void addTask(String t){ lock.lock(); try{ System.out.println("+" + t); queue.add(t); notEmpty.signal(); } finally{ lock.unlock(); } } void handle(){ while(true){ lock.lock(); try{ final String t = queue.poll(); if(t == null){ notEmpty.await(); } else{ new Thread(new Runnable(){ @Override public void run() { System.out.println("->" + t); try { Thread.sleep(new Random().nextInt(5000)); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } catch (InterruptedException e) { e.printStackTrace(); } finally{ lock.unlock(); } } } </source> 主函数: <source lang="java"> public static void main(String [] args){ final QueueTest test = new QueueTest(); Thread addThread = new Thread(new Runnable(){ @Override public void run() { for(int i = 0; i < 100; i++){ test.addTask(new Random().nextInt(10000) + ""); try { Thread.sleep(new Random().nextInt(100)); } catch (InterruptedException e) { e.printStackTrace(); } } } }); addThread.start(); test.handle(); } </source> 这里使用了一个信号,当队列为空之后,线程就会等待,当调用了add方法添加之后,释放了信号,处理线程唤醒。不清楚是否有更好的做法。权且记录之。 [[Category:Concurrency]]
返回
Java ReentrantLock
。