Posts

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 mappings = new ArrayList (); public LargeDoubleMatrix(Strin...

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 = 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 and have the loop iterate one less. However, another approach is to use a do/while loop whic...

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 = 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; static { final Thread t = new Thread() { @Override public ...

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 A trickier solution public static boolean isPalindromeAnagram(String text) { int count = 0; for (int i = 0; i