thread-safety

Executing a Thread in Asynctask and its complications

时光怂恿深爱的人放手 提交于 2019-12-22 12:34:53
问题 I was wondering is it ok to execute a Thread inside the doInBackground method of Asynctask . Should I avoid using this kind of structure on my codes? And if yes, why should I avoid it? Would this cause any ineffectiveness in my apps? 回答1: In principle , there's no problem with starting a thread in the doInBackground() of an AsyncTask , but sometimes you see this done not because it's the right thing to do, but because of a misunderstanding about how AsyncTask works. The point is that

In Java, does read only access to Strings need to be synchronized? [duplicate]

烈酒焚心 提交于 2019-12-22 12:19:41
问题 This question already has answers here : Do I need to synchronize access to immutable types in Java? (5 answers) Closed 4 years ago . Is it required to synchronize read only access to Strings in Java? 回答1: No. Strings are immutable. If all you're doing is reading the String, you're absolutely fine. Edit to add: Yes, if you have a mutable property, that changing the value of the property would need to be synchronized. 回答2: Immutable objects like String are thread safe, in that their internal

Modifying QStandardItemModel from non-UI QThread?

爱⌒轻易说出口 提交于 2019-12-22 11:46:10
问题 I have Qt4 app which binds QStandardItemModel to the QListView and have the model updated from background/non-UI thread. Sometimes, when the QStandardItem 's setText(..) method is called very repeatedly from the non-UI thread, the application will crash at a la dataChanged(..) handler. I can reproduce the issue by calling setText("xxxxx") repeatedly in a for loop. In my app, the data is read from network hence I update the model in separate, non-UI thread. Is this a common pb? If I understand

How can one implement a thread-safe wrapper to maps in Go by locking?

a 夏天 提交于 2019-12-22 11:27:38
问题 I'm trying to wrap a general map (with interface{} as both key and value) as in-memory key-value store that I named MemStore . But it is not thread-safe, despite my use of a sync.RWMutex to lock access to the underlying map. I did verify that it works fine when used from a single goroutine. However, just two concurrent goroutines accessing it results in panic: runtime error: invalid memory address or nil pointer dereference . What is causing this problem, and what is the proper way to achieve

Is this example thread safe?

随声附和 提交于 2019-12-22 10:56:33
问题 Suppose I have singleton class that acts as a data cache. Multiple threads will read from the cache, and a single thread will periodically refresh it. It looks something like this: public sealed class DataStore { public static DataStore Instance { get { return _instance; } } public Dictionary<Foo, Bar> FooBar { get; private set; } static DataStore() { } private DataStore() { } public void Refresh() { FooBar = GetFooBarFromDB(); } private static readonly DataStore _instance = new DataStore();

nHibernate logging with Log4Net, thread session issue

夙愿已清 提交于 2019-12-22 10:38:26
问题 Hey there folks, having a little issue here which I'm trying to wrap my head around. I'm currently starting out with nHibernate, such I have to due to work requirements, and am getting a little stuck with nHibernate's Sessions and multiple threads. Well the task I want to complete here is to have Log4Net log everything to the database, including nHibernate's debug/errors etc. So what I did was create a very simple Log4Net:AppenderSkeleton class which fires perfectly when I need it. My intial

Lockfree standard collections and tutorial or articles

时光怂恿深爱的人放手 提交于 2019-12-22 08:52:04
问题 Does someone know of a good resource for the implementation (meaning source code) of lock-free usual data types. I'm thinking of Lists, Queues and so on? Locking implementations are extremely easy to find but I can't find examples of lock free algorithms and how to exactly does CAS work and how to use it to implement those structures. 回答1: Check out Julian M Bucknall's blog. He describes (in detail) lock-free implementations of queues, lists, stacks, etc. http://www.boyet.com/Articles

Java threaded Random.nextLong() returning the same number

烂漫一生 提交于 2019-12-22 08:36:40
问题 I'm using an OAuth library that calls new Random().nextLong() to generate nonces, however it generates the same nonce on asynchronous calls. I've narrowed it down to threading Random.nextLong() to returning the same exact number every so often. Does anyone know if this is a known limitation of Java? If so, does anyone know of a thread safe operation? EDIT: I'm using Java 1.6 EDIT: This is a small program I made to test out what was going on in my larger app. I ran this several times and more

Condition Variable - Wait/Notify Race Condition

删除回忆录丶 提交于 2019-12-22 07:58:09
问题 I'll present some code first since explaining is easier that way. Assume that mutexes are correctly used with the condition variables to keep it simple: // Thread 1 while(1) { conditionVariable.wait(); // Do some work } // Thread 2 while(1) { // Do some work conditionVariable.notify_one(); } // Thread 3 while(1) { // Do some work conditionVariable.notify_one(); } What I would like to achieve is that thread 1 is guaranteed to be waiting on the condition variable when thread 2 or Thread 3

What is better approach to listen in MultiThreading Service?

China☆狼群 提交于 2019-12-22 07:50:13
问题 I am relatively new both to MSMQ and Threading in .NET. I have to create a service which listen in different threads, via TCP and SNMP, several network Devices and all this stuff run in dedicated threads, but here also is required to listen on MSMQ Queue from another applications. I am analyzing another similar projects and there is used next logic: private void MSMQRetrievalProc() { try { Message mes; WaitHandle[] handles = new WaitHandle[1] { exitEvent }; while (!exitEvent.WaitOne(0, false)