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 MyFutureTask.call() 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; } }
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?
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(naturalOrder.compare(new Integer(42), new Integer(42))); /*This one works but is still broken - outputs 0 as expected! - Why is this broken?*/ System.out.println(naturalOrder.compare(42,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.)
(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!*/ Hope.save(evilObjects); /*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!"); } }
Sunday, July 11, 2010
Puzzle 61 - Set for Optimization?
I'm back with an optimization question. Lately I've been dealing with some huge volume of data and I've realized how performance can start to be a serious problem as the data increases. Here is a small illustration with relatively less data (50,000 values). Hopefully the question should be self explanatory from the code.
Just keep in mind that both the "Are Equal" print statements print true and that you don't use the reference commonA or commonB when writing the code for the third part. You are free to make any other changes (in the place where the comments are there).
Update - The commonA, commonB were incorrectly pointing to the objects listOf2/listof3. That has been corrected to create new Objects -- it now reads commonA = new ArrayList(listOf2); instead of commonA = listOf2;.
Thanks to Colin Hebert for pointing it out.
package com.test; import java.util.List; import java.util.ArrayList; class MyCollection{ public static void populateList(List<Long> l, int multiple){ for(int i=0; i<50000; i++){ Long value = Long.valueOf(i*multiple); l.add(value); } } public static void main(String[] args) { List<Long> listOf2 = new ArrayList<Long>(); populateList(listOf2, 2); List<Long> listOf3 = new ArrayList<Long>(); populateList(listOf3, 3); long startTimestamp, endTimestamp; List<Long> commonA = null, commonB = null, commonC = null; //First Attempt - Runs in 60 seconds startTimestamp = System.currentTimeMillis(); commonA = new ArrayList<Long>(listOf2); commonA.retainAll(listOf3); endTimestamp = System.currentTimeMillis(); System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); //Second Attempt - Runs in 73 seconds - //There are fewer elements in commonB shouldn't this run faster? startTimestamp = System.currentTimeMillis(); commonB = new ArrayList<Long>(listOf3); commonB.retainAll(listOf2); endTimestamp = System.currentTimeMillis(); System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); System.out.println("Are Equal : " + (commonA.equals(commonB))); //Third Attempt - Runs in 2 seconds startTimestamp = System.currentTimeMillis(); /* This part has been intentionally left blank. * That is because I need to have a question for the puzzle, * and I felt like leaving out the Third part would be the right * thing do to. * I've done most of the hard work so this should be easy to fill. * Yes this must run 10 times faster than the solution I have provided * and should be done just as many lines (3 to 5 lines of code should be fine!). * Well I never said anything about life being fair, did I? * */ endTimestamp = System.currentTimeMillis(); System.out.println("Execution Time : " + (endTimestamp-startTimestamp)/1000); System.out.println("Are Equal : " + (commonA.equals(commonC))); } }
Just keep in mind that both the "Are Equal" print statements print true and that you don't use the reference commonA or commonB when writing the code for the third part. You are free to make any other changes (in the place where the comments are there).
Update - The commonA, commonB were incorrectly pointing to the objects listOf2/listof3. That has been corrected to create new Objects -- it now reads commonA = new ArrayList
Thanks to Colin Hebert for pointing it out.
Subscribe to:
Posts (Atom)