Saturday 27 September 2008

Super-Power Synchronisation in Java and C#: Say Arrivederci to the Ornamental Garden Problem and Buonasera Deadlock!

In Java you would use a synchronised block. In C#, the syntactic shortcut lock does the same thing.

lock(x), where x is a reference type, is equivalent to

System.Threading.Monitor.Enter(x)
try {
...
} finally {
System.Threading.Monitor.Exit(x);
}

except that x is only evaluated once. Locking avoids problems like the ornamental garden problem. C#'s System.Threading namespace has stuff for COM interoperability which Java doesn't have e.g. the the System.Threading.ApartmentType to determine if an apartment is STA or MTA.

Wednesday 24 September 2008

Is Java GC really better than C# GC? NO WAY! Java just hints, C# actually DOES stuff!

JAVA way: System.gc() -> Runtime.getRuntime().gc() - this is just a "hint" to JVM to do some cleanup
C# way: GC.Collect() -> this is a "command" to CLR, "clean up your act!" This will run all eligible object finalizers on a separate thread. Another important method is GC.WaitForPendingFinalizers, usually you call this after a GC.Collect. It makes a difference to the memory used by your program! You can also run a GC.Collect on a specific generation (generation 0 being the most recently allocated objects).

Thursday 11 September 2008

Why Java Math Beats C# Math (Prime numerology)

Now if Java's integers don't have enough juice for you (and you must be a serious arithmetic junkie if that's the case) there is always refuge in java.math's BigInteger class.This is a perennial favourite if you want to test prime numbers. AFAIK prime numbers don't exist in C#, but hobbyists have nonetheless tried to find them using BigInteger code on codeproject.OK, partner! Let's instantiate java.math.BigIntegers! Let's find primes! To Double.POSITIVE_INFINITY and beyond!

Saturday 6 September 2008

IEEERemainder: Simply the Best and Probably the Most Under-used method in java.lang.math

java.lang.Math's Math.IEEEremainder( double, double) rocks and rules! Forget about those boring and simple min, max and trig functions! Every Tom, Dick and Harry knows that. (Well, trig functions are not so simple in that they use Taylor series, but that's not too hardcore for a vaguley mathematical computer programmer). IEEERemainder is so cool that C# implements EXACTAMUNGO the same method in the System namespace of mscorlib. They copied the signatures cleanly right down to the name of the static class. Microsoft really did their homework on this one.

But whose implementation is faster? Haven't a clue what I'm talking about? Take a crash-course on math co-processors by talking to Dr Chuck. Now, THAT's what I'm talking about.