synchronization interview questions
Top synchronization frequently asked interview questions
I have a desktop application using git for synchronization. I have also an android application which do the same as the desktop, but I don't know how to do the synchronization part on it. I haven't found any implementation of git on android. I found a jgit, but its unwell documented and I was not able to do even a commit with that.
I was thinking about remote commands to my git server from my android app, but this is really a tease for me.
Could you tell me if any other git implementation which can be used on android exists? If you have any idea how to solve that, please tell me.
Source: (StackOverflow)
My company has a subsidiary with a slow Internet connection. Our developers there suffer to interact with our central Subversion server. Is it possible to configure a slave/mirror for them? They would interact locally with the server and all the commits would be automatically synchronized to the master server.
This should work as transparently as possible for the developers. Usability is a must.
Please, no suggestions to change our version control system.
Source: (StackOverflow)
I see that for using objects which are not thread safe we wrap the code with a lock like this:
private static readonly Object obj = new Object();
lock (obj)
{
// thread unsafe code
}
So what happens when multiple threads access the same code (let's assume that it is running in a ASP.NET web application). Are they queued? If so how long will they wait?
What is the performance impact because of using locks?
Source: (StackOverflow)
Does c# have its own version of the java "synchronized" keyword?
I.e. in java it can be specified either to a function, an object or a block of code, like so:
public synchronized void doImportantStuff() {
// dangerous code goes here.
}
or
public void doImportantStuff() {
// trivial stuff
synchronized(someLock) {
// dangerous code goes here.
}
}
Source: (StackOverflow)
I have been working on a method to sync core data stored in an iPhone application between multiple devices, such as an iPad or a Mac. There are not many (if any at all) sync frameworks for use with Core Data on iOS. However, I have been thinking about the following concept:
- A change is made to the local core data store, and the change is saved. (a) If the device is online, it tries to send the changeset to the server, including the device ID of the device which sent the changeset. (b) If the changeset does not reach the server, or if the device is not online, the app will add the change set to a queue to send when it does come online.
- The server, sitting in the cloud, merges the specific change sets it receives with its master database.
- After a change set (or a queue of change sets) is merged on the cloud server, the server pushes all of those change sets to the other devices registered with the server using some sort of polling system. (I thought to use Apple's Push services, but apparently according to the comments this is not a workable system.)
Is there anything fancy that I need to be thinking about? I have looked at REST frameworks such as ObjectiveResource, Core Resource, and RestfulCoreData. Of course, these are all working with Ruby on Rails, which I am not tied to, but it's a place to start. The main requirements I have for my solution are:
- Any changes should be sent in the background without pausing the main thread.
- It should use as little bandwidth as possible.
I have thought about a number of the challenges:
- Making sure that the object IDs for the different data stores on different devices are attached on the server. That is to say, I will have a table of object IDs and device IDs, which are tied via a reference to the object stored in the database. I will have a record (DatabaseId [unique to this table], ObjectId [unique to the item in the whole database], Datafield1, Datafield2), the ObjectId field will reference another table, AllObjects: (ObjectId, DeviceId, DeviceObjectId). Then, when the device pushes up a change set, it will pass along the device Id and the objectId from the core data object in the local data store. Then my cloud server will check against the objectId and device Id in the AllObjects table, and find the record to change in the initial table.
- All changes should be timestamped, so that they can be merged.
- The device will have to poll the server, without using up too much battery.
- The local devices will also need to update anything held in memory if/when changes are received from the server.
Is there anything else I am missing here? What kinds of frameworks should I look at to make this possible?
Source: (StackOverflow)
Whenever a question pops up on SO about Java synchronization, some people are very eager to point out that synchronized(this)
should be avoided. Instead, they claim, a lock on a private reference is to be preferred.
Some of the given reasons are:
Other people, including me, argue that synchronized(this)
is an idiom that is used a lot (also in Java libraries), is safe and well understood. It should not be avoided because you have a bug and you don't have a clue of what is going on in your multithreaded program. In other words: if it is applicable, then use it.
I am interested in seeing some real-world examples (no foobar stuff) where avoiding a lock on this
is preferable when synchronized(this)
would also do the job.
Therefore: should you always avoid synchronized(this)
and replace it with a lock on a private reference?
Some further info (updated as answers are given):
- we are talking about instance synchronization
- both implicit (synchronized methods) and explicit form of
synchronized(this)
are considered
- if you quote Bloch or other authorities on the subject, don't leave out the parts you don't like (e.g. Effective Java, item on Thread Safety: "Typically it is the lock on the instance itself, but there are exceptions.")
- if you need granularity in your locking other than
synchronized(this)
provides, then synchronized(this)
is not applicable so that's not the issue
Source: (StackOverflow)
Does @synchronized not use "lock" and "unlock" to achieve mutual exclusion? How does it do lock/unlock then?
The output of the following program is only "Hello World".
@interface MyLock: NSLock<NSLocking>
@end
@implementation MyLock
- (id)init {
return [super init];
}
- (void)lock {
NSLog(@"before lock");
[super lock];
NSLog(@"after lock");
}
- (void)unlock {
NSLog(@"before unlock");
[super unlock];
NSLog(@"after unlock");
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyLock *lock = [[MyLock new] autorelease];
@synchronized(lock) {
NSLog(@"Hello World");
}
[pool drain];
}
Source: (StackOverflow)
I think both are doing the same job,how do you decide which one to use for synchronization?
Source: (StackOverflow)
Can someone explain the difference between:
- lock (someobject) {}
- Using Mutex
- Using Semaphore
- Using Monitor
- Using Other .Net synchronization classes
I just can't figure it out. It seems to me the first two are the same?
Source: (StackOverflow)
If I have a util class with static methods that will call Hibernate functions to accomplish basic data access. I am wondering if making the method synchronized
is the right approach to ensure thread-safety.
I want this to prevent access of info to the same DB instance. However, I'm now sure if the following code are preventing getObjectById
being called for all Classes when it is called by a particular class.
public class Utils {
public static synchronized Object getObjectById (Class objclass, Long id) {
// call hibernate class
Session session = new Configuration().configure().buildSessionFactory().openSession();
Object obj = session.load(objclass, id);
session.close();
return obj;
}
// other static methods
}
Source: (StackOverflow)
java.util.concurrent
API provides a class called as Lock
, which would basically serialize the control in order to access the critical resource. It gives method such as park()
and unpark()
.
We can do similar things if we can use synchronized
keyword and using wait()
and notify() notifyAll()
methods.
I am wondering which one of these is better in practice and why?
Source: (StackOverflow)