multithreading

workerCountOf() method in ThreadPoolExecutor java

℡╲_俬逩灬. 提交于 2020-05-29 04:56:26
问题 I am trying to understand ThreadPoolExecutor class.I found some final variables declared in that class and unable to understand their use. private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0)); private static final int COUNT_BITS = Integer.SIZE - 3; //29 private static final int CAPACITY = (1 << COUNT_BITS) - 1; //536870911 00011111111111111111111111111111 // RUN_STATE is stored in the high-order bits private static final int RUNNING = -1 << COUNT_BITS; //-536870912

How can I measure how many threads are executing a piece of code?

余生颓废 提交于 2020-05-28 18:04:09
问题 I am trying to measure how many threads are executing a section of code at the same time. Currently i am (ab)using Semaphores for this, is there a better way? final int MAX_THREADS = Integer.MAX_VALUE; Semaphore s = new Semaphore(MAX_THREADS); s.acquire(); // start of section // do some computations // track how many threads are running the section trackThreads( (MAX_THREADS - s.availablePermits()) ); s.release(); // end of section 回答1: Use an AtomicInteger instead of a Semaphore . Something

Java “Throttled Consumer” Only Runs Once

和自甴很熟 提交于 2020-05-28 09:46:29
问题 I made the following code with the intention of Creating one thread dedicated to performing a possibly slow network request AND Ensuring a delay between network requests public class QueryManager implements Runnable { private Plugin p; private ScheduledExecutorService executor; private ConcurrentLinkedQueue<QueryRequest> jobs; public QueryManager(Plugin p) { this.p = p; this.executor = Executors.newSingleThreadScheduledExecutor(); this.jobs = new ConcurrentLinkedQueue<>(); int throttle = Math

Calling proc within a tcl thread

血红的双手。 提交于 2020-05-28 07:55:40
问题 When I try to call a proc within a tcl thread, I get an error stating invalid command name. Following is my tcl code. Pls help in identifying why the proc is not recognized within the thread. Thanks. package require Thread proc CPUload { Start Stop } { for {set i $Start} {$i <= $Stop} {incr i} { set j [expr {sqrt($i)*sqrt($i)}] set k [expr {$i % 123}] } } set id1 [thread::create] catch {thread::send $id1 "CPUload 1 50000000"} ret puts $ret puts $errorInfo while {[llength [thread::names]] > 1}

Calling proc within a tcl thread

做~自己de王妃 提交于 2020-05-28 07:55:20
问题 When I try to call a proc within a tcl thread, I get an error stating invalid command name. Following is my tcl code. Pls help in identifying why the proc is not recognized within the thread. Thanks. package require Thread proc CPUload { Start Stop } { for {set i $Start} {$i <= $Stop} {incr i} { set j [expr {sqrt($i)*sqrt($i)}] set k [expr {$i % 123}] } } set id1 [thread::create] catch {thread::send $id1 "CPUload 1 50000000"} ret puts $ret puts $errorInfo while {[llength [thread::names]] > 1}

Calling proc within a tcl thread

99封情书 提交于 2020-05-28 07:54:20
问题 When I try to call a proc within a tcl thread, I get an error stating invalid command name. Following is my tcl code. Pls help in identifying why the proc is not recognized within the thread. Thanks. package require Thread proc CPUload { Start Stop } { for {set i $Start} {$i <= $Stop} {incr i} { set j [expr {sqrt($i)*sqrt($i)}] set k [expr {$i % 123}] } } set id1 [thread::create] catch {thread::send $id1 "CPUload 1 50000000"} ret puts $ret puts $errorInfo while {[llength [thread::names]] > 1}

How can I utilize multithread CPU most in Matlab?

人走茶凉 提交于 2020-05-28 06:55:11
问题 I just bought the Matlab Parallel Computing toolbox. The command matlabpool open opens parallel workers with the number of the cores in my CPU. But each of my CPU core has two threads. According to Windows Task Manager, each worker can only use half performance of one CPU core, which seems could be interpreted as one worker = one thread = "half core". Therefore, after all workers opened, still half of the total power of CPU could be utilized. Is there any other command could help with that?

Mediator deadlock on async await within background worker - how to detect thread calling itself

大兔子大兔子 提交于 2020-05-28 06:50:13
问题 I have a mediator which I have recently needed to synchronize one at a time message dispatch on a background thread but it is locking, demonstrated below. I post a command to a queue and return a task from a TaskCompletionSource: public Task<object> Send(object command, CancellationToken cancellationToken) { var item = new CommandItem() { Command = request, Tcs = new TaskCompletionSource<object>(), Ct = cancellationToken }; this.queue.Writer.WriteAsync(item); // just write and immediatly

Executing GTK functions from other threads

人盡茶涼 提交于 2020-05-28 06:39:07
问题 This question is about GTK and threads. You may find it useful if your application crashes, freezes or you want to have a multithreaded GTK application. 回答1: Main Loop In order to understand GTK you must understand 2 concepts. All contemporary GUIs are single-threaded. They have a thread which processes events from window system (like button, mouse events). Such a thread is called main event loop or main loop. GTK is also single threaded and not MT-safe. This means, that you must not call any

How do I make memory stores in one thread “promptly” visible in other threads?

谁都会走 提交于 2020-05-28 06:07:01
问题 Suppose I wanted to copy the contents of a device register into a variable that would be read by multiple threads. Is there a good general way of doing this? Here are examples of two possible methods of doing this: #include <atomic> volatile int * const Device_reg_ptr = reinterpret_cast<int *>(0x666); // This variable is read by multiple threads. std::atomic<int> device_reg_copy; // ... // Method 1 const_cast<volatile std::atomic<int> &>(device_reg_copy) .store(*Device_reg_ptr, std::memory