Hot answers tagged synchronization
15
It sounds to me like they are leading you toward a semaphore solution. Semaphores are used to signal another thread that it's their turn. They are used much less frequently than mutexes, which I guess is why they think it's a good interview question. It's also why the example seems contrived.
Basically, you would create m semaphores. Each thread x waits ...
12
Interview questions are sometimes actually trick questions, intended to make you think about the problem that you're trying to solve. Asking questions about a question are an integral part of approaching any problem, whether it's in the real world or in an interview. There are a number of videos circulating the internet on how to approach questions in ...
12
This is an oddly phrased question that is really, really broad if answered fully. I'm going to focus on clearing up some of the specifics that you're asking about.
Immutability is a design trade off. It makes some operations harder (modifying state in large objects quickly, building objects piecemeal, keeping a running state, etc.) in favor of others ...
7
The only difference between the two is the synchronization used in StringBuffer. The overhead of synchronization is not huge in the grand scheme of things, but it is significant relative to the StringBuilder methods that don't have them. The JVM is doing work that it wouldn't otherwise have to do--especially with only one thread, etc.
If your code works ...
7
On Windows there's a mechanism to have the OS alert you when there's a change to a 'watched' directory structure - FindFirstChangeNotification(). When that indicates a file has changed, an application can then go about comparing files in the changed directory to find the actual files that have changed by looking at size, modified date, hash, etc.
This (as ...
6
In my opinion, this is a fabulous interview question -- at least assuming (1) the candidate is expected to have deep knowledge of threading, and (2) the interviewer also has deep knowledge and is using the question to probe the candidate. It's always possible that the interviewer was looking for a specific, narrow answer, but a competent interviewer should ...
6
Is there any algorithm that must use one of them in its implementation?
Almost certainly not. (Indeed, from the theoretical perspective, you should be able to simulate wait / notify using other java.util.concurrent.. classes. And synchronized could be replaced with explicit Lock operations ... though you would need to be careful to unlock in finally ...
5
We use both Redis and Zookeeper at work so this is from first hand experience
Redis is fast; really, really fast. It is also immediately consistent, so it's good for fast moving data sets. The downside is that, running on one server, if it fails then you lose write access until another server takes it's place. Replacing the server is a manual operation ...
5
This is the Byzantine Generals problem, which is unsolvable. You can never guaranteed synchronize the two servers if you cannot guarantee that at some time in the future, you will have sufficient reliable bandwidth to perform the synchronization all in one go.
5
No, Dropbox and any decent synchronization tool relies on file system events. All operating systems offer such events and programs like Dropbox are just simply listening for changes on files they are watching. When a change happens, or file is added / deleted, Dropbox decides what it has to do.
If several events happen while others are in progress, they a ...
5
A function that accepts some value and returns some other value, and doesn't disturb anything outside of the function, has no side effects, and is therefore thread-safe. If you want to consider things like how the way the function executes affects power consumption, that's a different problem.
I am assuming that you're referring to a Turing-complete ...
4
I'm inclined to agree with you for the reasons you state. Also, if you use the same database, you also get a very practical business benefit - you can easily 'upgrade' a customer to the full version without a complicated data export/import.
One thing you might recommend is trying it first with the one database approach. If that becomes a problem, its far ...
4
Client 1 connects, checks for an active lock on key 1, finds none, and
gets the data
You should not test for lock then create it, but rather attempt to create it with Memcache::add, which will either create lock or fail. It does so atomically, so you'll no longer have TOCTTOU race condition.
$mc= new Memcache;
$mc->connect('localhost', 11211);
...
4
The usual solution for knowing "which change is correct" is a vector clock. You essentially keep track of counters for each repository that holds the data, and reject changes if a particular client's view of everyone else's state differs from that of the peer it is connecting to.
The big question that you have to answer is how you'll resolve rejected saves. ...
4
The reason is nothing to do with race conditions, or the method returning a value that you can't rely on:
If the current thread holds the lock before the test, it is guaranteed to hold it after the test.
If the current thread doesn't hold the lock before the test, it won't hold it after the test.
This situation will only change if the thread executes a ...
4
For each event, besides its "event time" you also need to keep track of one more time attribute, the time of last modification. This takes care of the simple and, ehm, lets say, uneventful sync scenario: events that have been modified since the last sync time get copied over.
The problems start if there is a possibility that two events may have both been ...
4
Since what you are really talking about here is a stylistic/readability issue (at least, that's the way I read it), you might be a little out of luck, because all the all alteratives I can think of aren't great.
Really you have two options, write multiple distinct functions like you have done above, or use closures to bring it all "inline" (as it were), ...
3
The actual logistics of this aren't too tricky (last-modified date on each record, and a "disconnected-since" or "last-synced" timestamp stored somewhere), the real issue is probably going to be your conflict resolution technique. What should the system do when two people modify the same field in the same record?
I'd suggest logging all offline changes ...
3
For hotels there is a third requirement you're not thinking of - exposing yourself to aggregation sites (think Expedia) for bookings.
If this is a requirement (and it probably is when they ask "so how do we sell through Expedia") then synchronisation sort of goes out of the window (there are ways around it but really you all want to look at the same data ...
3
I'm unsure if I understood your questions.
IMHO the answer is yes. If all your objects are immutable, then you don't need any locks. But if you need to preserve a state (e.g. you implement a database or you need to aggregate the results from multiple threads) then you need to use mutability and therefore also locks. Immutability eliminates the need for ...
3
The problem you mention doesn't exist, since the limit you specified in your question is wrong.
Until Vista, Explorer was limited to 248 characters.
.NET Framework File is limited to 260 characters (MAX_PATH).
The real maximum path for Windows itself is approx. 32,767 characters.
I have no idea where have you found the mention of 255 characters.
Now, if ...
3
I can't guarantee that this would solve the issues you're running into, but Queuing is often used to chain a series of asynchronous calls so that they happen one after another.
I wrote a Queue script a while ago which I will be using as an example:
(function () {
var q = new Queue();
q.queue(function (next) {
//this gets run first
...
3
You should look into Deferreds.
You can implement your functions and execute them sequentially, regardless of the fact the the code inside the function is async. Once the first finishes the second will be triggered and so on.
2
Paxos is an algorithm that can be used for active-active replication between multiple masters. In the (patented) version we use, it has been enhanced to work well in a WAN environment.
A nice feature is the entirely of failure conditions are contained within the algorithm, so the issues of data loss or packet loss are not a threat to data safety. The ...
2
This would totally depend on your replication requirements.
Do you want immediate consistency across all boxes? (If so, the choice is simple - replicate rt and deal with failures / slow speed).
Do you want guaranteed tolerance against datacenter failures?
How much data can you afford to lose in such a case? This would determine how often you move data ...
2
You Testing a lock because leads to race conditions.
The "only race conditions possible are in #3 ... and may be irrelevant" is a bad policy.
A race condition is simply a fatal flaw in the design.
That's why these "test" methods are not provided.
You've answered your own question very nicely.
Also, anyone writing an application using lock testing will ...
2
You outline 2 approaches and it has already been suggested to you that you could have a desktop & web app talk to the same database or the same web service - both are of course possible.
The problem is how to choose the approach, and for this I suggest you consider the following scenarios:
What is the impact if the system becomes unavailable to the ...
2
Difficult question to answer, but the following should get you somewhere:
Static methods are not inherently evil. If they encapsulate some functionality then it's a completely valid way of doing something. In fact there are entire programming paradigms such as functional programming which require local state only.
However if there are any static state ...
2
When the application only consists of static methods it is clearly not object oriented (however, this doesn't mean that you should never have any static methods). Further more you will probably have little to no unit tests (since testing static things is almost impossible). The only advantage I can think of is that walking the call graph in the application ...
Only top voted, non community-wiki answers of a minimum length are eligible