thread-safety

Confusion about thread safety - SimpleDateFormat example

浪尽此生 提交于 2019-12-30 06:06:14
问题 I have a question about thread safety. From what I have been told, SimpleDateFormat is not thread safe. I was wondering what effects it would have if I use it the following way in my spring controller: private final static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy", Locale.US); Later in my of my controller functions I use it as follows: try { changedate = changedate.substring(0, 15); calcDate = dateFormat.parse(changedate); } catch (ParseException e2) { logger.error(

Confusion about thread safety - SimpleDateFormat example

狂风中的少年 提交于 2019-12-30 06:05:33
问题 I have a question about thread safety. From what I have been told, SimpleDateFormat is not thread safe. I was wondering what effects it would have if I use it the following way in my spring controller: private final static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd yyyy", Locale.US); Later in my of my controller functions I use it as follows: try { changedate = changedate.substring(0, 15); calcDate = dateFormat.parse(changedate); } catch (ParseException e2) { logger.error(

How to share a variable between 2 threads

百般思念 提交于 2019-12-30 04:24:09
问题 Using Python 2.7.3 on Windows. How can I share a variable num between threads, such that, after num is squared, it is printed? I realized that I need to understand how threads work, but docs don't have much, and I haven't found anything here either.. So, could someone explain how threads work and how to share variables between 2 threads? My code (keeps printing 2 ) import threading def func1(num): while num < 100000000: num = num**2 def func2(num): while num < 100000000: print num, num = 2

ASP.NET MVC How safe are static variables

与世无争的帅哥 提交于 2019-12-30 03:59:07
问题 I want to ensure my website is capable of being hosted on the cloud in the future and also that it can handle a lot of requests. How safe are static variables? Are they unsafe because separate requests by separate users are actually sharing these static variables? Or is it because if you spread the site over threads/sharding or similar, (to handle high loads) the threads are sharing the static variables? Mainly I have helper classes, with static properties, should I change this architecture

Lucene.Net writing/reading synchronization

巧了我就是萌 提交于 2019-12-30 03:09:09
问题 Could I write (with IndexWriter ) new documents into index while it is opened for reading (with IndexReader )? Or must I close reading before writing? Could I read/search documents (with IndexReader ) in index while it is opened for writing (with IndexWriter )? Or must I close writing before reading? Is Lucene.Net thread safely or not? Or must I write my own? 回答1: You may have any amount of readers/searchers opened at any time, but only one writer. This is enforced by a directory specific

AtomicInteger and volatile [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-29 16:07:12
问题 This question already has answers here : What is the difference between atomic / volatile / synchronized? (7 answers) Closed 3 months ago . I know volatile allows for visibility, AtomicInteger allows for atomicity. So if I use a volatile AtomicInteger , does it mean I don't have to use any more synchronization mechanisms? Eg. class A { private volatile AtomicInteger count; void someMethod(){ // do something if(count.get() < 10) { count.incrementAndGet(); } } Is this threadsafe? 回答1: I believe

0MQ: How to use ZeroMQ in a threadsafe manner?

孤人 提交于 2019-12-29 14:28:48
问题 I read the ZeroMq guide and I stumbled upon the following: You MUST NOT share ØMQ sockets between threads. ØMQ sockets are not threadsafe. Technically it's possible to do this, but it demands semaphores, locks, or mutexes. This will make your application slow and fragile. The only place where it's remotely sane to share sockets between threads are in language bindings that need to do magic like garbage collection on sockets. and later on: Remember: Do not use or close sockets except in the

Always declare std::mutex as mutable in C++11?

痞子三分冷 提交于 2019-12-29 11:45:16
问题 After watching Herb Sutter's talk You Don't Know const and mutable, I wonder whether I should always define a mutex as mutable? If yes, I guess the same holds for any synchronized container (e.g., tbb::concurrent_queue )? Some background: In his talk, he stated that const == mutable == thread-safe, and std::mutex is per definition thread-safe. There is also related question about the talk, Does const mean thread-safe in C++11. Edit: Here, I found a related question (possibly a duplicate). It

Setting text on TextView from background thread anomaly

时光毁灭记忆、已成空白 提交于 2019-12-29 08:46:55
问题 I've just started playing with Android Concurrency/Loopers/Handers, and I have just faced with strange anomaly. The code below doesn't block me from setting text on TextView from different Thread. TextView tv; Handler backgroundHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.sample_text); Runnable run = new Runnable() { @Override public void run() { Looper

Update UI from background Thread

痞子三分冷 提交于 2019-12-29 08:23:15
问题 This is just a curious question. Which one is the best way to update UI from another thread. First, this one: private delegate void MyDelegateMethod(); void MyMethod() { if (unknowncontrol.InvokeRequired) { this.BeginInvoke(new MyDelegateMethod(MyMethod)); return; } unknowncontrol.property = "updating!"; } On the other hand: Invoke((System.Threading.ThreadStart)delegate() { unknowncontrol.property = "updating!"; }); Or, is there a better way to do this? Of course this is for WinForms, for WPF