{"id":263,"date":"2007-08-21T13:50:40","date_gmt":"2007-08-21T18:50:40","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=263"},"modified":"2007-08-21T16:15:22","modified_gmt":"2007-08-21T21:15:22","slug":"javas-threading-tools","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/263","title":{"rendered":"Java&#8217;s Threading Tools"},"content":{"rendered":"<p>There is a lot to like about the threading model in Java. It's easy to make threads, it's decently fast, and it's stable. One thing that I <b>wished<\/b> they'd put into the JVM - or at least into <tt>javac<\/tt> like all the other 1.5\/1.6 compiler 'hacks', is the idea that synchronization on a setter can be controlled by the instance variable itself. What I'm asking for is an automatic way to have <tt>javac<\/tt> create thread-safe setters and getters by simply naming the ivar.<\/p>\n<p>The most obvious way of making a thread-safe setter is this:<\/p>\n<pre><code>\n<span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #00AC00;\">\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;standard&nbsp;setter&nbsp;for&nbsp;the&nbsp;temperature\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;of&nbsp;this&nbsp;instance.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/<\/span><span style=\"color: #000;\">\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">public<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">synchronized<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">void<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">setTemp<\/span><span style=\"color: #000;\">(<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">aTemp<\/span><span style=\"color: #000;\">)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">&nbsp;=&nbsp;<\/span><span style=\"color: #000;\">aTemp<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/span><\/code><\/pre>\n<p>but the downside is that this means that if this instance is in a highly threaded environment, or there are other methods that take a significant amount of time to process, the ability to 'set' the temperature is dependent on the other <tt>synchronized<\/tt> methods on this class.<\/p>\n<p>Sure, you can make an object to lock on and it might look like this:<\/p>\n<pre><code>\n<span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">&nbsp;=&nbsp;<\/span><span style=\"color: #000;\">0<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #000;\">String<\/span><span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #000;\">_tempLock<\/span><span style=\"color: #000;\">&nbsp;=&nbsp;<\/span><span style=\"color: #EF2F91;\">\"TempLock\"<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #00AC00;\">\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;setter&nbsp;for&nbsp;the&nbsp;temperature&nbsp;uses&nbsp;an\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;additional&nbsp;ivar&nbsp;-&nbsp;a&nbsp;String,&nbsp;to&nbsp;protect&nbsp;multiple\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;threads&nbsp;changing&nbsp;the&nbsp;value&nbsp;at&nbsp;once.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/<\/span><span style=\"color: #000;\">\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">public<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">void<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">setTemp<\/span><span style=\"color: #000;\">(<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">aTemp<\/span><span style=\"color: #000;\">)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">synchronized<\/span><span style=\"color: #000;\">&nbsp;(<\/span><span style=\"color: #000;\">_tempLock<\/span><span style=\"color: #000;\">)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">&nbsp;=&nbsp;<\/span><span style=\"color: #000;\">aTemp<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/span><\/code><\/pre>\n<p>but then you have all these <i>extra<\/i> ivars that you have no real need for and no need to really put into the setter other than as a thread-safety mechanism.<\/p>\n<p>Some will argue that if I had used a <tt>Double<\/tt> and not a <tt>double<\/tt> I could synchronize on the ivar itself. But it's not really safe doing that. You'd have to be careful of the <tt>null<\/tt> value and then what if you changed the reference within the <tt>synchronized<\/tt> block - you'd no longer be protected. No, there needs to be the ivar itself - allowing <tt>null<\/tt>, and then some mechanism to ensure thread safety on a more finer-grained level than locking the entire instance.<\/p>\n<p>They need to have something that does the <b>obvious<\/b> things for an ivar of a given type. You want to save typing? Make something like this:<\/p>\n<pre><code>\n<span style=\"color: #000;\">&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #00AC00;\">\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;These&nbsp;create&nbsp;the&nbsp;setter&nbsp;and&nbsp;getter&nbsp;for&nbsp;the&nbsp;ivar\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;'_temp'&nbsp;because&nbsp;the&nbsp;need&nbsp;to&nbsp;lock&nbsp;is&nbsp;implied\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;on&nbsp;the&nbsp;setter&nbsp;and&nbsp;typically&nbsp;unnecessary&nbsp;on&nbsp;the\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;getter&nbsp;(unless&nbsp;it's&nbsp;a&nbsp;long).\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/<\/span><span style=\"color: #000;\">\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">public<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">threadsafe<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">setTemp<\/span><span style=\"color: #000;\">(<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">aTemp<\/span><span style=\"color: #000;\">)&nbsp;:&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">public<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">threadsafe<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">getTemp<\/span><span style=\"color: #000;\">()&nbsp;:&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #00AC00;\">\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;These&nbsp;versions&nbsp;would&nbsp;create&nbsp;'protected'&nbsp;versions\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;of&nbsp;the&nbsp;getter&nbsp;and&nbsp;setter&nbsp;methods.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/<\/span><span style=\"color: #000;\">\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">protected<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">threadsafe<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">setTemp<\/span><span style=\"color: #000;\">(<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">aTemp<\/span><span style=\"color: #000;\">)&nbsp;:&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">;\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">protected<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">threadsafe<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">getTemp<\/span><span style=\"color: #000;\">()&nbsp;:&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">;\n\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #00AC00;\">\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;This&nbsp;creates&nbsp;both&nbsp;setter&nbsp;and&nbsp;getter&nbsp;for&nbsp;the&nbsp;ivar\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;'_temp'&nbsp;because&nbsp;the&nbsp;structure&nbsp;to&nbsp;create&nbsp;the&nbsp;two\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;is&nbsp;clear&nbsp;from&nbsp;this&nbsp;information&nbsp;alone.\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/<\/span><span style=\"color: #000;\">\n&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000C4;\">public<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">threadsafe&nbsp;accessors<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">Temp<\/span><span style=\"color: #000;\">&nbsp;:&nbsp;<\/span><span style=\"color: #0000C4;\">private<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #0000C4;\">double<\/span><span style=\"color: #000;\">&nbsp;<\/span><span style=\"color: #000;\">_temp<\/span><span style=\"color: #000;\">;\n<\/span><\/code><\/pre>\n<p>The information to build the code for the setter and getter is clearly specified in the above examples. In the first set, the ivar <tt>_temp<\/tt> is clearly private, a <tt>double<\/tt> and the names of the methods to build are clear. There's even the argument type - which is really redundant, if you think about it.<\/p>\n<p>A possibly even simpler solution would be to combine the definition of the ivar and the scope of the setter and getter in one statement - like the last example. That way, you would know the ivar's name, type, scope, and then the scope of the setter and getter. You could even allow one to use a PropertyChange system with listener registration, etc. This is not that hard, and I've used it in <b>tons<\/b> of classes. The clutter it'd save would be <i>far far more<\/i> than the <i>collapsed<\/i> for-loop.<\/p>\n<p>Anyway, today I did a lot of the 'mutex' variables because I needed to remove as many <tt>synchronized<\/tt> methods as possible on a class I was working with. Virtually every one of them was a setter, and it was annoying to think that they made a goofy <i>collapsed<\/i> for-loop in 1.5, but they left this little nugget out.<\/p>\n<p>Amazing. I think it shows to me that defensive programming is not heavily exercised by the Java Developers. Otherwise, they too, would be sick of making thread-safe but not <i>lock greedy<\/i> accessor methods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a lot to like about the threading model in Java. It&#8217;s easy to make threads, it&#8217;s decently fast, and it&#8217;s stable. One thing that I wished they&#8217;d put into the JVM &#8211; or at least into javac like all the other 1.5\/1.6 compiler &#8216;hacks&#8217;, is the idea that synchronization on a setter can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":0,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}