concurrent-programming

How to stop the execution of Executor ThreadPool in java?

[亡魂溺海] 提交于 2019-11-27 20:31:54
I am working on the Executors in java to concurrently run more threads at a time. I have a set of Runnable Objects and i assign it to the Exceutors.The Executor is working fine and every thing is fine.But after all the tasks are executed in the pool ,the java program is not terminated,i think the Executor takes some time to kill the threads.please anyone help me to reduce the time taken by the executor after executing all tasks. The ExecutorService class has 2 methods just for this: shutdown() and shutdownNow() . After using the shutdown() method, you can call awaitTermination() to block until

Java 8: Parallel FOR loop

馋奶兔 提交于 2019-11-27 10:40:26
问题 I have heard Java 8 provides a lot of utilities regarding concurrent computing. Therefore I am wondering what is the simplest way to parallelise the given for loop? public static void main(String[] args) { Set<Server> servers = getServers(); Map<String, String> serverData = new ConcurrentHashMap<>(); for (Server server : servers) { String serverId = server.getIdentifier(); String data = server.fetchData(); serverData.put(serverId, data); } } 回答1: Read up on streams, they're all the new rage.

STL algorithms and concurrent programming

怎甘沉沦 提交于 2019-11-27 00:47:04
问题 Can any of STL algorithms/container operations like std::fill , std::transform be executed in parallel if I enable OpenMP for my compiler? I am working with MSVC 2008 at the moment. Or maybe there are other ways to make it concurrent? Thanks. 回答1: There are a number of projects that aim at having parallel STL type libraries: OpenMP Multi-Threaded Template Library libstdc++ parallel HPC++ Parallel Standard Template Library Parallel Patterns Library (shamelessly borrowed from AshleysBrain's

How to stop the execution of Executor ThreadPool in java?

ぃ、小莉子 提交于 2019-11-26 22:57:51
问题 I am working on the Executors in java to concurrently run more threads at a time. I have a set of Runnable Objects and i assign it to the Exceutors.The Executor is working fine and every thing is fine.But after all the tasks are executed in the pool ,the java program is not terminated,i think the Executor takes some time to kill the threads.please anyone help me to reduce the time taken by the executor after executing all tasks. 回答1: The ExecutorService class has 2 methods just for this:

What are the main uses of yield(), and how does it differ from join() and interrupt()?

余生颓废 提交于 2019-11-26 19:23:33
I am a little bit confused about the use of yield() method in Java, specifically in the example code below. I've also read that yield() is 'used to prevent execution of a thread'. My questions are: I believe the code below result in the same output both when using yield() and when not using it. Is this correct? What are, in fact, the main uses of yield() ? In what ways is yield() different from the join() and interrupt() methods? The code example: public class MyRunnable implements Runnable { public static void main(String[] args) { Thread t = new Thread(new MyRunnable()); t.start(); for(int i

Is there a Mutex in Java?

不想你离开。 提交于 2019-11-26 18:15:35
Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e) { semaphore.release(); } if an exception happens at the first acquire, the release in the catch block will increase the permits, and the semaphore is no longer a binary semaphore. Will the correct way be? try { semaphore.acquire(); //do stuff } catch (Exception e) { //exception stuff } finally { semaphore.release(); } Will the above code ensure that the

How to have 2 JVMs talk to one another

谁说我不能喝 提交于 2019-11-26 11:42:37
I have the following situation: I have 2 JVM processes (really 2 java processes running separately, not 2 threads) running on a local machine. Let's call them ProcessA an ProcessB . I want them to communicate (exchange data) with one another (e.g. ProcessA sends a message to ProcessB to do something). Now, I work around this issue by writing a temporary file and these process periodically scan this file to get message. I think this solution is not so good. What would be a better alternative to achieve what I want? haylem Multiple options for IPC : Socket-Based (Bare-Bones) Networking not

What are the main uses of yield(), and how does it differ from join() and interrupt()?

偶尔善良 提交于 2019-11-26 08:55:01
问题 I am a little bit confused about the use of yield() method in Java, specifically in the example code below. I\'ve also read that yield() is \'used to prevent execution of a thread\'. My questions are: I believe the code below result in the same output both when using yield() and when not using it. Is this correct? What are, in fact, the main uses of yield() ? In what ways is yield() different from the join() and interrupt() methods? The code example: public class MyRunnable implements

Is there a Mutex in Java?

走远了吗. 提交于 2019-11-26 06:16:06
问题 Is there a Mutex object in java or a way to create one? I am asking because a Semaphore object initialized with 1 permit does not help me. Think of this case: try { semaphore.acquire(); //do stuff semaphore.release(); } catch (Exception e) { semaphore.release(); } if an exception happens at the first acquire, the release in the catch block will increase the permits, and the semaphore is no longer a binary semaphore. Will the correct way be? try { semaphore.acquire(); //do stuff } catch

How to have 2 JVMs talk to one another

跟風遠走 提交于 2019-11-26 02:30:58
问题 I have the following situation: I have 2 JVM processes (really 2 java processes running separately, not 2 threads) running on a local machine. Let\'s call them ProcessA an ProcessB . I want them to communicate (exchange data) with one another (e.g. ProcessA sends a message to ProcessB to do something). Now, I work around this issue by writing a temporary file and these process periodically scan this file to get message. I think this solution is not so good. What would be a better alternative