Archive for the ‘General’ Category

Writing thread safe static methods

Recently, I was writing a static method and a thought of concurrency crossed my mind. So, I read and discussed about this with a colleague and I am sharing my understandings.

Memory in java is split up into two kinds – the heap and the stacks. The heap is where all the objects live and the stacks are where the threads do their work. Each thread has its own stack and can’t access each others stacks.

Each thread also has a pointer into the code which points to the bit of code they’re currently running. When a thread starts running a new method it saves the arguments and local variables in that method on its own stack. 
Some of these values might be pointers to objects on the heap. 

If two threads are running the same method at the same time they will both have their code pointers pointing at that method and have their own copies of arguments and local variables on their stacks.
They will only interfere with each other if the things on their stacks point to the same objects on the heap.
In which case all sorts of things might happen. But For example Strings are immutable (cannot be changed) so we’re safe if this is the only object being “shared”.

In Summary, While writing static methods try to keep the scope of variables to the smallest possible. this way most probably we are thread safe.
If shared objects are immutable then also we are good to go.

Advertisements

Why are you a programmer ?

Being a Software Developer, you should know why are you a programmer?

Are you in Programming just because you have e done MCA or BE in Computers ?. Answer of majority people is Yes. But it should not be the case.

One of the major problems with the people is that many just aren’t interested in programming.

I have seen profiles of people who are not even computer graduates and they are programmers.

From my perspective, Creating computer programs is not only a job.

Majority of the people aspiring to be programmers won’t write a program until they are FORCED to by their college or by their employer. And that is NOT good.

I would say 70% of the people opting for this profession are doing it just because They thinks that it has good money and a very convenient way of getting abroad.

And when people do something without having a real liking or passion for that thing, it shows up in the quality of the output.

You can teach people syntax of JAVA and SQL and any other programming language. But you can’t really teach people to think creatively. Think of a solution for a problem. And certainly you cant teach someone to love their profession.

Well, One thing is also that I am not talking about individuals situation but this is the true fact which can’t be ignored.