java.util.concurrent

Synchronization vs Lock

白昼怎懂夜的黑 提交于 2019-11-26 17:26:18
问题 java.util.concurrent API provides a class called as Lock , which would basically serialize the control in order to access the critical resource. It gives method such as park() and unpark() . We can do similar things if we can use synchronized keyword and using wait() and notify() notifyAll() methods. I am wondering which one of these is better in practice and why? 回答1: If you're simply locking an object, I'd prefer to use synchronized Example: Lock.acquire(); doSomethingNifty(); // Throws a

Difference between shutdown and shutdownNow of Executor Service

好久不见. 提交于 2019-11-26 17:09:33
I want to know the basic difference between shutdown() and shutdownNow() for shutting down the Executor Service ?As far as I understood shutdown() should be used for graceful shutdown which means all tasks that were runing and queued for processing but not started should be allowed to complete and shutdownNow() does an abrupt shut down meaning that some unfinished tasks are cancelled and unstarted tasks are also cancelled . Is there anything else which is implicit/explicit that I am missing ? P.S: I found another question on SO related to this but not exactly what I want know . In summary, you

How to properly use Java Executor?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:17:43
I've used Java Executors in my multi-threading apps, but I can't seem to figure out when is the best to use each of the following ways: 1. ExecutorService executor=Executors.newFixedThreadPool(50); executor.execute(new A_Runner(... some parameter ...)); executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } 2. int Page_Count=200; ExecutorService executor=Executors.newFixedThreadPool(50); doneSignal=new CountDownLatch(Page_Count); for (int i=0;i<Page_Count;i++) executor.execute(new A_Runner(doneSignal, ... some parameter ...)); doneSignal.await(); executor.shutdown();

Memory Consistency - happens-before relationship in Java [duplicate]

左心房为你撑大大i 提交于 2019-11-26 15:17:06
问题 This question already has answers here : How to understand happens-before consistent (4 answers) Closed last year . While reading Java docs on Memory Consistency errors. I find points related to two actions that creates happen - before relationship: When a statement invokes Thread.start() , every statement that has a happens-before relationship with that statement also has a happens-before relationship with every statement executed by the new thread. The effects of the code that led up to the

Run Java Threads sequentially

≡放荡痞女 提交于 2019-11-26 11:22:36
问题 How will you execute Three threads sequentially? For eg. Thread1, Thread2, Thread3. It is not possible to pass the reference of one Thread to the other and invoke from the run() method. So code should be like this: Thread1.start(); Thread2.start(); Thread3.start(); and out put should be Printing Thread1 Printing Thread2 Printing Thread3 This can be possible by using ThreadPoolExecutor and using a blocking queue but even that is not an acceptable answer. 回答1: Use ExecutorService in java.util

Is ConcurrentHashMap totally safe?

可紊 提交于 2019-11-26 08:59:29
问题 this is a passage from JavaDoc regarding ConcurrentHashMap . It says retrieval operations generally do not block, so may overlap with update operations. Does this mean the get() method is not thread safe? \"However, even though all operations are thread-safe, retrieval operations do not entail locking, and there is not any support for locking the entire table in a way that prevents all access. This class is fully interoperable with Hashtable in programs that rely on its thread safety but not

WAITING at sun.misc.Unsafe.park(Native Method)

穿精又带淫゛_ 提交于 2019-11-26 07:27:57
问题 One of my applications hangs under some period of running under load, does anyone know what could cause such output in jstack: \"scheduler-5\" prio=10 tid=0x00007f49481d0000 nid=0x2061 waiting on condition [0x00007f494e8d0000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) - parking to wait for <0x00000006ee117310> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186) at

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

Difference between shutdown and shutdownNow of Executor Service

巧了我就是萌 提交于 2019-11-26 05:16:06
问题 I want to know the basic difference between shutdown() and shutdownNow() for shutting down the Executor Service ?As far as I understood shutdown() should be used for graceful shutdown which means all tasks that were runing and queued for processing but not started should be allowed to complete and shutdownNow() does an abrupt shut down meaning that some unfinished tasks are cancelled and unstarted tasks are also cancelled . Is there anything else which is implicit/explicit that I am missing ?

How to properly use Java Executor?

旧城冷巷雨未停 提交于 2019-11-26 04:44:36
问题 I\'ve used Java Executors in my multi-threading apps, but I can\'t seem to figure out when is the best to use each of the following ways: 1. ExecutorService executor=Executors.newFixedThreadPool(50); executor.execute(new A_Runner(... some parameter ...)); executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } 2. int Page_Count=200; ExecutorService executor=Executors.newFixedThreadPool(50); doneSignal=new CountDownLatch(Page_Count); for (int i=0;i<Page_Count;i++) executor