multithreading

Making asynchronous code cancellable

青春壹個敷衍的年華 提交于 2021-02-07 20:33:22
问题 I have some time-consuming method: public class TimeConsumingClass { public void TimeConsumingMethod() { //let's imagine my method works this way for (var i = 0; i < 10000; i++) Thread.Sleep(); } } It was executed in main thread previously. Then I needed to call it in secondary thread to not block UI: Task.Factory.StartNew(() => { new TimeConsumingClass().TimeConsumingMethod(); }); And now I need to make it possible to stop this method at any time. So I want to make my method treat

Spring Scheduled fixedRate not working properly

孤人 提交于 2021-02-07 20:30:38
问题 As the title says, I am trying to use the fixedRate paramater of the Scheduled annotation in order to invoke a function every second. Here is the code that I am using: //execute once every second @Scheduled(fixedRate = 1000) private void pullLiveDataFromExternalServer() throws InterruptedException { System.err.println("START THREAD " + Thread.currentThread().getId()); Thread.sleep(5500L); System.err.println("END THREAD " + Thread.currentThread().getId()); } The way I understand it, the

Two waiting threads (producer/consumer) with a shared buffer

余生长醉 提交于 2021-02-07 20:28:44
问题 I am trying to have a bunch of producer threads that wait untill the buffer has room for an item, then it puts items in the buffer while it can, going back to sleep if there is no more room. At the same time there should be a bunch of consumer threads that wait untill there is something in the buffer, then it takes things from buffer while it can, going back to sleep if it's empty. In pseudo code, here's what Iam doing, but all Iam getting is deadlocks. condition_variable cvAdd; condition

ManualResetEvent WaitOne(timeout) returns early. Any ideas why?

房东的猫 提交于 2021-02-07 20:18:10
问题 I'm using the ManualResetEvent WaitOne(timeout) method and set the timeout value to 30ms. I log using log4net at either side of the WaitOne. The log messages show the WaitOne returned false after only waiting for 22ms. What would cause this? A .Net bug? Thanks in advance. 回答1: From the Win32 documentation on Wait Functions which the .NET methods ultimately use (http://msdn.microsoft.com/en-us/library/ms687069.aspx): Wait Functions and Time-out Intervals The accuracy of the specified time-out

Thread stops randomly in the middle of a while loop

六月ゝ 毕业季﹏ 提交于 2021-02-07 19:42:33
问题 I have a program which is doing something very interesting. Basicly i have three main threads, all are busy and the problem thread basiclly has a while loop that looks like this: while(variable which is always true) { get some data; process data; print message; } print end message; Now the print message gets printed for about the first 400 rounds but after this the thread just stops running. I have tried making this thread a high priority. I have tried reducing the priority of other threads.

org.springframework.core.task.TaskRejectedException while listening for SQS queue?

妖精的绣舞 提交于 2021-02-07 19:40:22
问题 I create a basic workflow of consuming messages from SQS with @SqsListener . It works fine but I constantly get tons of similar messages: org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@372b568[Running, pool size = 3, active threads = 3, queued tasks = 0, completed tasks = 0]] did not accept task: org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer$SignalExecutingRunnable@4c30c2f9 at org.springframework

How should I increment a number for a round robin threading scenario with least contention?

此生再无相见时 提交于 2021-02-07 19:19:43
问题 If many threads are calling GetNextNumber simultaneously with the following code, GetNextNumber will return 1 more times than any other numbers. private class RoundRobbinNumber { private int _maxNumbers = 10; private int _lastNumber; private RoundRobbinNumber(int maxNumbers) { _maxNumbers = maxNumbers; } public int GetNextNumber() { int nextNumber = Interlocked.Increment(ref _lastNumber); if (_lastNumber > _maxNumbers) { Interlocked.CompareExchange(ref _lastNumber, 1, _maxNumbers); nextNumber

How should I increment a number for a round robin threading scenario with least contention?

随声附和 提交于 2021-02-07 19:19:16
问题 If many threads are calling GetNextNumber simultaneously with the following code, GetNextNumber will return 1 more times than any other numbers. private class RoundRobbinNumber { private int _maxNumbers = 10; private int _lastNumber; private RoundRobbinNumber(int maxNumbers) { _maxNumbers = maxNumbers; } public int GetNextNumber() { int nextNumber = Interlocked.Increment(ref _lastNumber); if (_lastNumber > _maxNumbers) { Interlocked.CompareExchange(ref _lastNumber, 1, _maxNumbers); nextNumber

Jetty, where are my five threads?

孤街浪徒 提交于 2021-02-07 18:30:11
问题 I'm using spring+jetty. I'm configuring jetty: @Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() { final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(port); factory.addServerCustomizers((Server server) -> { final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class); threadPool.setMinThreads(minThreads); threadPool.setMaxThreads(maxThreads); threadPool.setIdleTimeout(1000); }); return factory; }

Eclipse RCP multithreading

╄→尐↘猪︶ㄣ 提交于 2021-02-07 17:56:29
问题 I have an eclipse rcp application. And I have a command when this command is executing. I need to start a thread. After execution of this thread GUI must be updated. But I suppose that this thread or other non-SWT thread cannot update GUI. But it seems reasonable. When I was trying to do that I got Exception in thread "Thread-5" org.eclipse.swt.SWTException: Invalid thread access . How I can make this goal? 回答1: Using SWT you need to have anything that updates the GUI be done on the main