Posts

Tasks to test your Java concurrency skills.

Write a program to calculate the fibonacci and factorals with large answers i.e. using BigInteger, to use multiple threads. Write a similar program to perform matrix multiplication of a large matrix, e.g. 1024x1024 in a multi-threaded manner. (Advanced) Write a thread safe, lockless ring buffer with one reader and one writer, multiple readers and one writer, multiple readers and multiple writers. Try to make this allocation free (ie. doesn't create any objects) Benchmark your code to determine how much faster or slower this is compared with the single threaded version. Take care to ensure your results are reproduceable. Examine how you can determine the optimial number of threads for your each task. Try to adapt your code to use JDK7's fork/join. If anyone has suggestions I would be happy to add them.

Creating Dangling Pointers in Java

Overview Since Java uses a Garbage Collector it is not possoble to create a dangling reference in pure Java See here for more details However using the Java Library it is possible to create a dangling pointer if you go out of your way to create one. You still cannot create one by accident AFAIK. Creating a dangling pointer deliberately A DirectByteBuffer has the address to a direct area of memory. In C terminology this could be called a pointer. This area of memory is not managed by the GC and must be explicity freed. The DirectByteBuffer manages the creation and cleanup for you so that when the DirectByteBuffer's Cleaner/Deallocator is GC'ed, the memory is freed. This means that indirectly the area of memory is freed when it is no longer used, however it is not directly managed meaning you can pervert this process and create a dangling references like so ByteBuffer bb1 = ByteBuffer.allocateDirect(4096); ByteBuffer bb2 = ByteBuffer.allocateDirect(4096); useReflection...

A history of "Java is Dead"

Image
"Java is Dead" has been a popular battle cry for those hoping that a cooler language will emerge as more popular. This could be D, a 4GL, Ruby, Groovy. This wishful thinking appears to be on the rise. This shows the number of hits estimated by google for "Java is dead" and year And what are the languages most associated with the "Java is dead" message? I google "java is dead" and language and the top 12 hits were D 35000 C 30300 PHP 25800 C++ 21300 Python 20000 javascript 19000 C# 18900 ruby 18100 perl 13700 Cobol 11800 Visual Basic 10300 groovy 10100 However, compare this with the number of hits for "Java Programming" and year which suggests there is still a growing interest in Java. This is supported by the latest Tiobe survey which lists not only Java in the top two most popular Programming Language wit...

With generics, the return type is part of the method signature.

I recently came across this odd behaviour in Java. The JVM has always treated the return type as part of the signature but before Generics Java did not. Here is an example of where it does. This was tested using Oracle JDK 6 update 23, OpenJDK 6 update 20, and IBM's JDK R9 2.4. Apparently Java 5.0 update 22 doesn't not compile this code, but given Java 5.0 is EOL, I would have hoped Java 6 would have the correct behaviour. public class Main { public static void main(String... args) { Main.<Integer>print(); Main.<Short>print(); Main.<Byte>print(); Main.<Void>print(); } public static <T extends Integer> int print() { System.out.println("here - Integer"); return 0; } public static <T extends Short> short print() { System.out.println("here - Short"); return 0; } public static <T extends Byte> byte print() { ...

float has a use after all.

After many years believing there was no good use for float , I discover its never too late to eat my words. float has significant round errors and doesn't save much memory compared with double . However I have situation where I want small whole numbers (no rounding error) and I want to handle not-a-number transparently. I also want to minimise data passed between threads to cut latency. So float turns out to be the best solution. :-P

Don't type 2.2250738585072012e-308 in java.

There is a long standing bug in Oracle's Java (even the latest version) where using 2.2250738585072012e-308 as an input causes the JVM to go into an infinitie loop. Even the compiler and your IDE can go into an infinite loop if you use it in code. :P

Memory was cheap but now its cheaper.

Its my impression that hardware costs tend to fall in January. Possibly due to lower demand... In any case I have had a look at the cost of servers with large amounts of memory and can see they are cheaper than the system I bought a year ago. Memory in GB Cost with 2+ GHz processors 24 £1,800 64 £4,200 1024 £53,000 Imagine running a JVM using close to 1 TB of memory of commodity hardware. :D