multithreading

Returning a value from thread?

我只是一个虾纸丫 提交于 2020-01-18 05:47:51
问题 How do I return a value from a thread? 回答1: One of the easiest ways to get a return value from a thread is to use closures. Create a variable that will hold the return value from the thread and then capture it in a lambda expression. Assign the "return" value to this variable from the worker thread and then once that thread ends you can use it from the parent thread. void Main() { object value = null; // Used to store the return value var thread = new Thread( () => { value = "Hello World"; //

Returning a value from thread?

南楼画角 提交于 2020-01-18 05:47:32
问题 How do I return a value from a thread? 回答1: One of the easiest ways to get a return value from a thread is to use closures. Create a variable that will hold the return value from the thread and then capture it in a lambda expression. Assign the "return" value to this variable from the worker thread and then once that thread ends you can use it from the parent thread. void Main() { object value = null; // Used to store the return value var thread = new Thread( () => { value = "Hello World"; //

Difference between pointer and reference as thread parameter

倾然丶 夕夏残阳落幕 提交于 2020-01-18 04:28:17
问题 This is the example: #include<iostream> #include<thread> using namespace std; void f1(double& ret) { ret=5.; } void f2(double* ret) { *ret=5.; } int main() { double ret=0.; thread t1(f1, ret); t1.join(); cout << "ret=" << ret << endl; thread t2(f2, &ret); t2.join(); cout << "ret=" << ret << endl; } And the output is: ret=0 ret=5 Compiled with gcc 4.5.2, with and without -O2 flag. Is this expected behavior? Is this program data race free? Thank you 回答1: The constructor of std::thread deduces

How to call a method with a separate thread in Java?

為{幸葍}努か 提交于 2020-01-18 04:09:41
问题 let's say I have a method doWork() . How do I call it from a separate thread (not the main thread). 回答1: Create a class that implements the Runnable interface. Put the code you want to run in the run() method - that's the method that you must write to comply to the Runnable interface. In your "main" thread, create a new Thread class, passing the constructor an instance of your Runnable , then call start() on it. start tells the JVM to do the magic to create a new thread, and then call your

Using a Thread to run Code and update UI Android

[亡魂溺海] 提交于 2020-01-17 18:26:24
问题 How do I use a thread to run some code continuously whilst an Apps running, using the information it gives to periodically update the UI of the App. Specifically the thread would run some code that searches through a text file in order to find co-ordinates which would then be plotted over a PNG on the UI. This would update automatically say every second maybe every half second, and would clear the image then redraw the points. How do i first of all set up the thread then second of all send

Role of object parameter in Monitor.Enter call .Net

落花浮王杯 提交于 2020-01-17 16:29:11
问题 we all know below code is used to form a critical section. public class CommonResource { public object obj = new object(); public void PopularFunction() { lock (obj) { ///Access variable that we want to protect form being accessed concurrently ///This forms critical section ///My question is what is role'obj' plays in forming critical section. ///How it works behind the scene. } ///Above code can be written as Monitor.Enter(obj); ///Access variable that we want to protect form being accessed

looking for a specific example where Operation is preferred over GCD or vice-versa

人盡茶涼 提交于 2020-01-17 15:31:36
问题 this question is not about what is the difference between operation queues and dispatch queues. i know that. i have gone through it. but still in mind, i don't have any such example where i can 100% say that, yeah GCD should be only choice for it. or OperationQueue should be perfect choice for it. can you give me some example, which explains clear priority of one over another? because almost everything which can be done by gcd, can be also done by operation queue. 回答1: Nothing’s 100%, but

looking for a specific example where Operation is preferred over GCD or vice-versa

有些话、适合烂在心里 提交于 2020-01-17 15:28:08
问题 this question is not about what is the difference between operation queues and dispatch queues. i know that. i have gone through it. but still in mind, i don't have any such example where i can 100% say that, yeah GCD should be only choice for it. or OperationQueue should be perfect choice for it. can you give me some example, which explains clear priority of one over another? because almost everything which can be done by gcd, can be also done by operation queue. 回答1: Nothing’s 100%, but

How to set divider location for JSplitPane on start-up

情到浓时终转凉″ 提交于 2020-01-17 08:06:29
问题 I have JPanel which contains JSplitPane. The JPanel is injected during a runtime into a JFrame using the method invokeAndWait. Then the invokeLater is called to update divider location in SplitPane. The problem is, when the divider update is invoked, JPanel width is still 0. When I add sleep or a breakpoint anywhere in the code (except the invokeLater), the code works fine. final JPanel viewPanel = new JPanel(); viewPanel.setLayout(new BorderLayout()); final JPanel header = getPresenterHeader

How to send and receive multiple messages at a time (java thread socket programming)

时光总嘲笑我的痴心妄想 提交于 2020-01-17 08:00:10
问题 I am creating a chat program between a server and a client but is run by a main function file. If "-l" is present on the command line, it will run as a server, otherwise it will run as a client Command line arguments to run server: java DirectMessengerCombined -l 3000 Command line arguments to run client: java DirectMessengerCombined 3000 All three files need to be able to access String args[] in the main function file because that is how it gets the port number Right now the program is able