Friday, October 22, 2010
Looping in different Threads?
In google app engine, It is a request based engine, where http request will be placed and our application should process and provide result back. But we have a limitation; each request should return within 30 seconds. But google app engine provides a task queue implementation where we can post request to that queue and which will be executed later. So, say if we want to process a request which last more than 30 seconds we should split that into small parts and post to task queue.
Mostly, if we want to process a collection, then we will go for looping. But in the case of google app engine, for every item of that collection, we should post request one after other by holding a counter in bigTable (google app engine datastore) .
Here if you see this we are looping a collection but each item will be processed in different threads.
Ok, Now think a senario where you want to loop from middle to end and start of a collection then we need to find some better way.
Thursday, October 14, 2010
Looping (count/2*(count%2==2?1:-1)) from middle to start and end.
One of the following would do:
1.
for(int i = 0 ; i < 5;i++){
System.out.println(i);
System.out.println(i*-1);
}
2.
for(int i =1 ; i <10;i++){
System.out.println(i/2*(i%2==0?1:-1));
}
of course, first one will print 0 two times. So,
1.1.
System.out.println(0);
for(int i = 1 ; i < 5;i++){
System.out.println(i);
System.out.println(i*-1);
}
Let me change these suitable for real situation:
1.1.
doSomethingForIndex(0)
for(int i = 1 ; i < 5;i++){
doSomethingForIndex(i);
doSomethingForIndex(0);
}
2.
for(int i =1 ; i <10;i++){
doSomethingForIndex(i/2*(i%2==0?1:-1));
}
If you look at these you will see no different in execution, because all statements run one after other.
But, say if we want to run doSomethingForIndex(i); in different threads one after other, then sample 1.1 will run two statements in one thread. So in that case it is better to go for option 2. (count/2*(count%2==2?1:-1))
Sunday, April 11, 2010
Counting boxes
I used to count boxes from any surface wherever I saw. Say, If we have a rectangle surface with boxes as shown here.
1. We can count all boxes one by one like this.
2. But, Yes, simply we can count rows and columns and multiply to get the total.
Yes that is 4x6=24, In this case we have to count two times(1-4,1-6)
3. What if we count like one of the following way.
We have to count only one time (1-6) but we have to remember the number once we reach edge.
I see counting diagonally means, counting both rows and columns at once. This different type of counting is possible(or we may use) in different situations in our life. May be sizing a problem etc. I am looking forward to see any real world examples/applications to match this. I just thought to write so you also can think.
Tuesday, September 15, 2009
Part 5: Thoughts of “How we do?” - Process
The question is, if I am waiting to cross, should I look at the other side vehicles as just as vehicle or should I check for my bus as well. Say if I want to catch bus numbered 811, If I sow a bus moving towards the bus stop with the same number, then should I consider that and harry up myself? Is that safer? , faster ? Or correct approach?. Actually once I cross the road and reached the bus stop only I can get the bus. So before getting to that stage, do I need to worry about that? Will it be safer?. Of cause, If I missed that bus then I want to wait for next bus which may come after another 10-20 min or some reasonable time.
For that, a simple advice may be: you shouldn't think about your bus number until you reach the bus stop. That always safer. Because if you think about your bus number then your mind will say to cross the road without looking for other vehicles, that will cause dangerous accidents. You may have to loss your self even.
Normally, the process will be defined for every project with a set of rules and procedures to ensure the safety or quality of the project. But that may slow down the project and we may loose a business because of the delay also.
Say If I have a meeting today at office and I should be on time. While waiting at the start of the crossing, If I sow a bus which I want to catch to go to office and if there is no vehicle then way I shouldn't cross the road? Of cause I shouldn't practice this in main junction or in a traffic area.
On the whole, If we follow all the process regulations in our project and if we missed our dead line we can only say that we have follow this and that process and our project is a better project etc. But we will loose the business at that time. Mostly business needs Good project rather Better project same as real life.
So, It is important to define and follow a process for our project, but at the same time we should identify and change or leave the process when ever its need. Again this is a skill of switch between following and leaving the process, we should be eye open for this always.
Monday, June 8, 2009
Tuesday, January 27, 2009
Part 4: Thoughts of “How we do?” - A Story
A family arranged a trip. They were going by a van. Van was driven by a young driver, next to him a young family member. In the next row, father, mother and their child around 10 years old boy were seated. In the last row grandfather and grandmother were there.
On the way, a traffic signal, it is turned to red. So driver stopped the van. Now the little boy prompt a question “Why you stopped the van?” “The traffic signal is red” driver replied. He asked more “Why we have to stop if signal is red?” for that the young family member replied “If not police man will charge us”
The boy was not satisfied with that answer; He turned his head to back. Then grandfather tried to answer “We have to give way to others before we take”, “There should be a common rule to control”, “you have to wait for your chance”. While grandfather was continuing with his answer, boy had turned his head to his father, because he couldn’t understand those answer. Then father explained, in a junction, if all people try to go, then there will be no coordination. So we have to divide time between us and move one by one. For that there should a person or signal to indicate the time change. And finally if someone violating those rules, there should be police man or someone to check and punish.
After father’s explanation boy was fine. Let stop the story there, in the story, you may have noticed three different answers for one question “Why we have to stop?”
All three answers are right and related to same question. First answer which was from the young man, is talking about immediate input/output of the problem. This is a kind of technician mode of thinking. It is without thinking about others, just thinking how to kindle the present situation according to present observation. Or they may have trained to understand the situation and react according to that.
Second answer, the grandfather’s answer which is more conceptual, no worries about how to design. Scientists used to tell thing like this. More like theories, speaking in an ideal situation.
Final answer, that took care of design aspect, and explained how these are made into a practical working solution. This is an engineer mode of thinking, explained and analyzed the design.
Even though I have put different modes into different people, it doesn’t mean those are owned by only them. All we have those three modes of thinking with us. For each problem in our life, we react or understand things in one or some of the above modes. For every problem, the time to find a solution, the understanding or knowledge we have about the area of the problem and the role we play to solve the problem are decide the way in which we going to handle. In case, if we couldn’t find a solution with one mode, we may have to switch to another different mode as well.
So, it is important of understand these different mode of thinking, choose one or more and switch between them to solve our problems. It is nothing new, we all know this, but just to get to know and organize into you mind.