Posts

Showing posts from December, 2011

Happy New Year all

Thank you everyone who read my blog, especially those who gave feedback. It encourages me to write me more. I hope everyone has a Happy New Year.

Using a memory mapped file for a huge matrix

Overview Matrices can be really large, sometimes larger than you can hold in one array. You can extend the maximum size by having multiple arrays however this can make your heap size really large and inefficient. An alternative is to use a wrapper over a memory mapped file. The advantage of memory mapped files is that they have very little impact on the heap and can be swapped in and out by the OS fairly transparently. A huge matrix This code supports large matrices of double. It partitions the file into 1 GB mappings. (As Java doesn't support mappings of 2 GB or more at a time, a pet hate of mine ;) import java.io.Closeable; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.List; public class LargeDoubleMatrix implements Closeable { private static final int MAPPING_SIZE = 1 << 30; private final RandomAccessFile raf; private

Thread Affinity library for Java supports JNA

Java Thread Affinity library 1.1 BegemoT has provided same code to allow the Thread Affinity library can use JNA if its available. This means you don't have to have the JNI library. The latest version is available HERE Click on the ZIP button to download the whole thing as a zip file.

What skills should a Core Java Developer have?

Overview I have been trying to put together a list of basic skills a Java developer should have to move on to being an advanced Core Java programmer. Skills You; can write code on paper which has a good chance of compiling. can use a debugger to debug programs and profile an application. are familiar all the primitives types and operators in Java. understands the class loading process and how class loaders work can use multiple threads both correctly and can prove this improve performance or behaviour. e.g. wait/notify/notifyAll, SwingUtils.invokeLater, the concurrency library can use checked exceptions, generics and enums can time a small benchmark and get reproducible results can write a very simple client server TCP service have an understanding of garbage collection, when is it triggered, what can you do to minimise it understand when to use design patterns such as Singleton, Factory, Fly-weight, Builder, Object Pool, Iterator, Strategy, Visitor, Composite Sugge

Solution: What is this broken code doing

Following my earlier blog , here is my solutions to these puzzles. Loop every second value List list = new ArrayList<>(); for (int i = 0; i < 10; i++) list.add(i); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); list.remove(i); } When you call remove(i) it shifts all the elements down one AND i++ shifts the index up one. This means, you see every second value. A work around is as follows. while(!list.isEmpty) System.out.println(list.remove(0)); Endless loop This prints all the characters which have value for(char ch = Character.MIN_VALUE; ch<= Character.MAX_VALUE; ch++) { int i = Character.getNumericValue(ch); if (i >= 0) System.out.println("char "+ch + ' ' + (int) ch+" = "+i); } As many realised, this doesn't stop because a char is always less than or equals to the maximum value. (Which is why its called the maximum ;) Some suggested avoiding this difficult value an

What is this broken code doing

Overview Here are some examples of broken code. I find the interesting to understand what they don't do what they appear to. Can you work out why these don't work? ;) Loop every second value List list = new ArrayList<>(); for (int i = 0; i < 10; i++) list.add(i); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); list.remove(i); } prints 0 2 4 6 8 Endless loop This prints all the characters which have value for(char ch = Character.MIN_VALUE; ch<= Character.MAX_VALUE; ch++) { int i = Character.getNumericValue(ch); if (i >= 0) System.out.println("char "+ch + ' ' + (int) ch+" = "+i); } However it doesn't stop. Can you change the code to use a do/while loop so that every char is considered but it does stop? (Without using a larger type) What am I waiting for? This "program" deadlocks. The main method is optional ;) class Main { static int value;

Solutions: Testing an Anagram of a Palindrome

There are many solutions to this problem . However, it is made simpler if you determine the number of letter with an odd number occurrences. As long as the text has no more than 1 odd numbered letter, you can make a palindrome from it. A straight forward solution public static boolean isPalindromeAnagram(String text) { BitSet bs = new BitSet(27); for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z')) bs.flip(ch % 32); } return bs.cardinality() <= 1; } A trickier solution public static boolean isPalindromeAnagram(String text) { int count = 0; for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); if (ch + Integer.MIN_VALUE - 'A' <= 'z' + Integer.MIN_VALUE - 'A') count ^= 1 << ch; } return Integer.bitCount(count

synchronized is slower but not worth worrying about

Overview Often developer jump through hoops to avoid synchronization. There are justifications like; the application does lots of writes or reads. Without a clear idea of what "lots" means, they have no idea whether the optimisations they are using will help or just add complexity (and possibly make it slower) Vector is slower than ArrayList, but that's not a good reason to avoid it I prefer to use lock free objects and collections in a single threaded context. This is more for clarity than performance. It makes it obvious this code was designed to be single threaded. The performance difference by comparison is often trivial. public static void main(String... args) throws IOException { for (int i = 0; i < 5; i++) { perfTest(new ArrayList ()); perfTest(new Vector ()); } } private static void perfTest(List objects) { long start = System.nanoTime(); final int runs = 100000000; for (int i = 0; i < runs; i += 20) { /

DZone adds page view summary.

DZone have added page views to the summary of articles an individual has posted. My articles on DZone This is interesting to me as the articles which are most popular on blogger and are not the same as those on DZone. (A factor for 2 one way or the other in many cases) Its is also interesting that the total number of pages views is currently 173K compared with on blogger its 163K page views. (Surprisingly similar)

Java Interview Question; Anagram of a Palindrome

Overview In many interview questions, what they are looking for is a simple solution to an apparently complex problem. Unfortunately, the solutions are usually obvious if you know the solution, but very hard to come up with in an interview situation. ;) Anagram of Palindrome Write a method to test if a String is an anagram of a palindrome. The method should have O(n) time complexity and O(1) space complexity. You can assume all letters are between ' ' (space) and 'z'. Assume only letters matter and case is ignored. e.g. "kayak" "Rats live on no evil star" "A man, a plan, a canal, Panama" "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod." The solution A brute force approach is to generate anagrams and test if they are a palindromes. This is not required. All you need to test is if the String is the sort of string which can be turned into a palindrome. Sample solution available HERE

Thread Affinity library for Java.

Overview Locking critical thread to a CPU can improve throughput and reduce latency. It can make a big difference to 99%, 99.9% and 99.99%tile latencies. Unfortunately there is no standard calls in the JDK to do this, so I wrote a simple library so you can manage and see how CPUs have been assigned. What does it look like running? In the following example, there are four threads main, reader, writer and engine. The main thread finishes before the engine starts so they end up using the same CPU. Estimated clock frequency was 3400 MHz Assigning cpu 7 to Thread[main,5,main] Assigning cpu 6 to Thread[reader,5,main] Assigning cpu 3 to Thread[writer,5,main] Releasing cpu 7 from Thread[main,5,main] Assigning cpu 7 to Thread[engine,5,main] The assignment of CPUs is 0: General use CPU 1: General use CPU 2: Reserved for this application 3: Thread[writer,5,main] alive=true 4: General use CPU 5: General use CPU 6: Thread[reader,5,main] alive=true 7: Thread[engine,5,main] alive=true Releas

Java Puzzle operator confusion

Why does this int _=1, __=2; int j = __ ^_^ __, k = __-- -+- _ -+- --__; System.out.println(j + " " + k); print the following? 1 3

Java puzzle System.exit and locks.

When you call System.exit() it will stop the execution of the thread at that point and not call any finally blocks. private static final Object lock = new Object(); public static void main(String... args) { Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { System.out.println("Locking"); synchronized (lock) { System.out.println("Locked"); } } })); synchronized (lock) { System.exit(0); } } What does this program print? Replace System.exit(0) with Thread.currentThread().stop() and run again for comparison.

Test a complete failure of the JVM

Say you want to test that your application behaves correctly on restart even after the application crashes. One approach is to trigger a crash in test code and check that data is in a correctable state on restart. Unsafe unsafe = getUnsafe(); // use reflection unsafe.setMemory(0, 1, (byte) 0); This triggers a SIGSEGV # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00000032b967ae09, pid=17870, tid=1082034496 # # JRE version: 7.0_01-b08 # Java VM: Java HotSpot(TM) 64-Bit Server VM (21.1-b02 mixed mode linux-amd64 compressed oops) # Problematic frame: # C [libc.so.6+0x7ae09] memset+0x9 # If your tests still pass, you can be reasonably confident it is recoverable even on a complete failure of the JVM. For the code for getUnsafe(). Warning: This class is appropriately named and misuse can result in a crash of the JVM. It can also change in future versions (It is more likely new methods will be added rather than removed) public