Showing posts with label shared data. Show all posts
Showing posts with label shared data. Show all posts

Friday, March 30, 2012

What is the meaning of the keyword Synchronized?

What is the meaning of the keyword Synchronized?

The keyword synchronized is used in multi-threaded applications, to guarantee consistency of the data and to avoid unexpected results.

When it is used in a method declaration, the JVM will guarantee that once a thread A starts to run this method, other threads that want to run other synchronized methods in the same object will be locked until thread A leaves the synchronized method.

E.g.:
          public synchronized int getFoo() { return foo++; }

This keyword can also be used in a synchronized statement, to improve the application performance,  as when not all of the lines of code in the method need to synchronized. It is necessary to declare which object will be synchronized (use this to synchronize the object that has the method).

E.g.:
        public int foo() {
                synchronized(this){
                      foo++;
                 }
                //do something else...
        }

Monday, December 19, 2011

Dining philosophy scenario?

Dining philosophy scenario?
It's a metaphor for us to study concurrency issues (like deadlock, starvation, livelock, etc) and possible solutions.

Imagine 5 philosophers (the concurrent processes or threads) sitting at a table that has a bowl with 'infinite' supply of spaghetti (the shared resources) and 5 forks, 1 between each pair of philosophers. They don't communicate between themselves and all they do is eating (using the resources) or thinking (not using the resources). When they are hungry, they need to grab the left fork (if it is free), then the right one (if it is also free), then eat. When done eating, they put the forks back to the table. If one fork is not available, the philosopher waits until it is available.

Dining philosophers


Issues
Deadlock
If each one gets hungry in the same time, each one will grab the left fork in the same time, and no one will be able to eat since they won't be able to grab the right fork. All of them will be waiting indefinitely for the right fork which will never be available.

Starvation
Considering that we add one specification to solve the deadlock issue - instead of "If one fork is not available, the philosopher waits until it is available", we can say "If one fork is not available, the philosopher returns the left fork and thinks for a specific  X time to try to eat again". Then we could have a philosopher starving if every time he/she tries to eat, one fork (left or right) is not available.

Possible solution
We could bring a waiter that knows when the philosophers are hungry, and knows who has priority, how much each one needs to eat, who has being waiting for a longer time etc. The philosophers will have to request the waiter for a fork then, instead of grabing them off the table.

Wednesday, December 14, 2011

What is race condition?

What is race condition?
Race condition in software is a concurrency flaw that occurs when 2+ threads (or processes) access non-mutual exclusive shared data - like a global variable - and the result of the system is unpredictable, like reading or writing the wrong value.


This question may be asked in a technical interview for a software engineer or any related job position