synchronization

Java: Lazy Initializing Singleton

倾然丶 夕夏残阳落幕 提交于 2019-11-27 06:14:07
问题 The pattern to create singletons seems to be something like: public class Singleton { private static final Singleton instance = new Singleton(); private Singleton(){ } public static Singleton getInstance() { return instance; } } However my problem is how do you Unit with a class like this if the Singleton Constructor does something that is not unit test friendly e.g. calls external service , jndi lookup etc. I would think i could refactor it like: public class Singleton { private static

Differences between impilicit, explicit and fluentwait

我的未来我决定 提交于 2019-11-27 05:37:46
What are the exact differences between implicitwait() , explicitwait() and fluentwait() ? Could you explain with examples? I posted a blog article about this , and I think I provide a few very details that these other answers missed. Implicit Wait: During an Implicit wait, if the Web Driver cannot find it immediately because of its availability, the WebDriver will periodically poll the DOM ( at an interval of 0.5 seconds or depending on the driver-browser implementation ) until the default implicit max wait time is reached. Once the specified implicit wait max time is over, it will try to

What's the best way to synchronize times to millisecond accuracy AND precision between machines?

不问归期 提交于 2019-11-27 05:32:46
问题 From what I understand, the crystals on PC's are notorious for clock skew. If clocks are always skewing, what is the best way to synchronize clocks between machines with millisecond accuracy and precision? From what I've found, NTP and PTP are possible solutions, but I was wondering if anybody had any experience on stackoverflow.com! I understand NTP is the popular choice, but am wondering if anybody has had any experience with PTP (IEEE1588) 回答1: Just run the standard NTP daemon. It does

what is correspoding feature for synchronized in java?

巧了我就是萌 提交于 2019-11-27 05:31:27
问题 synchronized in Java can guarantee safety of thread. What about C++ ? Thank you! 回答1: Use the following in C++11: mutex _mutex; void f() { unique_lock<mutex> lock(_mutex); // access your resource here. } Use boost if you don't have a C++11 compiler yet. 回答2: Despite this question has been already answered, by the idea of this article I made my version of synchronized keyword using just standard library (C++11) objects: #include <mutex> #define synchronized(m) \ for(std::unique_lock<std:

iCloud + CoreData - how to avoid pre-filled data duplication?

孤街浪徒 提交于 2019-11-27 05:20:15
问题 I have a problem with an iCloud shoebox application and hope some-one can help me (I've spent many hours fighting it in vain). The App: - A simple library style application - containing set of categories (Cat1 .. CatN) each containing items (Item1...ItemM). I used Apple's iPhoneCoreDataRecipes to set up iCloud CoreData stack. Everything works almost perfect with iCloud except - there should be a number of pre-filled empty categories the user can start using once he has opened the app for the

Thread Safe Singletons in Java

余生长醉 提交于 2019-11-27 05:16:45
问题 The wikipedia article on Singletons mentions a few thread safe ways to implement the structure in Java. For my questions, let's consider Singletons that have lengthy initialization procedures and are acccessed by many threads at once. Firstly, is this unmentioned method thread-safe, and if so, what does it synchronize on? public class Singleton { private Singleton instance; private Singleton() { //lots of initialization code } public static synchronized Singleton getInstance() { if(instance =

Using string as a lock to do thread synchronization

强颜欢笑 提交于 2019-11-27 05:14:39
While i was looking at some legacy application code i noticed it is using a string object to do thread synchronization. I'm trying to resolve some thread contention issues in this program and was wondering if this could lead so some strange situations. Any thoughts ? private static string mutex= "ABC"; internal static void Foo(Rpc rpc) { lock (mutex) { //do something } } Strings like that (from the code) could be " interned ". This means all instances of "ABC" point to the same object. Even across AppDomain s you can point to the same object (thx Steven for the tip). If you have a lot of

Do lock-free algorithms really perform better than their lock-full counterparts?

邮差的信 提交于 2019-11-27 05:08:07
问题 Raymond Chen has been doing a huge series on lockfree algorithms. Beyond the simple cases of the InterlockedXxx functions, it seems like the prevailing pattern with all of these is that they implement their own locks . Sure, there are not processor locks, but the concept of looping over and over on each CPU to ensure consistency is very much like a spinlock. And being a spinlock, they are going to be less efficient than the general locks that come with the operating system because they do not

Is changing a pointer considered an atomic action in C?

三世轮回 提交于 2019-11-27 04:45:14
If I have a multi-threaded program that reads a cache-type memory by reference. Can I change this pointer by the main thread without risking any of the other threads reading unexpected values. As I see it, if the change is atomic the other threads will either read the older value or the newer value; never random memory (or null pointers), right? I am aware that I should probably use synchronisation methods anyway, but I'm still curious. Are pointer changes atomic? Update: My platform is 64-bit Linux (2.6.29), although I'd like a cross-platform answer as well :) Michael As others have mentioned

How can two instances of a userscript communicate between frames?

北慕城南 提交于 2019-11-27 04:34:43
Refer to the technique of having the same JavaScript to run in both a web page and an iframe, as described in this answer : For example, suppose you have this page at domain_A.com: <html> <body> <iframe src="http://domain_B.com/SomePage.htm"></iframe> </body> </html> If you set your @match directives like this: // @match http://domain_A.com/* // @match http://domain_B.com/* Then your script will run twice -- once on the main page and once on the iframe as though it were a standalone page. What are the options to have the two instances of the script to communicate with each other? This would be