- 浏览: 121394 次
- 性别:
- 来自: 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 847java.util.concurrent.CyclicBarr ... -
Delayed interface and Delay Queue
2009-04-22 17:42 1000/** * A standard implementati ... -
Count Down Latch example code
2009-04-22 10:38 1089Key point : 1) 1 task is co ... -
3 ways to break dead lock
2009-04-21 17:30 7031) supply special resources. ... -
Blocking Queue Usage
2009-04-20 11:21 7943 implementations: LinkedBlocki ... -
Re entrantLock usage
2009-04-15 17:15 1289a thread can be really interru ... -
new interrupt in java5
2009-04-15 12:08 633In Java 5, Thread.interrupt() i ... -
interrupt
2009-04-15 10:57 7631) Each thread has a boolean in ... -
Executor Service Usage
2009-04-14 18:18 861ExecutorService exec = Executor ... -
Thread Local usage
2009-04-14 17:46 760ThreadLocal usage – from Javado ... -
Timer TimerTask usage
2009-04-14 12:03 688Timer typical usage new Tim ... -
wait, notify及线程通讯机制
2009-02-26 22:42 8031) wait(), notify() 方法被调用的时候,只要 ... -
Java Thread programming basical knowledge
2009-02-26 22:40 923yield() : Give a hint to the th ... -
Count Down Latch explanation
2008-10-02 10:29 896Very important paragraph on how ... -
Scheduled Executor Service
2008-07-22 11:27 1060Executor can return Executor, E ... -
Executor usage
2008-07-22 11:04 842Executor is used to arrange thr ... -
Callable Usage
2008-07-22 10:24 884The important thing need to loo ...
相关推荐
This example demonstrates the usage of UCase and LCase.
Determining the required stack sizes for a software project is a crucial part of the development process. The developer aims to create a stable application, while not wasting resources. This ...
Usage of cause and location in the Digital Subscriber Signalling System No. 1 and the Signalling System No. 7 ISDN User Part
[1] and the 2015 fourth edition, re-titled Fowler's Dictionary of Modern English Usage, edited by Jeremy Butterfield, takes the same approach as the third edition (and only revised some entries)....
Usage of A4J.Usage of A4J.Usage of A4J.
ISO-TR 8550-3:2007 Guidance on the selection and usage of accept
ISO-TR 8550-2:2007 Guidance on the selection and usage of accept
ISO-TR 8550-1:2007 Guidance on the selection and usage of accept
It includes many concise examples of correct and incorrect usage, as well as coverage of the latest language features. The author also provides recommendations on use of the integrated development ...
Demonstrates usage of progress bar and list box controls. Its purpose is to get all the files from a specified folder while showing the progress completed.
网络化系留飞行平台在大型活动中的应用——以朝觐朝圣为例_On the Usage of Networked Tethered Flying Platforms for Massive Events -- Case Study Hajj Pilgrimage.pdf
This study was conducted to achieve two main objectives. Firstly it assesses the effect of low cost ... to decrease or remove the cost of ATMs usage and to introduce a good training to ATMs users.
This book presents recent investigations and enhancements in the field of Decision Support Systems, Web Data Mining, web engineering, with specific emphasis on development of Decision Support Sys- ...
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 ...
Configuration Examples for Displaying CPU Usage of the Device Using SNMP
ISO-TR 8550-1:2007 Guidance on the selection and usage of acceptance sampling systems for inspection of discrete items in lots - Part 1:Acceptance sampling - 完整英文版(43页).pdf
ISO-TR 8550-3:2007 Guidance on the selection and usage of acceptance sampling systems for inspection of discrete items in lots - Part 3:Sampling by variables - 完整英文版(45页).pdf
ISO-TR 8550-2:2007 Guidance on the selection and usage of acceptance sampling systems for inspection of discrete items in lots - Part 2:Sampling by attributes - 完整英文版(19页).pdf
With a detailed look at english grammar, style, diction, word formation, gender, social groups and scientific forms, this valuable reference work is ideal for students, writers, academicians and ...
ROR 6.6.4 - The Bit Operations 6.6.4.1 - TEST 6.6.4.2 - The Bit Test Instructions: BT, BTS, BTR, and BTC 6.6.4.3 - Bit Scanning: BSF and BSR 6.6.5 - The "Set on Condition" Instructions...