Skip to main content

Posts

Showing posts from March, 2011

To Hold or Not to Hold – A story on Thread references !!!

void SomeMethod(int x, double y) { // some code .... new Thread(ThreadFunc).Start(); } What do you think about the code above? Some may say nothing seems to be wrong. Some may say there is not enough information to comment. A few may say that it is awful to spin off a thread like that (the last line of the method), and that there is a probability for the thread to be garbage collected at an unexpected point of execution. That is something interesting to discuss about. Before presenting my thoughts and supporting facts, the short answer is NO. A thread spun off like that will not be garbage collected as we expect, although one should be morally insane to write such code. Alright, let us discuss. The Thread class is like any other reference type in the BCL. When an instance of a reference type holds no more outstanding references, it is a candidate to be garbage collected. Even worse, it could become a candidate even while it is executing an instance method, while there ar

Crazy Brackets - [](){}();

What does this cryptic bracket sequence mean? What programming language is it? Is it valid syntax? If there is even a weak chance of this syntax being valid, then what does it mean? Alright, alright, alright.....It is C++. That would calm most people; with all their love (pun) for C++. Specifically, it is C++0x. Amongst many other features that we have been waiting for, C++0x gives us the power of lambdas. The formal definition of a lambda in C++0x is as follows:- [capture_mode] (parameters) mutable throw() -> return_type { body } So a lambda may capture one or more variables in scope by value or by reference, or it may capture none. Specifying return_type is not necessary if the type can be inferred or is void. For instance, a std::for_each 's functor based code could be inlined with a lambda as follows:- std::for_each(v.begin(), v.end(), [](int x) { cout << x << std::endl; }); A lambda definition could be assigned to a variable and then used or in

Wetting my feet in Android - Seinfeld Calendar

A couple of my colleagues and I huddled up to learn a bit of Android. I think I told you about that a short while back. We developed a very simple application - The Seinfeld Calendar . Seinfeld calendar, or otherwise called the habit calendar, is Seinfeld's productivity secret . The secret of achieving your goal is practising something, whatever your goal is, everyday and make it a habit. And mark it in your calendar each day you practice, and make sure you do not break the chain. Our application helps you keep track of your everyday tasks. Our application does not jog or meditate or quit smoking for you. You have to do your tasks. Our application provides the facility to create tasks for the habits you wish to pursue, which in certain cases like smoking means quitting. And then you mark each day in the calendar if you pursued your task, else you don't mark and break the chain. And no cheating! The application is in very nascent stage and does not provide you (fancy) s

Anonymous Classes vs Delegates !!!

I am not a java programmer. By that, I do not mean I am against Java. As a programmer by profession and passion, I try to learn things along the way. That includes a little of bit of Java. I should say that my proper encounter, so to say, with Java is a simple application that I am trying out with Android. There might be some hard core differences and/or limitations in the Android version of Java. But I am almost certain that I am using only primary level features of Java. In android, there is this OnClickListener interface, which is used as a callback interface for a button click. So, it is used something like this:- // Create an anonymous implementation of OnClickListener private OnClickListener mCorkyListener = new OnClickListener() { public void onClick(View v) { // do something when the button is clicked } }; protected void onCreate(Bundle bundle) { ... Button button = (Button)findViewById(R.id.someButton); button.setOnClickListener( new OnClick