[concurrency-interest] Java 7 try-with-resources on j.u.c.locks.Lock

David Holmes davidcholmes at aapt.net.au
Wed Feb 23 06:01:05 EST 2011


Simple answer: no.

try-with-resources doesn't work that way. Ignoring that you'd have to define a close() method that does the unlock, you'd have to write something like:

void getAndLock(Lock l) {
  l.lock();
  return l;
}

  try (Lock l = getAndLock(lock)) {
    // do work
  }

to turn it into a resource-based statement.

This doesn't add to the useability or clarity in my view.

David Holmes
  -----Original Message-----
  From: concurrency-interest-bounces at cs.oswego.edu [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Dávid Karnok
  Sent: Wednesday, 23 February 2011 8:02 PM
  To: concurrency-interest at cs.oswego.edu
  Subject: [concurrency-interest] Java 7 try-with-resources on j.u.c.locks.Lock


  Hi! 


  Is there a chance the new Java 7 try-with-resources operator will be supported by the standard Lock interface and/or implementations? E.g., writing simply


  try (lock.lock()) {
     // do work
  }


  because lock() returns an AutoCloseable instance, or this kind of functionality will be deferred to 3rd party libraries with nice long static utility class names and method names?


  Regards,


  David Karnok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20110223/4df2cc3b/attachment-0001.html>


More information about the Concurrency-interest mailing list