- 浏览: 442879 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (158)
- J2SE (15)
- c/c++ (17)
- linux & ubuntu (20)
- js (18)
- algorithm (21)
- android (1)
- software (3)
- svn (1)
- db (6)
- other (19)
- css (5)
- go (1)
- html 5 (3)
- computer science (1)
- php (3)
- 创业 (8)
- EJB & jboss (1)
- TDD (1)
- jsp & servlet (2)
- http, tcp & ip (2)
- hibernate (1)
- json (1)
- 乐 (2)
- ps (2)
- netbeans (1)
- extjs (2)
- eclipse (4)
- 项目管理 (1)
- varnish (2)
- study abroad (1)
- python (1)
- erlang (1)
- math (1)
- shell (1)
- assembly (4)
- lucene (1)
- web (1)
- http (1)
- tcp & ip (1)
最新评论
-
yiguxianyun:
...
css li 不换行 -
stdayong:
...
netbeans 中使用 maven -
程序猿_星:
为啥会中文乱码啊
servlet 以 gzip 格式返回数据 -
huanhuan519:
感谢分享~
gdb 调试工具 -
heyl1234:
写过些js,对css还不熟。谢谢~
css li 不换行
java Lock & Condition, provide a similar, yet more extensive locking mechanism than synchronized,
Lock:
similar to the monitor lock used by synchronized, but more powerfull,
Condition:
bound to lock, provide methods similar to wait()/notify()/notifyAll() of Object,
------
class & interface
* Lock
interface of lock,
* ReentrantLock
basic implementation of Lock
*
* Condition
interface of condition,
*
* ReadWriteLock
interface, read/write version of Lock,
* ReentrantReadWriteLock
basic implementation of ReadWriteLock,
*
------
code
lock test:
package eric.j2se.concurrence.lockcondition; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Lock test * * @author eric * @date Aug 14, 2012 8:04:57 PM */ public class LockTest { private static int i; private static Lock lk = new ReentrantLock(); public static void test() { List<Thread> list = new ArrayList<Thread>(); int tcount = 3; // prepare threads for (int i = 0; i < tcount; i++) { list.add(new Thread(new TmpRunnable(), "t-" + i)); } // start threads for (int i = 0; i < tcount; i++) { list.get(i).start(); } } private static class TmpRunnable implements Runnable { @Override public void run() { lk.lock(); try { printTime("begin"); Thread.sleep(1000 * 1); // sleep a while, for test purpose printTime("end"); } catch (InterruptedException e) { e.printStackTrace(); } finally { lk.unlock(); } } } public static void printTime() { printTime(""); } /** * print thread name & time * * @param info * additional info to print */ public synchronized static void printTime(String info) { System.out.printf("%s:\t%d,\t,%d,\t%s\n", Thread.currentThread().getName(), ++i, System.currentTimeMillis() / 1000, info); } public static void main(String[] args) { test(); } }
condition test:
package eric.j2se.concurrence.lockcondition; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * Condition test * * @author eric * @date Aug 14, 2012 9:10:44 PM */ public class ConditionTest { private static int i; private static int bufSize = 26, size; private static char cStart = 97; private static char[] cbuf = new char[bufSize]; private static Lock lk = new ReentrantLock(); private static Condition cdEmpty = lk.newCondition(); private static Condition cdFull = lk.newCondition(); private static final int testInterval = 500; // interval of each step in milliseconds, for test /** * fill buffer, start when buffer is empty * * @throws InterruptedException */ public static void fill() throws InterruptedException { lk.lock(); try { while (size == bufSize) { // buffer is full, wait for condition empty cdEmpty.await(); } System.out.printf("\n\n------\nstart fill\n\n"); while (size < bufSize) { // buffer is not full now, fill it char c = (char) (cStart + size); cbuf[size++] = c; printTime("put: " + new Character(c)); Thread.sleep(testInterval); } cdFull.signal(); // buffer is full filled now, need to be read System.out.printf("\nbuffer full\n------\n\n"); } catch (Exception e) { e.printStackTrace(); } finally { lk.unlock(); } } /** * take from buffer & empty it, start when buffer is full * * @throws InterruptedException */ public static void take() throws InterruptedException { lk.lock(); try { while (size != bufSize) { // not full, wait for condition full cdFull.await(); } System.out.printf("\n\n------\nstart take\n\n"); for (int i = 0; i < bufSize; i++) { // now is full, take printTime("take: " + new Character(cbuf[i])); size--; Thread.sleep(testInterval); } cdEmpty.signal(); // buffer is empty now, need to be fill System.out.printf("\nbuffer empty\n------\n\n"); } catch (Exception e) { e.printStackTrace(); } finally { lk.unlock(); } } public static void test() { // thread to fill buffer Thread tFill = new Thread(new Runnable() { @Override public void run() { try { while (true) fill(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "t-fill"); // thread to take from buffer Thread tTake = new Thread(new Runnable() { @Override public void run() { try { while (true) take(); } catch (InterruptedException e) { e.printStackTrace(); } } }, "t-take"); tTake.start(); tFill.start(); } public static void printTime() { printTime(""); } /** * print thread name & time * * @param info * additional info to print */ public synchronized static void printTime(String info) { System.out.printf("%s:\t%d,\t,%d,\t%s\n", Thread.currentThread().getName(), ++i, System.currentTimeMillis() / 1000, info); } public static void main(String[] args) { test(); } }
read/write lock test:
package eric.j2se.concurrence.lockcondition; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * Read Write Lock test * * @author eric * @date Aug 14, 2012 10:34:08 PM */ public class ReadWriteLockTest { private static int i; private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private static Lock rlk = lock.readLock(); private static Lock wlk = lock.writeLock(); private static String data = ""; private static volatile long lastUpdate; // track last publish date /** * publish data, use write lock, * * @param newData */ public static void publish(String newData) { wlk.lock(); try { printTime("begin publish"); data = newData; lastUpdate = System.currentTimeMillis(); // modify last update date printTime("data:\n\t" + data); printTime("end publish"); } catch (Exception e) { e.printStackTrace(); } finally { wlk.unlock(); } } /** * view data, use read lock * * @param previousView * last viewed publish date * @return date of new publish, or -1 if no new publish */ public static long view(long previousView) { if (previousView < lastUpdate) { // new publish rlk.lock(); try { printTime("view data:\n\t" + data); } catch (Exception e) { e.printStackTrace(); } finally { rlk.unlock(); } return lastUpdate; } else { // no new publish printTime("no new publish yet"); return -1; } } public static void test() throws InterruptedException { Thread tPublish = new Thread(new Runnable() { @Override public void run() { while (true) { publish("hi, xxxxxx, data_" + i + "_xxxxxx"); try { Thread.sleep(1000 * 10); // update interval } catch (InterruptedException e) { e.printStackTrace(); } } } }, "t-publish"); // prepare view threads int tViewCount = 3; // count of view thread List<Thread> tViewList = new ArrayList<Thread>(); final List<Long> tLastView = new ArrayList<Long>(); // keep track of last viewed publish date for (int i = 0; i < tViewCount; i++) { final int _index = i; tViewList.add(new Thread(new Runnable() { @Override public void run() { while (true) { long _lastDate = view(tLastView.get(_index)); if (_lastDate > 0) { tLastView.set(_index, _lastDate); // update last viewed publish date, if has new publish } try { Thread.sleep(1000 * 4); // view interval } catch (InterruptedException e) { e.printStackTrace(); } } } }, "t-view-" + i)); tLastView.add(0L); } tPublish.start(); for (int i = 0; i < tViewCount; i++) { tViewList.get(i).start(); Thread.sleep(1000 * 5); // start interval of view threads } } public static void printTime() { printTime(""); } /** * print thread name & time * * @param info * additional info to print */ public synchronized static void printTime(String info) { System.out.printf("%s:\t%d,\t,%d,\t%s\n", Thread.currentThread().getName(), ++i, System.currentTimeMillis() / 1000, info); } public static void main(String[] args) throws InterruptedException { test(); } }
------
发表评论
-
java: lib & classpath
2014-08-21 10:14 1123classpath, is the path to searc ... -
Scan classes with specified annotation under specified package
2014-08-05 13:41 2348Scan classes with specified a ... -
http - simple server & client
2014-01-10 11:53 3497Simple http server & clien ... -
Java proxy
2012-12-07 18:14 1681Java proxy proxy, a design ... -
java udp 简单实现
2011-02-20 18:36 10208java udp 简单实现 ------ 代码: ... -
java 文件统计 工具类
2011-02-18 23:55 1396import java.io.File; import ... -
udp 简单使用
2010-10-15 01:11 1910udp 概述 user datagram pro ... -
String StringBuilder StringBuffer "..." 4种的效率
2010-08-31 21:53 1508"..." +"..." ... -
统计 某目录下 文件中的 字符
2010-08-30 06:14 1145统计给定目录下,所有文件中的 行数、空格数、数字个数: ... -
多线程 通过 公共数据区 协调运行
2010-08-30 04:52 718多线程 通过 公共数据区 协调运行: 每个线程打印同1 ... -
面试题 - 刁钻
2010-08-28 11:34 1185记录下 遇到的 一些 刁钻 面试题: 嘿嘿 i++ & ... -
JDK版本不一样导致WTP项目错误
2010-04-08 11:06 1041转载自:http://xucons.iteye.com/blo ... -
class init & object creation
2010-03-19 13:31 907class init & object creatio ... -
String 中 equals() vs ==
2009-08-16 18:02 1085String 中的 equals() 和 == : equa ...
相关推荐
Java Lock与Condition是Java并发编程中的重要概念,它们提供了比synchronized关键字更细粒度的控制,从而使得多线程程序的设计和管理更加灵活高效。本文将深入探讨ReentrantLock(可重入锁)和Condition的基本原理、...
Java中的Lock类与Condition类是Java并发编程的重要组成部分,它们为多线程环境下的同步提供了更为灵活和强大的控制。在JDK 1.5及之后的版本中,Lock类作为替代synchronized关键字的一种方式出现,提供了更精细的锁...
在Java并发编程中,`Condition`接口是Java并发包(java.util.concurrent)中的一个重要组成部分,它提供了比`synchronized`关键字更为精细的线程同步和唤醒机制。`Condition`允许我们创建多个独立的等待队列,每个...
在这个场景中,"生产者-消费者(lock和condition).zip"的文件内容可能包含一个使用Java语言实现的示例,它利用了`java.util.concurrent.locks.Lock`接口和`Condition`接口来解决这个问题。 `Lock`接口是Java并发库...
Java Lock接口是Java并发编程中一个重要的组成部分,它提供了一种更为灵活的锁机制,相比传统的`synchronized`关键字,Lock接口允许我们进行更细粒度的控制,包括可中断的锁等待、尝试获取锁以及定时等待等。...
Java多线程中ReentrantLock与Condition详解 ReentrantLock是Java多线程中一种高级的锁机制,它实现了Lock接口,提供了与synchronized相同的并发性和内存语义,但添加了一些特性,如锁投票、定时锁等候和可中断锁...
Java中的`ReentrantLock`是Java并发包`java.util.concurrent.locks`中的一个高级锁机制,它是可重入的互斥锁,具有与`synchronized`关键字相似的同步性,但提供了更多的灵活性和控制功能。本篇文章将深入探讨`...
为了确保线程安全和有效率的数据交换,Java提供了多种同步机制,其中包括Lock接口及其实现,如ReentrantLock。本项目通过Lock机制实现了生产者-消费者的解决方案。 Lock接口是Java并发库(java.util.concurrent....
使用Java Lock锁来实现流水线任务非常简单,我们可以使用ReentrantLock类来创建一个Lock对象,然后使用Condition对象来实现线程之间的通信。在上面的例子中,我们使用了三个Condition对象来实现三个线程之间的通信。...
在Java 1.5版本后,引入了`java.util.concurrent.locks`包,其中的`Lock`接口作为同步机制的新选择,弥补了`synchronized`关键字的一些局限性。下面将详细解释`Lock`接口以及与`synchronized`的区别。 `Lock`接口是...
Java中基于Lock的生产者消费者模型是使用Lock和Condition来实现线程同步和通信的。 在本示例中,我们使用了ReentrantLock和Condition来实现生产者消费者模型。ReentrantLock是Java中的一种可重入锁,它可以多次锁定...
本资源主要探讨了Java并发的新特性,特别是Lock锁和条件变量的使用。下面将详细阐述这两个概念以及如何在实际编程中应用它们。 Lock锁是Java并发库中的一个核心组件,位于java.util.concurrent.locks包下。相比传统...
Java中的Lock接口及其实现,如ReentrantLock,提供了更高级的线程通信机制,其中包括Condition接口。 Condition接口是Java并发包java.util.concurrent.locks的一部分,它允许我们创建特定于锁的等待集合。相比于...
Java 中,Condition 类是 Lock 接口的一个补充,它提供了一种机制来让线程等待某个条件的满足,然后继续执行。Condition 将同步监视锁方法(wait、notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与...
在Java多线程编程中,`Condition`是Java并发包`java.util.concurrent.locks`中的一个接口,它提供了比`Object`类中的`wait()`、`notify()`和`notifyAll()`更加灵活和强大的线程间通信机制。`Condition`与`Lock`配合...
在这个示例中,使用了Java的`java.util.concurrent.locks`包中的`Lock`接口和`Condition`接口来实现这一模式。`Lock`接口提供了比`synchronized`更精细的锁控制,而`Condition`接口则允许我们创建特定条件的等待队列...