Read / Write Lock Syntactic Sugar?
I'm sure this must have been discussed already, but a search of Google and the JCP turned up nothing.
It occurred to me after reviewing the documentation for the Java 1.5 ReeentrantReadWriteLock from the java.util.concurrent package it could benefit from some syntactic sugar a la the for loop.
If you browse the Javadocs for ReentrantReadWriteLock, you'll find that the suggested idiom is to use a try finally block like so:
public Data get(String key) {What if you could write something like:
r.lock(); try { return m.get(key); } finally { r.unlock(); }
}
public Data get(String key) {instead?
lock (rwl.readLock()) {
return m.get(key);
}
}
I asked this question around the water-cooler (so to speak) at Terracotta and got some good responses - the most convincing was that maybe it was to ensure that Read Write locks looked different enough from traditional synchronized so that it's obvious to the reader that there is something different going on.
This seems a reasonable argument, but I don't completely buy it. I personally think several things about Java lead to it's eventual success:
- Simplicity - embodied in things like GC, single inheritance, lack of operator overloading, etc.
- Built-in thread primitives and synchronization. C++ finally got these with posix threads, but its never been as easy, IMO, as Java
- Ubiquity. Sun's mantra "Write once, run everywhere" is a great philosophy - even if it isn't 100% true it's pretty close
So therefore I think we deserve some (more) simplicity.
Do I think the first idiom has its place? Sure. It provides for composable synchronization, which is important if you want to decouple your synchronization from your call-stack - something which Java doesn't give you out of the box (but of course, you can always implement it yourself using the synchronization primitives).
Well if you have seen this before, let me know in the comments. Or let me know what you think of this syntax.
10 comments:
I believe the major advantage of the ReeentrantReadWriteLock is that it allows to break synchronization across several methods (lock in one place and unlock in a different one).
As for the different syntax, I think Neal Gafter's colusure proposal + some static utility classes will provide exact semantics but with nicer and more flexible syntax.
Eugene,
I agree, I am not proposing removing that idiom, just adding to it.
And agreed, maybe a closure syntax would allow us to write this..
The problem with adding a new keyword, or appending the synchronized capabilities, is it further complicates the language. The ReadWriteLock API is flexible enough to allow a myriad of different strategies, and the syntax really isn't that overwhelming.
Considering that closures seem to stand a good chance of making the Java 7 feature list, I don't really see a reason to add a specialized syntax to further complicated an already obfuscated language solely for the purpose of wrapping (and intrinsically limiting) a pre-existing API.
Why do you need new syntax to perform a simple and easy to understand operation? The try/finally pattern is used in many other scenarios, eg opening-using-closing ResultSets, so why should this one be any different?
I'm sure you can configure your IDE to write it in a single key press.
If the control abstraction part of closures are adopted into Java then you won't need to add a dedicated piece of syntax. See BGGA - http://www.javac.info/ - or FCM-JCA - http://docs.google.com/Doc?id=ddhp95vd_0f7mcns.
@Robert
Did you read my post or just the title? Because I thought I made it pretty clear that I felt new syntax would provide a more simple idiom. It's not about how easy it is physically to type, but the syntax that makes it easier on the eyes.
One could have argued that the for loop was just fine, the syntactic sugar added in Java 5 was unnecessary because it can all be done using the traditional syntax, or look even at the arguments for the new syntax for closures proposed by various people - you can do closures today w/Java, it's just not fun.
Is it necessary to add this change? No. Will it radically change my life and bring me nirvana? No. It just seemed that at least the discussion should be had - personally I avoid try / finally as much as possible - its unavoidable to be sure - but that doesn't mean I have to like it.
To those pointing out that closures will do the job for us, I agree with that point and look forward to the ratification of the requisite proposals.
I think definitely the way to handle this is via a control abstraction mechanism as defined in the various closure proposals. This is kind of a poster child example for control abstraction. Basically, having the ability to define new control abstractions lets you add things that appear to extend the Java language itself. Naturally, this is controversial, but would be immensely powerful. Personally, I like the JCA/FCM style better than the BGGA style.
Josh Bloch (not a fan of closures) has also proposed something called "automatic resource management blocks" as an alternative (not requiring the more general closure/control abstraction proposals).
You can find links to all this stuff on my Java 7 page.
hgkhk I like your blog. Thank you. They are really great . Ermunterung ++ .
Some new style Puma Speed is in fashion this year.
chaussure puma is Puma shoes in french . Many Franzose like seach “chaussure sport” by the internet when they need buy the Puma Shoes Or nike max shoes. The information age is really convenient .
By the way ,the nike max ltd is really good NIKE air shoes ,don’t forget buy the puma mens shoes and nike air max ltd by the internet when you need them . Do you know Nike Air Shoes is a best Air Shoes . another kinds of Nike shoes is better . For example , Nike Air Rift is good and Cheap Nike Shoes .the nike shox shoes is fitting to running.
Spring is coming, Do you think this season is not for Ugg Boots? maybe yes .but this season is best time that can buy the cheap ugg boots. Many sellers are selling discounted. Do not miss . Please view my fc2 blog and hair straighteners blog.
.thank you .
I like orange converse shoes ,I like to buy the cheap converse shoes by the internet shop . the puma shoes and the adidas shoes (or addidas shoes) are more on internet shop .i can buy the cheap nike shoes and cheap puma shoes online. It’s really convenient.
Many persons more like Puma basket shoes than nike air rift shoes . the Puma Cat shoes is a kind of Cheap Puma Shoes .
If you want to buy the Cheap Nike Air shoes ,you can buy them online. They are same as the Nike Air shoes authorized shop. Very high-caliber Air shoes and puma cat shoes . the cheap puma shoes as same as other.
池袋 風俗
渋谷 風俗
新宿 風俗
アダルトDVD
av 写真
大人のオモチャ
アダルトショップ
ペニス増大
電マ
TENGA
SM 通販
男性セクシー下着
メンズ下着
Tバック下着
大規模修繕
決済代行
SEO
SEO
fether felt flat
決済代行
FXとは
クレジットカード 申込
子猫
子犬
仔猫
アダルトショップ
アダルトグッツ
ゴールドカード審査
ダッチワイフ
Must i believeCheap RS Gold the primary idiom has its location? Sure. It provides pertaining to composable synchronization, that's critical if you want to decouple the Buy Guild wars 2 Goldsynchronization from the call-stack
Post a Comment