thread-safety

Do I need a concurrent collection for adding elements to a list by many threads?

眉间皱痕 提交于 2019-12-21 17:48:26
问题 static final Collection<String> FILES = new ArrayList<String>(1); for (final String s : list) { new Thread(new Runnable() { public void run() { List<String> file2List = getFileAsList(s); FILES.addAll(file2List); } }).start(); } This collections gets very big, but the code works perfect. I thought I will get a concurrent modifcation exception, because the FILES list has to extend its size, but it has never happened. is this code 100% threadsafe ? The code takes a 12 seconds to load up and a

Schema validation error / Thread safety of XmlSchemaSet?

偶尔善良 提交于 2019-12-21 17:24:09
问题 Good afternoon, An XML schema validation snippet is working fine on development/q&a environments, but is yielding some odd validation results in Production. The usual suspect would be that the code is unsafe for threading, and that the additional load of the Production scenario is flushing out the error. The exact scenario is as follows. Consider that the XML being validated is: <mssql:spExecute type="ResultSet" xmlns:mssql="urn:namespace"> <mssql:actor>IPASS</mssql:actor> <mssql:connection

ID generator with local static variable - thread-safe?

与世无争的帅哥 提交于 2019-12-21 12:39:10
问题 Will the following piece of code work as expected in a multi-threaded scenario? int getUniqueID() { static int ID=0; return ++ID; } It's not necessary that the IDs to be contiguous - even if it skips a value, it's fine. Can it be said that when this function returns, the value returned will be unique across all threads? 回答1: No, it won't. Your processor will need to do the following steps to execute this code: Fetch value of ID from memory to a register Increment the value in the register

Why should we make a SessionScoped ManagedBean thread safe in JSF?

冷暖自知 提交于 2019-12-21 12:09:13
问题 I know that Application-Scope persists across multiple users, so it's obvious that we should make sure that all the ApplicationScoped ManagedBeans are thread safe. I also understand that we don't need to care about thread safety for a RequestScoped ManagedBean. That's because it last only for one HTTP request, and is newly instantiated for each request if it's referenced. But I am not quite sure why we should worry about thread safety for a SessionScoped ManangedBean. Even though it persists

Do effectively immutable objects make sense?

我是研究僧i 提交于 2019-12-21 08:00:19
问题 In the book Java Concurrency In Practice it explains the advantages of "effectively immutable" objects versus mutable objects concurrency-wise. But it does not explain what advantage "effectively immutables" objects would offer over really immutable objects. And I don't get it: can't you always build a really immutable object at the moment you'd decide to publish safely an "effectively immutable" object? (instead of doing your "safe publication" you'd build a really immutable object and that

Why is the standard C# event invocation pattern thread-safe without a memory barrier or cache invalidation? What about similar code?

限于喜欢 提交于 2019-12-21 07:27:38
问题 In C#, this is the standard code for invoking an event in a thread-safe way: var handler = SomethingHappened; if(handler != null) handler(this, e); Where, potentially on another thread, the compiler-generated add method uses Delegate.Combine to create a new multicast delegate instance, which it then sets on the compiler-generated field (using interlocked compare-exchange). (Note: for the purposes of this question, we don't care about code that runs in the event subscribers. Assume that it's

How to terminate or stop a detached thread in c++?

落爺英雄遲暮 提交于 2019-12-21 07:24:17
问题 I am interested in terminating/stopping/killing a detached thread in c++. How can this be done? void myThread() { int loop = 0; while(true) { std::this_thread::sleep_for(std::chrono::seconds(5)); ++loop; } } void testThread() { std::thread globalThread(myThread); globalThread.detach(); } int main(void) { testThread(); for(unsigned int i=0; i < 1000; i++) { cout << "i = " << i << endl; } return 0; } The reason why I'd like to "stop"/"terminate" the globalThread() is because valgrind lists that

WPF ObservableCollection Thread Safety

╄→尐↘猪︶ㄣ 提交于 2019-12-21 06:44:49
问题 I've got a MVVM setup. My model periodically calls some service and then invokes an action on the ViewModel which then updates some variables exposed to the View. The variable is an ReadOnlyObservableCollection<T> , which has an ObservableCollection<T> it listens on. The problem is that the Model calls the callback from a different thread, and thus it doesn't allow me to clear the ObservableCollection<T> on a different thread. So I thought: use the dispatcher, if we aren't on the correct

Example of something that is not thread-safe in Rails

本秂侑毒 提交于 2019-12-21 04:34:09
问题 I've seen threads like this on thread-safety in Rails and various web pages on the topic, and I'm sure everyone's great at reciting what it is and giving 'tips' on what isn't thread-safe ("class variables!"), but I can never seem to find a clear, simple, complete example of something that is actually not thread-safe in Rails , to the point where I wonder if anyone actually understands it at all. I would be grateful if someone could prove me wrong and give: a clear, simple, complete example of

Stopping a non-looping Java thread

╄→尐↘猪︶ㄣ 提交于 2019-12-21 04:29:25
问题 This may be a case of me just misunderstanding what I've read, but all the examples for killing a thread in Java seem to indicate that you have to signal a thread to kill itself; you can't kill it from the outside without some serious risks. The problem is, all the examples of how to "politely" ask a thread to die have some kind of looping so all you have to do is watch a flag on every iteration. So, what I've got is a thread that does something that just takes a while (a series of SQL