<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0" 
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    
    xmlns:content="http://purl.org/rss/1.0/modules/content/">

  <channel>
    <title>Java category of Luke Hutteman's public virtual MemoryStream</title>
    <link>http://www.hutteman.com/weblog/cat_java.html</link>
    <description>Get a complete rss feed (all categories) at &lt;a href="http://www.hutteman.com/weblog/rss.xml"&gt;http://www.hutteman.com/weblog/rss.xml&lt;/a&gt;</description>
    <dc:language>en-us</dc:language>
    <dc:creator>luke@hutteman.com</dc:creator>
    <dc:rights>Copyright 2008</dc:rights>
    <dc:date>2006-03-09T22:06:27-05:00</dc:date>
    <admin:generatorAgent rdf:resource="http://www.movabletype.org/?v=3.33" />
    <admin:errorReportsTo rdf:resource="mailto:luke@hutteman.com"/>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <sy:updateBase>2000-01-01T12:00+00:00</sy:updateBase>
    

    <item>
      <title>Why I Hate Frameworks</title>
      <link>http://www.hutteman.com/weblog/2006/03/09-245.html</link>
      <description>Benji Smith in Why I Hate Frameworks: So this week, we're introducing a general-purpose tool-building factory factory factory, so that all of your different tool factory factories can be produced by a single, unified factory. The factory factory factory will produce only the tool factory factories that you actually need, and each of those factory factories will produce a single... (112 words)</description>
      <guid isPermaLink="false">245@http://www.hutteman.com/weblog/</guid>
      <content:encoded><![CDATA[<p><a href="http://benjismith.net/">Benji Smith</a> in <a href="http://discuss.joelonsoftware.com/default.asp?joel.3.219431.12">Why I Hate Frameworks</a>:</p>
<blockquote>
So this week, we're introducing a general-purpose tool-building factory factory factory, so that all of your different tool factory factories can be produced by a single, unified factory. The factory factory factory will produce only the tool factory factories that you actually need, and each of those factory factories will produce a single factory based on your custom tool specifications. The final set of tools that emerge from this process will be the ideal tools for your particular project.
</blockquote>
<p>
A great read that not only shows why I too hate most frameworks, but also over-engineered code in general. YAGNI FTW! (via <a href="http://pluralsight.com/blogs/dbox/archive/2006/03/08/19865.aspx">Don Box</a>)
</p>
]]></content:encoded>
              <category>C#</category>
              <category>Java</category>
      
      <dc:date>2006-03-09T22:06:27-05:00</dc:date>
      
    </item>
    <item>
      <title>JVM hack implementations, and why I love Java</title>
      <link>http://www.hutteman.com/weblog/2005/06/03-227.html</link>
      <description> During a Java debug session today, I was investigating an exception and noticed that its cause was set to itself; effectively leading to an infinite stacktrace (or so I thought). When I added a watch on exception.getCause(), this returned null though, so I initially assumed this was a bug in my IDE (I'm using a beta release of IDEA... (579 words)</description>
      <guid isPermaLink="false">227@http://www.hutteman.com/weblog/</guid>
      <content:encoded><![CDATA[<p>
During a Java debug session today, I was investigating an exception and noticed
that its <code>cause</code> was set to itself; effectively leading to an infinite
stacktrace (or so I thought).
</p><p>
When I added a watch on <code>exception.getCause()</code>, this returned
<code>null</code> though, so I initially assumed this was a bug in my IDE (I'm using a beta release of <a href="http://www.jetbrains.com/idea/features/newfeatures.html">IDEA 5.0</a>).
However, a quick check of the source-code of <code>Throwable</code> disproved that initial assumption. As
it turns out, this problem is caused by a JVM hack implementation.
</p><p>
Take a look at the excerpt from <code>Throwable</code> below:

<blockquote><pre>
  /**
   * The throwable that caused this throwable to get thrown, or null if this
   * throwable was not caused by another throwable, or if the causative
   * throwable is unknown.  If this field is equal to this throwable itself,
   * it indicates that the cause of this throwable has not yet been
   * initialized.
   *
   * @serial
   * @since 1.4
   */
  private Throwable cause = this;

  public Throwable getCause() {
      return (cause==this ? null : cause);
  }

  public synchronized Throwable initCause(Throwable cause) {
      if (this.cause != this)
          throw new IllegalStateException("Can't overwrite cause");
      if (cause == this)
          throw new IllegalArgumentException("Self-causation not permitted");
      this.cause = cause;
      return this;
  }
</pre></blockquote>

It turns out that self-causation is the default state; indicating that the
<code>cause</code> has not yet been initialized. In other words, it's nothing
but a hack to save the developer from either adding a <code>causeInitialized boolean</code>
to <code>Throwable</code>, or (if they really felt they need to save those 4 bytes),
doing something like

<blockquote><pre>
  private static Throwable NOT_INITIALIZED =
             new Throwable("CAUSE NOT INITIALIZED", null);
    
  private Throwable cause = NOT_INITIALIZED;
</pre></blockquote>

Now from a runtime-perspective, this hack really doesn't matter as <code>cause</code>
is <code>private</code> and the <code>this</code> initial value is never returned
to the user. Unfortunately from a debug-perspective, this implementation is utterly
confusing.
</p><p>
So why do I still say I love Java? Because, unlike .NET for instance, I have access to the
source-code in moments like this. Language-wise, I actually prefer C# over Java. Library-wise, I also tend to favor the .NET implementations
over their Java counterparts. But with .NET, if something works somewhat differently
from what I would expect, I don't have the option of checking the actual implementation.
To me, this is a <b>BIG DEAL</b>. Having the source available is not only helpful in situations
like this, but it's also a tremendous aid when you truly want to grok an API.
</p><p>
And yes, I'm aware of the existence of Rotor and decompilers, but
<ol type='a'>
<li>Rotor is an incomplete implementation, lacking all of WinForms for instance.</li>
<li>Decompilation is not perfect and sometimes leads to awkward looking code.</li>
<li>Checking code from either Rotor or decompiling a class tends to not be integrated in VS.NET, so I'm forced to leave the IDE for tasks like this.</li>
<li>.NET decompilers typically show only one decompiled method at a time (the ones I've tried did this anyway) instead of the full
class, leading to a more fragmented view of the code.</li>
<li>Decompiled code lacks comments and sometimes even lacks the original variable-names.</li>
</ol>
</p><p>
There's been some dialog going on for over a year now about open-sourcing Java, but as far as I'm concerned, Java's already open-source enough. I just wish .NET would follow Java's example on this aspect as well. I don't need a license to allow me to change the source, but it sure would be nice if Microsoft would surprise me and include the source in .NET 2.0...
</p>]]></content:encoded>
              <category>C#</category>
              <category>Java</category>
      
      <dc:date>2005-06-03T23:15:59-05:00</dc:date>
            <comments>http://www.hutteman.com/weblog/2005/06/03-227.html#comments</comments>
      
    </item>
    <item>
      <title>C# Continuations useful? Hell yeah!</title>
      <link>http://www.hutteman.com/weblog/2005/04/26-224.html</link>
      <description>Last week, Sam Ruby posted a very interesting article on continuations for &quot;people older than dirt&quot; (a category which I, according to his definition, fall into). The topic became even more interesting when Don Box posted how you can use a very similar syntax in the next iteration of C#. Shortly thereafter, Cedric Beust posted that he wasn't convinced on... (530 words)</description>
      <guid isPermaLink="false">224@http://www.hutteman.com/weblog/</guid>
      <content:encoded><![CDATA[<p>Last week, Sam Ruby posted a very interesting <a href="http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons">article</a> on continuations for "people older than dirt" (a category which I, according to his definition, fall into). The topic became even more interesting when Don Box <a href="http://pluralsight.com/blogs/dbox/archive/2005/04/17/7467.aspx">posted</a> how you can use a very similar syntax in the next iteration of C#. Shortly thereafter, Cedric Beust <a href="http://beust.com/weblog/archives/000272.html">posted</a> that he wasn't convinced on the usefulness of this construct, and that a simple Java class without continuations pretty much does the same thing.
</p><p>
Despite having, like Cedric, mainly a Java background, I do think this construct will be useful, and would welcome it in the next release (that seems to be the established pattern anyway). Consider the example of trying to write a filtered Iterator. Using the C# 2.0, the code almost writes itself:

<pre>
    class Program
    {
        public static IEnumerable&lt;T> IteratorFilter&lt;T>(IEnumerable&lt;T> iterator,
                                                       Predicate&lt;T> predicate)
        {
            foreach (T value in iterator)
            {
                if (predicate(value))
                {
                    yield return value;
                }
            }
        }

        static void Main()
        {
            int[] values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
            foreach(int i in IteratorFilter(values, 
                    delegate(int v){ return v%2 == 0; }))
            {
                Console.WriteLine(i); // even numbers only
            }
        }
    }
</pre>

The same can definitely not be said for Java. My initial version took quite a bit longer to create than the C# version, and ended up looking like this:

<pre>
    public interface Predicate&lt;T> {
        boolean evaluate(T value);
    }

    public class IteratorFilter&lt;T> implements Iterator&lt;T> {
        private Iterator&lt;T> _iterator;
        private Predicate&lt;T> _predicate;
        private T _currentValue;
        private boolean _hasNext = true;
    
        public IteratorFilter(Iterator&lt;T> iterator, Predicate&lt;T> predicate) {
            _iterator = iterator;
            _predicate = predicate;
            skipFiltered();
        }
    
        private void skipFiltered() {
            while (_iterator.hasNext()) {
                _currentValue = _iterator.next();
                if (_predicate.evaluate(_currentValue)) {
                    return;
                }
            }
            _hasNext = false;
        }
    
        public boolean hasNext() {
            return _hasNext;
        }
    
        public T next() {
            if (!_hasNext) {
                throw new NoSuchElementException();
            }
            T result = _currentValue;
            skipFiltered();
            return result;
        }
    
        public void remove() {
            throw new UnsupportedOperationException();
        }
    
        public static void main(String[] args) {
            Integer[] values = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
            Iterator&lt;Integer> i = new IteratorFilter&lt;Integer>(
                    Arrays.asList(values).iterator(), 
                    new Predicate&lt;Integer>() {
                        public boolean evaluate(Integer value) {
                            return value % 2 == 0;
                        }
                    });
    
            while(i.hasNext()) {
                System.out.println(i.next());
            }
        }
    }
</pre>

Ouch! Without continuations, there's no option to just iterate over the elements and filter out the unwanted values, like I did in the C# implementation. Instead, I'm forced to create a class that implements the <code>Iterator</code> interface, store <code>_currentValue</code> and <code>_hasNext</code> as fields, and create a <code>skipFiltered()</code> method to skip unwanted values.
</p><p>
I am aware that C#'s <code>IEnumarable</code> isn't exactly the same as a Java <code>Iterator</code>; in a way it's more like a subset of Java's <code>Collection</code>. However, creating a Java function that creates a new <code>Collection</code> (containing the filtered subset of the original) wouldn't be quite the same as it would create a second Collection and have all filtering done up-front, instead of just an iterator whose elements are fetched from the original in an on-demand fashion.
</p><p>
Disclaimer: I am by no means an expert on the new constructs in C# 2.0 or Java 5.0 (far from it) - if I've overlooked something, please let me know in the comment section below.
</p>]]></content:encoded>
              <category>C#</category>
              <category>Java</category>
      
      <dc:date>2005-04-26T00:41:59-05:00</dc:date>
            <comments>http://www.hutteman.com/weblog/2005/04/26-224.html#comments</comments>
      
    </item>
    <item>
      <title>JavaOne Report: The MemoryModel and Concurrency Utilities</title>
      <link>http://www.hutteman.com/weblog/2004/07/04-199.html</link>
      <description>For me, the most interesting sessions of the conference were regarding the new MemoryModel and Concurrency Utilities. Having recently done quite a bit of threading code, it's clear that JDK 5.0 will vastly improve on the current primitives synchronized, wait and notify (which will, of course, still be supported in JDK 5.0, though they probably won't be used as much).... (875 words)</description>
      <guid isPermaLink="false">199@http://www.hutteman.com/weblog/</guid>
      <content:encoded><![CDATA[<p>For me, the most interesting sessions of the conference were regarding the new MemoryModel and Concurrency Utilities. Having recently done quite a bit of threading code, it's clear that JDK 5.0 will vastly improve on the current primitives <code>synchronized</code>, <code>wait</code> and <code>notify</code> (which will, of course, still be supported in JDK 5.0, though they probably won't be used as much).
</p><p>
The new memory model will ensure that things will work much more intuitively than they currently do. The double-checked locking idiom for instance is broken right now due to allowed statement re-ordering and caching of variables in registers (this, by the way, is not just a Java-issue, it's also broken in languages like C++ and C#). Now statement reordering (either by the compiler or by the processor) and caching of values in registers are extremely important features for performance so they cannot simply be turned off to make stuff like this work. Using new definitions for <code>volatile</code> and <code>synchronized</code>, stating when they should read/write to memory (as opposed to using cached values), things will work much better and more intuitively in JDK 5.0.
</p><p>
The new concurrency utilities package (based on Doug Lea's threading libraries) contains a lot of new Classes and Interfaces to make writing multi-threaded code much easier. The idea behind this package is to do for threading what the collections framework did for collections. When they first mentioned this, I was rather skeptical to say the least. At the end of the session though, I believed they might just pull it off.
</p><p>
The concurrent utilities package will implement thread pooling through <code>Executor</code>s. The <code>Executor</code> interface consists of a single "<code>void execute(Runnable command)</code>" method, so is effectively just "something that runs <code>Runnable</code>s". This can be a single threaded worker, a regular thread pool, a scheduled thread pool or even a custom implementation. There's an <code>Executors</code> class which has factory methods to create many commonly used types of <code>Executor</code>s. The actual type returned by these factory methods is typically an "<code>ExecutorService</code>": a sub-interface of <code>Executor</code> with added methods to, among other things, manage termination of the pool.
</p><p>
JDK 5 also introduces concurrent collections. These collections achieve thread safety while still allowing certain operations to occur concurrently. While using the <code>synchronized</code> keyword often requires placing a lock on the entire collection during iteration, concurrent collections will allow multiple operations to overlap each other. The new <code>ConcurrentHashMap</code> class for instance allows overlap of multiple reads, reads over writes and even up to 15 overlapping writes (it will be interesting to check out the code to see how they pulled off the concurrent writes). Also, iteration over a <code>ConcurrentHashMap</code> will never throw a <code>ConcurrentModificationException</code>. 
</p><p>
Another example of a concurrent collection is the <code>CopyOnWriteArrayList</code> - this class is optimized for cases where iteration is much more frequent than insertion or removal. It is for instance ideal for EventListeners. As the name suggests, every write causes the current collection data to be copied, thus guaranteeing that no <code>ConcurrentModificationException</code>s will ever be thrown during iteration - if a collection gets changed while another thread is iterating, that other thread will continue seeing the original (non-changed) data.
</p><p>
There are also a number of lower level concurrency utils like <code>Lock</code>s and <code>Condition</code>s that will be introduced in JDK 5.0. A <code>Lock</code> allows for functionality similar to that provided by the <code>synchronized</code> keyword, but without forcing you to lock and unlock within a single block of code. <code>Lock</code>s also have added options like the ability to check a <code>Lock</code> without waiting infinitely, or timing out <code>Lock</code>s. 
</p><p>
The <code>ReentrantLock</code> class implements a reentrant mutual exclusion lock with the same semantics as built-in monitor locks (<code>synchronized</code>), but with extra features like the ability to interrupt a thread waiting to acquire a lock, specifying a timeout while waiting for a lock, polling for lock availability, and support for multiple wait-sets per lock via the "Condition" interface. <code>ReentrantLock</code> outperforms built-in monitor locks in most cases, but is slightly less convenient to use as it requires a <code>finally</code> block to release lock, like

<blockquote><pre>
Lock lock = new ReentrantLock();
...
lock.lock();
try {
    ...
} finally {
    lock.unlock();
}
</pre></blockquote>
</p><p>
A <code>Condition</code> is the abstraction of <code>wait</code>-<code>notify</code>. You can get a <code>Condition</code> object from an existing <code>Lock</code> object, and subsequently call <code>await()</code> and <code>signal()</code> on them. This allows you to for instance have multiple <code>Condition</code> objects against a single <code>Lock</code>, instead of having to use <code>notifyAll</code> to wake up all threads, only to put all but one of them back into a wait-state.
</p><p>
There are many other useful classes and interfaces like <code>ReadWriteLock</code>, <code>ReentrantReadWriteLock</code>, <code>Semaphore</code>, <code>CountdownLatch</code>, etc.
</p><p>
And last but not least, JDK 5.0 will introduce Atomic Variables: classes that support atomic operations like "compare-and-set" and "get,set-and-arithmetic". At runtime, the JVM will use the best available implementation of this functionality, depending on the platform it runs on. So this may internally be implemented using a lock, or may use a native contruct (if supported by the processor) to do these kinds of atomic operations. An example of an atomic variable class is "<code>AtomicInteger</code>". which will be useful for things like counters and sequence numbers.
</p>]]></content:encoded>
              <category>Java</category>
      
      <dc:date>2004-07-04T00:56:33-05:00</dc:date>
            <comments>http://www.hutteman.com/weblog/2004/07/04-199.html#comments</comments>
      
    </item>
    <item>
      <title>JavaOne notes</title>
      <link>http://www.hutteman.com/weblog/2004/07/04-198.html</link>
      <description>Sun's studio creator has interesting collaboration-options built in: it comes with an instant messenger pane that has code-options, enabling things like copy'n paste of code (which will be sent with code-highlighting), doing code-completion in the IM window, and even sending entire files, which can subsequently be edited synchronously between both parties - very cool stuff. It was announced that &quot;There... (526 words)</description>
      <guid isPermaLink="false">198@http://www.hutteman.com/weblog/</guid>
      <content:encoded><![CDATA[<p>Sun's studio creator has interesting collaboration-options built in: it comes with an instant messenger pane that has code-options, enabling things like copy'n paste of code (which will be sent with code-highlighting), doing code-completion in the IM window, and even sending entire files, which can subsequently be edited synchronously between both parties - very cool stuff.</p>

<p>It was announced that "There is no application in the gaming space today that cannot be written in Java". I find this somewhat hard to believe to say the least. The first example they gave to show the power of Java in gaming was a third person shooter that was <i>partially</i> written in Java (that is, none of the actual rendering used Java) not quite what I'd call overwhelming evidence. The second game they showed was 100% Java though and can be downloaded from Java.net. It looked pretty good, but only showed a 3-D view walking through the environment. There were no other characters, let alone any actual action. I don't think managed code is quite at a point yet where it can be used to create games like HalfLife 2, but I'm willing to be proven wrong...</p>

<p>The also showed the phantom gaming service hardware. This is a system, which <i>supports</i> Java (which I assume means it can do Java, but can do other stuff as well), was created by the co-creator of Microsoft's XBox. It is not a regular console, but instead can download games on demand from the network. The hardware will be free with a two-year subscription.</p>

<p>JDK 5 will have less dependence on command-line parameters and instead auto-tune itself for optimal performance. It will use your machine's configuration to come up with an initial config, and dynamically change settings at runtime. This is great news as tweaking stuff like NewSize, SurvivorRatio, etc. can be a major pain. This feature will NOT be turned on by default on windows though (it will be on other platforms) - use the concurrentGC option to enable it on windows.</p>

<p>Lucene is a Java search-engine I'll definitely have to check more into. I think there's a .NET port for this project as well (though I don't know how up-to-date it is with the master Java project) so I may even be able to use it in SharpReader.</p>

<p>The Groovy scripting language is, well, groovy. Using a very powerful and highly condensed syntax, you can write things in Groovy that would take ten times the number of lines in Java. Closures are especially cool. Performance is currently about 20-90% of that of Java (closer to 20% when using dynamic typing (which I guess uses reflection for every method call), closer to 90% for static typing (which takes away from the easy of use of Groovy)). This makes me somewhat sceptical of how useful this language really is right now for production systems. I'm sure they'll be able to beef up performance at some point, but until they do, I think I'd rather spend some extra time and learn Python instead if I want to use a scripting language (although I have to admit that the Java support in Groovy is pretty nice...)</p>]]></content:encoded>
              <category>Java</category>
      
      <dc:date>2004-07-04T00:37:56-05:00</dc:date>
            <comments>http://www.hutteman.com/weblog/2004/07/04-198.html#comments</comments>
      
    </item>


  </channel>
</rss>
