Category: java

  • Don’t Yield to pressure?

    or: does Thread.yield have its place in todays Java programs? I was profiling a rather old legacy codebase (since the first rule of performance optimization is “profile it” with the close second of “have clear goals in mind” – but that’s an other post) and – after optimizing the first few hotspots, Thread.yield appeared at…

  • Using TarInputStream from Java

    Recently I had to parse trough a bunch of logs, scattered in subdirectories and different types of archives (tar, bz and gz). My first thought was of course Perl (since it is the language for parsing quasi-freeform text), however I didn’t have “streaming” implementation for the archive modules, which in my case was very important,…

  • Careful with that axe^H^H^H static, Eugene!

    An other instance from the “bugs which will bite you” series: public class TestStatic { static class Foo { static Foo instance = new Foo(); static String name = Foo.class.getName(); public Foo() { System.err.println("Hello, my name is " + name); } } public static void main(String[] args) { System.err.println("Your name is what?n" + "Your name…

  • Spot the error

    C++ compilers are notorious for giving error messages which point you in the wrong direction. However even simpler languages can have issues. Can you spot the real problem with the java code below? There is a comma missing between the parameters! Nice, ey? (To be fair, on the sidebar Eclipse shows two errors, one of…

  • Why can’t I see the stacktrace under Java?

    I recently had a situation where Log4j wasn’t outputting the stacktrace of the logged exceptions. While I’m not sure that the following is the actual explanation, it seems very plausible (since the program was running Java 5). Quote from the Java 5 release notes: The compiler in the server VM now provides correct stack backtraces…

  • Hidden Java concurrency bugs

    Question: how can the following line of Java code throw the exception shown below? priv.addAll(common); Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at java.util.ArrayList.toArray(Unknown Source) at java.util.ArrayList.addAll(Unknown Source) at TestConcurrentList$ConsumeThread.run(TestConcurrentList.java:34) Answer: because of bad synchronization. The scenario is the following: one thread is continuously modifying the list “common” while the second thread tries to…

  • Java tips&tricks

    A couple of quick/short Java tips: The Collections.unmodifiable* just wrap the original object in a facade class which throws UnsupportedOperationException when you invoke operations that would modify it (like add or remove), but the original collection still remains mutable (and the mutations are reflected in facade object). This might be obvious to some, but not…

  • Is Java slower than C? (and does it matter?)

    Via daniel’s blog (the creator of curl) I arrived to this page: why the Java implementation (JGit) doesn’t run nearly as fast as the C implementation. The short version of it is: even after many tunings JGit is twice as slow as the C implementation. One of the problems which got my attention, was the…

  • Weird Sybase JDBC driver issue (jConnect)

    I post this so that the search engines can pick up on it and maybe it can help somebody out. I had the following issue with the Sybase JDBC driver (jConnect): I was calling a stored procedure and it was throwing an error. However these errors weren’t propagated to the Java code in form of…

  • Hackish method to include custom content into CruiseControl

    Disclaimer: I’m a CruiseControl newbie, so there might well be a much better / simpler / cleaner method to achieve this. However this is the way I managed to get it working. Write your (Perl) script and make it output something like this: <testsuite tests="0" name="summary" failures="0"><system-out> foo bar </system-out></testsuite> Make your script run during…