- 浏览: 128471 次
- 性别:
- 来自: Singapore
-
文章分类
- 全部博客 (112)
- Tiger Thread (18)
- Perforce (6)
- Spring (5)
- maven (3)
- log4j (3)
- Quartz Scheduler (4)
- unix and linux (12)
- hibernate (3)
- Enum (1)
- Futures and Options (1)
- Market Making (2)
- Java Basic (11)
- Tibco EMS (3)
- F I X message (5)
- equity derivative (2)
- Sybase (3)
- XML (1)
- JUnit (2)
- J A X B 2.0 (1)
- N I O (1)
- windows batch file (1)
- Cruise Control (1)
- util Class (5)
- ant (1)
- JMS (1)
- profiling (0)
- Sql Server (6)
- GXT (2)
- eclipse (1)
- Generics (1)
- Tibco RV (3)
- Autosys (0)
- Message (1)
最新评论
-
houzhe11:
<property name="proxyTa ...
AOP usage -- BeanNameAutoProxyCreator usage
//: concurrency/waxomatic2/WaxOMatic2.java
// Using Lock and Condition objects.
package concurrency.waxomatic2;
import static java.lang.System.out;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Car
{
//car has a lock, which is used when
//1) you are waxing the car
//2) the wax on the care disappear
//And obviously, these operations are exclusive to each other.
//
//Another very important thing about Lock object is:
//Pay attention to the format of lock()/unlock() usage as below:
//
//lock.lock()
//try
//{
// ... ...
//}
//finally
//{
//lock.unlock();
//}
private Lock lock = new ReentrantLock();
//in java1.4 and previous, we just wait() and notify()/notifyAll() on some object, generally "this"
//But in java1.5 concurrency, we will create a condition object, derived from Lock object.
//And all the await() and signal()/signalAll() are on this condition object.
private Condition condition = lock.newCondition();
private boolean waxOn = false;
/**
* you are waxing the car
*/
public void waxed()
{
lock.lock();
try
{
//-----location (1) where waitForWaxing() should be moved.
waxOn = true; // Ready to buff
condition.signalAll();
}
finally
{
lock.unlock();
}
}
/**
* the wax on the car disappeared
*/
public void buffed()
{
lock.lock();
try
{
//------location (2) where waitForBuffing() should be moved.
waxOn = false; // Ready for another coat of wax
condition.signalAll();
}
finally
{
lock.unlock();
}
}
/**
* I think this method is redundant. Because the 1st step if you want to wax a car, you need to wait for waxing.
* What I mean is, the condition.await() wrapped with while loop should be moved to location (1) of waxed() method.
* @throws InterruptedException
*/
public void waitForWaxing() throws InterruptedException
{
lock.lock();
try
{
while (waxOn == false) //wait must be surrended by while(condition)! Don't forget this!!
condition.await();
}
finally
{
lock.unlock();
}
}
/**
* quite similiar to the above method, should be moved to buffed() of location (2)
* @throws InterruptedException
*/
public void waitForBuffing() throws InterruptedException
{
lock.lock();
try
{
while (waxOn == true) //wait must be surrended by while(condition)! Don't forget this!!
condition.await();
}
finally
{
lock.unlock();
}
}
}
class WaxOn implements Runnable
{
private Car car;
public WaxOn(Car c)
{
car = c;
}
public void run()
{
try
{
while (!Thread.interrupted())
{
out.println("Wax On! ");
TimeUnit.MILLISECONDS.sleep(200);
car.waxed();
car.waitForBuffing();
}
}
catch (InterruptedException e)
{
out.print("Exiting via interrupt");
}
out.print("Ending Wax On task");
}
}
class WaxOff implements Runnable
{
private Car car;
public WaxOff(Car c)
st
发表评论
-
javadoc for Cyclic Barrier
2009-04-24 12:48 914java.util.concurrent.CyclicBarr ... -
Delayed interface and Delay Queue
2009-04-22 17:42 1060/** * A standard implementati ... -
Count Down Latch example code
2009-04-22 10:38 1165Key point : 1) 1 task is co ... -
3 ways to break dead lock
2009-04-21 17:30 7771) supply special resources. ... -
Blocking Queue Usage
2009-04-20 11:21 8403 implementations: LinkedBlocki ... -
Re entrantLock usage
2009-04-15 17:15 1331a thread can be really interru ... -
new interrupt in java5
2009-04-15 12:08 670In Java 5, Thread.interrupt() i ... -
interrupt
2009-04-15 10:57 8381) Each thread has a boolean in ... -
Executor Service Usage
2009-04-14 18:18 913ExecutorService exec = Executor ... -
Thread Local usage
2009-04-14 17:46 821ThreadLocal usage – from Javado ... -
Timer TimerTask usage
2009-04-14 12:03 729Timer typical usage new Tim ... -
wait, notify及线程通讯机制
2009-02-26 22:42 8541) wait(), notify() 方法被调用的时候,只要 ... -
Java Thread programming basical knowledge
2009-02-26 22:40 1097yield() : Give a hint to the th ... -
Count Down Latch explanation
2008-10-02 10:29 979Very important paragraph on how ... -
Scheduled Executor Service
2008-07-22 11:27 1133Executor can return Executor, E ... -
Executor usage
2008-07-22 11:04 927Executor is used to arrange thr ... -
Callable Usage
2008-07-22 10:24 954The important thing need to loo ...
相关推荐
In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have ...
condition is known to exist between AcpiWalkNamespace and the Load/Unload ASL operators and is still under investigation. Restructured the AML ParseLoop function, breaking it into several ...
Use the EXPAND command to expand and copy the DMDRVR.BI_ file from Disk 6 to your DOS directory and the XBIOS.OV_ file from Disk 6 to your root directory. 2. Name the files DMDRVR.BIN and XBIOS....
Deprecated functions and types of the C API Deprecated Build Options Removed API and Feature Removals Porting to Python 3.6 Changes in ‘python’ Command Behavior Changes in the Python API ...
This a simple app to fix SystemUpdateService wakelock which keeps your favorite Android device awake and drains much battery. ROOT REQUIRED Tested on the following condition: Hardware: OnePlus One A...
5.14 The Aggregation Function and the Scalar Subquery. . . . . . . 136 5.15 The Row Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 5.16 The Table Expression . . . . . . ....