thread-safety

What is uninterruptible blocking?

点点圈 提交于 2020-01-15 06:17:15
问题 Consider this code from TIJ 4th edition class SleepBlocked implements Runnable { public void run() { try { TimeUnit.SECONDS.sleep(100); } catch(InterruptedException e) { print("InterruptedException"); } print("Exiting SleepBlocked.run()"); } } class IOBlocked implements Runnable { private InputStream in; public IOBlocked(InputStream is) { in = is; } public void run() { try { print("Waiting for read():"); in.read(); } catch(IOException e) { if(Thread.currentThread().isInterrupted()) { print(

Catching exceptions caused in different threads [duplicate]

ぐ巨炮叔叔 提交于 2020-01-15 05:09:35
问题 This question already has answers here : Catching unhandled exception on separate threads (3 answers) Closed 6 years ago . The below example is a simplification of my problem. An exception is thrown within a new thread. If im not handling this within the thread it is not caught by the outer try/catch and crashes my application. Is there any way to guarantee that I catch any exception that occurs. try { new Thread(delegate() { throw new Exception("Bleh"); // <--- This is not caught }).Start();

Ensuring Thread-Safety On Static Methods In C#

旧时模样 提交于 2020-01-14 09:36:09
问题 I've got some code I currently have in a static class/method, but I wanted to check that it would be thread-safe. From what I've read I think this should be ok, but something in the back of my mind is saying it might not be. My web page's data processing stage uses an external web service to create order records, and this could be quite slow: possibly 30-40 seconds to, possibly, 5 or 10 minutes (this is out of my hands) so I was going to fire a return page back to the user, then start a new

VB.NET RaiseEvent, threadsafe?

耗尽温柔 提交于 2020-01-14 03:07:12
问题 Is RaiseEvent thread safe? In C# you write if (event != null) { event.invoke(); } and the C# code is not thread safe.... 回答1: If I need to do thread safe events, I write this: Class Test Public Event Click(ByVal sender As Object, ByVal e As EventArgs) Public Event MouseIn(ByVal sender As Object, ByVal e As EventArgs) Private Delegate Sub EventArgsDelegate(ByVal e As EventArgs) Private ReadOnly _parent As Control Public Sub New(ByVal parent As Control) _parent = parent End Sub Private Sub

Can multiple threads write the same value to the same variable at the same time safely?

时光总嘲笑我的痴心妄想 提交于 2020-01-14 02:55:16
问题 Can multiple threads write the same value to the same variable at the same time safely? For a specific example — is the below code guaranteed by the C++ standard to compile, run without undefined behavior and print "true", on every conforming system? #include <cstdio> #include <thread> int main() { bool x = false; std::thread one{[&]{ x = true; }}; std::thread two{[&]{ x = true; }}; one.join(); two.join(); std::printf(x ? "true" : "false"); } This is a theoretical question; I want to know

Where should my Try Catch block be when running a thread?

↘锁芯ラ 提交于 2020-01-13 20:42:07
问题 Take this thread: Thread thread = new Thread(delegate() { //Code }); thread.Start(); Should it be around the thread.Start(); or inside: Thread thread = new Thread(delegate() { try { //Code } catch (Exception) { //Code } }); 回答1: it is completely different to put then inside or outside. If you put them around the thread.Start() call, you can detect (according to this page: http://msdn.microsoft.com/en-us/library/system.threading.thread.start(v=vs.71).aspx) ThreadStateException The thread has

Does ASP.NET multithreading impact Structuremap singleton class?

你离开我真会死。 提交于 2020-01-13 16:25:12
问题 In my ASP.NET MVC project I have a class which is instantiated via Structuremap and is configured as a singleton. Given that ASP.NET is inherently multithreaded and the Singleton pattern is not threadsafe by default, will it cause any issues? I faced an issue where multiple instances were being returned for a class configured as Singleton. Could this problem be because of the instances being requested from different threads. EDIT : A more detailed description is given on this question

Is pymysql connection thread safe? Is pymysql cursor thread safe?

戏子无情 提交于 2020-01-13 06:18:17
问题 I have queue data structure where multiple thread consume items, each thread is going to write to a database using PyMySQL, no other sync is need among threads. Is race free to use the same cursor coming from the same pymysql connection in all threads? Is race free to use different cursor per thread coming from the same connection? (of course to use multiple connection in multiple threads is ok, because that case has no shared resource, I has no interest in this case) 回答1: Thanks to El Ruso,

Swift 3- Update UI from main thread

淺唱寂寞╮ 提交于 2020-01-13 05:14:43
问题 I wanted to load data in background thread and update tableview/UI on main thread. Based on what's indicated here about threading, I was wondering if the code below is the way to go about it. I'm trying to load more data as user scrolls to a specific index and wanted to make sure the UI is not freezing due to threading. Thank you! func loadMore () { guard !self.reachedEndOfItems else { return } self.offset = self.offset! + 10 print("load more offset: \(self.offset)") var start = 0 var end = 0

Thread safety issue with SimpleDateFormat

爱⌒轻易说出口 提交于 2020-01-12 08:22:30
问题 I got the following piece of code from a programmers test private String formatDate(Date date) { String result = ""; //…. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); result = sdf.format(date); //… return result; } with the additional information that several threads are using the method at once. Are there any problems with this? My answer is that no, it should be fine (assuming that nothing else is going on in the //... parts). My motivation is that no global or class data