thread-safety

About asynchronous methods and threads

橙三吉。 提交于 2019-12-25 07:50:02
问题 What actually happens behind the scenes with asynchronous functions? Does it open a new thread and let the OS start and run it? If so, can it cause deadlocks or other thread problems? Here's an example of a async method: var fs = require('fs') var file = process.argv[2] fs.readFile(file, function (err, contents) { var lines = contents.toString().split('\n').length - 1 console.log(lines) }) 回答1: In fs.readFile(file,callback) .This is a non-blocking call which means. node's main thread stores

How to asynchronously collect results from new threads created in real time in ruby

﹥>﹥吖頭↗ 提交于 2019-12-25 07:37:26
问题 I would like to continously check the table in the DB for the commands to run. Some commands might take 4minutes to complete, some 10 seconds. Hence I would like to run them in threads. So every record creates new thread, and after thread is created, record gets removed. Because the DB lookup + Thread creation will run in an endless loop, how do I get the 'response' from the Thread (thread will issue shell command and get response code which I would like to read) ? I thought about creating

Accessing servlet scoped beans from another thread

吃可爱长大的小学妹 提交于 2019-12-25 07:19:01
问题 I am using approach from the "Accessing scoped proxy beans within Threads of" answer. However I am seeing rare deadlocks involving RequestAttributes object. The main reason of the deadlock is between the synchronized (this.sessionAttributesToUpdate) statement in the object and servlet session hash-map. Normally the instances of the object are created for each request, so they don't clash, but if I pass the object to another thread to use the session beans, the same object is used and it

What thread-safe collection is appropriate for this scenario?

旧街凉风 提交于 2019-12-25 06:54:59
问题 I have a singleton class handling Subscribe and Unsubscribe requests from different clients running in different threads. The singleton contains a collection of Subscribers. Before adding or removing, the class needs to validate that the element hasn't already been added or deleted. Normally I would implement this using a List or a HashSet calling Contains before each operation, but if I want to use one of the new classes in the System.Collections.Concurrent namespace, the only option I see

Tkinter.Tk() and threading

核能气质少年 提交于 2019-12-25 06:26:56
问题 There is an interesting issue with Tkinter and threading: I have a Tkinter based GUI and some code executed alongside mainloop. It works like charm if I only do it once. But if i do it twice Tkinter.Tk() blocks BOTH threads: GUI and MainThread. here is the code (inspired by another Tkinter vs threading topic): import Tkinter import threading import logging logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(levelname)s: %(message)s") def gui(): logging.info("Starting GUI") root =

How to make singleton static value securely accessible across all calls for a single HTTP request

孤人 提交于 2019-12-25 05:31:14
问题 I want a singleton accessible to the whole request and only to calls from that request. I have a client with a requirement that the built-in server side session not be used. Instead I need to rebuild the UserSession object each time based on the request's cookies and query string and validate it with the database. There is some overhead in building the UserSession object, so I want to do it only once per request, as early as possible, and then reference that object later throughout the

How to make singleton static value securely accessible across all calls for a single HTTP request

大憨熊 提交于 2019-12-25 05:31:07
问题 I want a singleton accessible to the whole request and only to calls from that request. I have a client with a requirement that the built-in server side session not be used. Instead I need to rebuild the UserSession object each time based on the request's cookies and query string and validate it with the database. There is some overhead in building the UserSession object, so I want to do it only once per request, as early as possible, and then reference that object later throughout the

HashMap or ConcurrentHashMap at Java Controllers?

限于喜欢 提交于 2019-12-25 05:18:14
问题 As explained here: ConcurrentHashMap in Java? concurrent hashmap at Java is thread safe. Java controllers are for web requests and can be called simultaneously from web. My question is that: Should I use concurrent hash map instead of hash map at Java? 回答1: You only need a ConcurrentHashMap if you will be doing concurrent read along with a write or two concurrent writes. If you never change the map after initialization, a regular HashMap is sufficient. Controllers should generally not contain

Async Logger. Can I lose/delay log entries?

徘徊边缘 提交于 2019-12-25 05:16:06
问题 I'm implementing my own logging framework. Following is my BaseLogger which receives the log entries and push it to the actual Logger which implements the abstract Log method. I use the C# TPL for logging in an Async manner. I use Threads instead of TPL. (TPL task doesn't hold a real thread. So if all threads of the application end, tasks will stop as well, which will cause all 'waiting' log entries to be lost.) public abstract class BaseLogger { // ... Omitted properties constructor .etc. ..

Splash screen thread throwing errors. How to resolve? (Code & Errors Included)

可紊 提交于 2019-12-25 03:54:11
问题 I have some errors with my splash screen that I have been working on for a while and can't figure out. Is there a better way to time my splash screen than a thread? What's wrong with my current thread? Can you see an issue with my media player object? I've posted the guts of my splash class. Hopefully I can get some direction on these issues. This works when I run the app but I just don't want to have errors. -------------------------Code------------------------------ @Override public void