Re: Using ReentrantLock
Thanks guys,
Please don't get the impression I am looking for someone to do my
homework -- rather, I'm very unsure of my understanding of this from
the docs, and, like everyone else here, I need to write solid code; I
am hoping tod exactly what I have received here, a solid review and
commentary from my peers.
It seems based on everyone's comments (please correct me if I am
wrong), that all I really need do is make lockObject a static variable
from the calling thread. This way, all see it, and there is only one
copy that all threads called would be working from. This seems simple,
solid and robust. For example:
class MyCallingClass{
static ReentrantLock lockObject = new ReentrantLock(false);
myCalledThread=newMyCalledThread(lockObject);
myCalledThread.t.start();
class MyCalledThread{
Thread t;
ReentrantLock lockObject;
MyCalledThread(ReentrantLock lockObject){
this.lockObject=lockObject;
}
public void run(){
try {
lockObject.lock();
output.write(merchLinklengthBytes(response));
output.write(headerControl);
output.write(response.getBytes());
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (null != lockObject) {
lockObject.unlock();
}
}
}
}
}
Does anyone see any glaring problem - grotesquely stupid mistake I am
making here. Again, I'm just very unsure on this approach to doing
this, and trying to keep it as simple & robust as possible. Based on
your critiques, I think this accomplishes that end. But in X decades
of writing code, I can;t think of ANYTIME, ANYTHING, has ever run as I
thought it would on the first pass! Thanks, RVince