Wednesday, December 7, 2011

Javachats - My Personal Blog

Its been over a year since I wrote a blog post. I ran out of puzzles - after all how many quirks could a single language have ;)!

I started writing out a bit again - this time its a more personal blog. More about stuff I do, things day in and day out and the occasional general post. Of course since I work in and around Java most if it would be about Java!

Hoping to see some old friends again @ my new blog -

Signing off - Sam!

Monday, August 2, 2010

Puzzle 64 - The Future is Secure

Java 1.5 adds a lot when it comes to threading. However thread programing is always a little tricky and Threading casually can lead to problems - With Great Power Come Great Responsibility. There is a Major bug (and a minor performance bug). Can you spot them both?

package com.test; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.ScheduledThreadPoolExecutor; public class MyObject { public static Executor ex = new ScheduledThreadPoolExecutor(100); public static void main(String[] args) throws InterruptedException, ExecutionException { Tracker t = new Tracker(); Future<Tracker> f[] = new Future[1000]; for(int i=0;i<1000;i++){ /*The code in would be executed sometime in the future*/ f[i] = new FutureTask<Tracker>(new MyFutureTask(t)); ex.execute((Runnable) f[i]); } /*Wait for all the future task to complete!*/ for(int i=0;i<1000;i++){ f[i].get(); } /*Print out the number of future task that we have completed*/ System.out.println(t.getValue()); /*This prints the number of task executed - 1000*/ } } class MyFutureTask implements Callable<Tracker>{ Tracker myTracker; MyFutureTask(Tracker t) { myTracker = t; } @Override public synchronized Tracker call() throws Exception { /*Some complex business logic inserted at this point*/ int rand = new Random().nextInt(1000); Thread.sleep(rand); myTracker.increment(); return myTracker; } } class Tracker{ Integer value = 0; public void increment() { value++; } public int getValue(){ return value; } }

Wednesday, July 28, 2010

Puzzle 63 - Out of the Box

Java 1.5 introduced Autoboxing and since then a lot of code has been indiscriminately been using Autoboxing. I've picked up a simple case this time around (in fact its based on Joshua Bloch's - Effective Java). We'll look at a more real world problem next week.

package com.test; import java.util.Comparator; public class Order { public static void main(String[] args) { Comparator<Integer> naturalOrder = new Comparator<Integer>(){ public int compare(Integer first, Integer second) { return first < second ? -1 :(first == second ? 0:1); } }; /*This one is obviously broken - outputs 1 instead of 0 - Why?*/ System.out.println( Integer(42), new Integer(42))); /*This one works but is still broken - outputs 0 as expected! - Why is this broken?*/ System.out.println(,42)); } }

Sunday, July 18, 2010

Puzzle 62 - WARNING - Is this the End?

The world is about to end, but maybe, just maybe there is some Hope. So it give it a spin and try it. After all the fate of Earth rests in your hands!
(The code is well commented, I think you'll find everything you need out there.)

package com.test; import java.util.ArrayList; import java.util.List; /*This class had been set to trigger on December 21, 2012. *Due to a programming this class will trigger off in the next 15 mins *of you reading it. *Yes its a know issue and we've sorry about it, but right now there are other major *issues to solve and this can wait for later! **/ public class DoomsDayEarth { /*No point in returning anything once the world has been destroyed!*/ public static void destroyWorld(List<EvilObject> evilObjects) { /*You do get a chance to save the world. Try it!*/; /*This code destroys the world and prints world destroyed. *Prevent that from happening and if possible get it to print world saved * */ for(Destroy d : evilObjects) { d.destroy(); } } public static void main(String[] args) { List<EvilObject> evil = new ArrayList<EvilObject>(); evil.add(new EvilObject()); DoomsDayEarth.destroyWorld(evil); } } /*This is the only class that you can modify. Whatever happens there is always Hope!*/ class Hope { /*Write some code here that will save the world. Remember you just have 15 mins *to save the world, before main starts up, so be quick. **/ public static void save(List<EvilObject> evilObjects) { } } interface Destroy{ void destroy(); } class EvilObject implements Destroy { public void destroy() { //This method has the power to destroy the world! //To prevent any possible misuse, the code has been censored //and is not published on twisters! System.out.println("The world is destroyed!"); } } class GoodObject implements Destroy { public void destroy() { //This method does nothing. Its sole purpose is to replicate the EvilObject //without doing any evil!! System.out.println("The world is saved!"); } }