thread-synchronization

Synchronizing on function parameter for multithreaded memoization

左心房为你撑大大i 提交于 2019-12-25 05:52:33
问题 My core question is: how can I implement synchronization in a method on the combination of the object instance and the method parameter? Here are the details of my situation. I'm using the following code to implement memoization, adapted from this answer: /** * Memoizes a unary function * @param f the function to memoize * @tparam T the argument type * @tparam R the result type */ class Memoized[-T, +R](f: T => R) extends (T => R) { import scala.collection.mutable private[this] val cache =

Synchronizing on function parameter for multithreaded memoization

馋奶兔 提交于 2019-12-25 05:51:50
问题 My core question is: how can I implement synchronization in a method on the combination of the object instance and the method parameter? Here are the details of my situation. I'm using the following code to implement memoization, adapted from this answer: /** * Memoizes a unary function * @param f the function to memoize * @tparam T the argument type * @tparam R the result type */ class Memoized[-T, +R](f: T => R) extends (T => R) { import scala.collection.mutable private[this] val cache =

What can you do to stop running out of stack space when multithreading?

那年仲夏 提交于 2019-12-25 02:15:14
问题 I've implemented a working multithreaded merge sort in C++, but I've hit a wall. In my implementation, I recursively split an input vector into two parts, and then thread these two parts: void MergeSort(vector<int> *in) { if(in->size() < 2) return; vector<int>::iterator ite = in->begin(); vector<int> left = vector<int> (ite, ite + in->size()/2); vector<int> right = vector<int> (ite + in->size()/2, in->end() ); //current thread spawns 2 threads HERE thread t1 = thread(MergeSort, &left); thread

Java - Threads state when performing I/O operations

谁说胖子不能爱 提交于 2019-12-23 23:22:59
问题 Suppose a Java thread performs some I/O operation like reading a file with traditional blocking Java I/O. The question is: What is the state of the thread while waiting? I don't know if it is RUNNING (doing some active wait) or WAITING (maybe there is some kind of monitor that wakes up the thread when file data is ready). How can I find it out? Thanks. 回答1: A thread that is blocked in an I/O syscall should be in RUNNABLE state, according to my reading of the javadocs. public static final

Print numbers 1-20 with two threads in Java

对着背影说爱祢 提交于 2019-12-23 04:23:14
问题 I'm trying to print numbers 1-20 with two threads: Even thread - Print only even numbers. Odd thread - print only odd numbers. I also have a lock object for synchronization. My application is stuck. Can you tell me what is the problem? My code: public class runIt { public static void main(String[] args) { Odd odd = new Odd("odd thread"); Even even = new Even("even thread"); odd._t.start(); even._t.start(); try{ odd._t.join(); even._t.join(); } catch (InterruptedException e){ System.out

Is the phrase from a book “The current SynchronizationContext is a property of the current thread” correct"?

允我心安 提交于 2019-12-21 22:18:34
问题 Having read the phrase "The current SynchronizationContext is a property of the current thread" correct", I am a little confused... In a C# app code in VS2010, when I type Thread.CurrentThread. I am not finding in the drop-down list of choices given by Intellisense any context-related properties for a thread. I know that current synchronization context can be got through " = SynchronizationContext.Current; " . But this is not quite fortunate with simultaneously executed in parallel threads,

How fast is an atomic/interlocked variable compared to a lock, with or without contention? [duplicate]

旧城冷巷雨未停 提交于 2019-12-20 12:36:24
问题 This question already has answers here : Overhead of using locks instead of atomic intrinsics (4 answers) Closed 8 months ago . And how much faster/slower it is as compared to an uncontested atomic variable (such as std::atomic<T> of C++) operation. Also, how much slower are contested atomic variables relative to the uncontested lock? The architecture I'm working on is x86-64. 回答1: There’s a project on GitHub with the purpose of measuring this on different platforms. Unfortunately, after my

What is the difference between Thread.join and Synchronized?

醉酒当歌 提交于 2019-12-20 12:29:15
问题 I am confused when to use Thread.join() and when to use synchronization in multi threading application. According to me, both of them block or wait for the execution to be done by some other thread. This example has to output 10 A's , 10 B's & 10 C's in sequential pattern one after other like : 1 : A 2 : A 3 : A 4 : A 5 : A 6 : A 7 : A 8 : A 9 : A 10 : A 1 : B 2 : B 3 : B 4 : B 5 : B 6 : B 7 : B 8 : B 9 : B 10 : B 1 : C 2 : C 3 : C 4 : C 5 : C 6 : C 7 : C 8 : C 9 : C 10 : C ----ProGraM ENDS--

How to implement a spinlock to avoid blocking

我与影子孤独终老i 提交于 2019-12-20 04:36:58
问题 Consider the following code: // Below block executed by thread t1 synchronized(obj) { obj.wait(0); } // This block executed by thread t2 synchronized(obj) { obj.notify(); } I understand that in above code if t1 has taken ownership of synchronized block and at the same time if thread t2 tries to take synchronized block, then t2 goes for a kernel wait. I want to avoid this situation and spin t2 before the block until t1 calls wait and leaves ownership of the block. Is that possible? 回答1: The

How to stop a thread after it has completed the runnable?

谁都会走 提交于 2019-12-19 10:56:07
问题 I have a list of tasks and a limited number of threads. The goal is to time how long the tasks take to finish using this number of threads. I know something is wrong with the way I am using threads and Runnable object. I am new to them and can't seem to figure out how to fix it. It errors with a java.lang.OutOfMemoryError: Java heap space error on the line worker.start() after a few seconds. Here is my code: public class Tasks { static Timer timer; //times how long it takes to complete all