multithreading

Eclipse RCP: check if a job has finished

孤街浪徒 提交于 2021-01-28 11:53:49
问题 I want to check if a Job has finished. Currently I do this: if ( traceJob.getState() != Job.WAITING && traceJob.getState() != Job.RUNNING){ But I think there should be a better way to check. Anyone has any good idea ? 回答1: You can use Job.addJobChangeListener to add a listener to a Job. The done method of the listener is called when the job finishes. There is a JobChangeAdapter class with default implementations of all the IJobChangeListener methods so that you don't need to implement all of

Why this example using mutex is less efficient compared to another one with additional condition variable?

半腔热情 提交于 2021-01-28 11:01:42
问题 An example from The Linux Programming Interface : In the producer threads, we would have code such as the following: static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static int avail = 0; /* Code to produce a unit omitted */ s = pthread_mutex_lock(&mtx); if (s != 0) errExitEN(s, "pthread_mutex_lock"); avail++; /* Let consumer know another unit is available */ s = pthread_mutex_unlock(&mtx); if (s != 0) errExitEN(s, "pthread_mutex_unlock"); And in the main (consumer) thread, we could

Changing image on Android thread

江枫思渺然 提交于 2021-01-28 10:07:41
问题 I've gone through stackoverflow and all the similar answers - but have found them not of use. Can anyone please point out why this is not working? It should be simple: update the image every 6 seconds (will be every 3 mins but for testing I've put it as 6 seconds). What the code does is - goes through each of the 4 images step by step but does not change the image. HOWEVER when it gets to the last image - it does change it(??). Questions: What's wrong with this? Why would it only update the

Problems with my Thread.sleep()

帅比萌擦擦* 提交于 2021-01-28 09:55:48
问题 I'm creating a simple video poker program and right now I'm working on the action that's performed after the user has specified the cards he wants to hold, and replace the discarded cards with new cards after the draw. I have an Action where I want to replace the cards one by one with a delay between all replacements, but with the code I have below, it'll sleep for 500 ms multiplied by the number of cards I have to replace and THEN replace all the cards at once, rather than replace it one at

How should conditional variables in producer-consumer implementations be initialized

陌路散爱 提交于 2021-01-28 09:51:12
问题 I am trying to understand the use of conditional variables to implement producer-consumer buffers. I have the following code, which implements a queue for integers (which could be linux file descriptors). The code works as expected, but I am trying to understand why. Both enqueue and dequeue operations wait on some conditional variable before signaling the other conditional variable. Why are these waits unblocking? Is this due to spurious wakeups? #include <iostream> #include <thread>

Is it ok accessing [UIApplication sharedApplication] from background thread?

前提是你 提交于 2021-01-28 09:33:51
问题 While working on Objective-C , I need to get the protectedDataAvailable status possibly inside some background threads. - (BOOL) isProtected { BOOL protectedDataAvailable = [[UIApplication sharedApplication] isProtectedDataAvailable]; return protectedDataAvailable; } As I am accessing [UIApplication sharedApplication] , I suspect that the code block should run in main queue. How can I do so? I was thinking to change it like, - (BOOL) isProtected { BOOL protectedDataAvailable = NO; dispatch

Is it ok accessing [UIApplication sharedApplication] from background thread?

僤鯓⒐⒋嵵緔 提交于 2021-01-28 09:31:54
问题 While working on Objective-C , I need to get the protectedDataAvailable status possibly inside some background threads. - (BOOL) isProtected { BOOL protectedDataAvailable = [[UIApplication sharedApplication] isProtectedDataAvailable]; return protectedDataAvailable; } As I am accessing [UIApplication sharedApplication] , I suspect that the code block should run in main queue. How can I do so? I was thinking to change it like, - (BOOL) isProtected { BOOL protectedDataAvailable = NO; dispatch

Why won't my simple Connection Pool execute these simple put statements?

爷,独闯天下 提交于 2021-01-28 09:15:27
问题 I posted a question regarding how to effectively manage threads here How do I properly use Threads to connect ping a url? I got some great recommendations and tips regarding pools, thread safety, and some libraries and gems to use. I'm trying to execute one of the recommendations listed by using concurrent-ruby to create a thread/connction pool to execute some threads. In a simple ruby file I have the following code: pool = Concurrent::FixedThreadPool.new(5) pool.post do puts 'hello' end As

How to handle uncaught exceptions from CompletableFuture.runAsync

你离开我真会死。 提交于 2021-01-28 08:55:49
问题 Our application has some code that runs asynchronously that is failing. Like this: CompletableFuture.runAsync( () -> { throw new RuntimeException("bad"); }, executorService ); We want default exception handling code that can catch these errors, in case specific uses forget to handle exceptions (this came from a production bug). This is apparently tricky. The answer given in Handling exceptions from Java ExecutorService tasks does not work. It relies on the task being a Future<?> and then

How to use concurrent.futures in Python

我的未来我决定 提交于 2021-01-28 08:44:18
问题 Im struggling to get multithreading working in Python. I have i function which i want to execute on 5 threads based on a parameter. I also needs 2 parameters that are the same for every thread. This is what i have: from concurrent.futures import ThreadPoolExecutor def do_something_parallel(sameValue1, sameValue2, differentValue): print(str(sameValue1)) #same everytime print(str(sameValue2)) #same everytime print(str(differentValue)) #different main(): differentValues = ["1000ms", "100ms",