java-threads

synchronized object not locked by thread before notifyAll()

柔情痞子 提交于 2019-11-30 09:23:26
问题 I want to have a boolean to notify some sections of the system that a specific service started. For some strange reason I'm getting the error java.lang.IllegalMonitorStateException: object not locked by thread before notifyAll() . What is strange is that the notifyAll() is inside a synchronized block that takes control over the object that I call notifyAll() on. My class starts like this: public class MyService { public static Boolean notifier = Boolean.valueOf(false); @Override public void

java code execution yields to different results in debug without breakpoints and normal run. Is ExecutorService broken?

最后都变了- 提交于 2019-11-29 20:28:08
TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); in debug runs concurrent, but in normal runtime it starts concurrent, but later runs in single thread. I have some code where I start 4 different tasks in ExecutorService . Two of those tasks should finish almost instantly, the other two should run for a while. Those tasks return execution time in seconds in Future<Double> . This code is responsible for task execution and measurement: public Future<Double> measure(int[] arr, ProcessIntArray processIntArray, ExecutorService es) { Callable<Double> task = () -> { long start

How to notify a specific thread in Java

十年热恋 提交于 2019-11-29 14:52:13
How I can call a particular thread in inter-thread communication? In the program below I have two threads t1 and t2 . When I call t1.notify() it raises: Exception in thread "Thread-1" java.lang.IllegalMonitorStateException at java.lang.Object.notify(Native Method) at Shared.methodTwo(NotifyThread.java:43) at Thread2.run(NotifyThread.java:77) Error class Shared { Thread1 t1 ; Thread2 t2 ; void ThreadInit( Thread1 t1 , Thread2 t2 ) { this.t1 = t1 ; this.t2 = t2 ; } synchronized void methodOne() { Thread t = Thread.currentThread(); System.out.println(t.getName()+" is relasing the lock and going

java code execution yields to different results in debug without breakpoints and normal run. Is ExecutorService broken?

谁说胖子不能爱 提交于 2019-11-28 16:30:46
问题 TL:DR ExecutorService executorService = Executors.newFixedThreadPool(8); in debug runs concurrent, but in normal runtime it starts concurrent, but later runs in single thread. I have some code where I start 4 different tasks in ExecutorService . Two of those tasks should finish almost instantly, the other two should run for a while. Those tasks return execution time in seconds in Future<Double> . This code is responsible for task execution and measurement: public Future<Double> measure(int[]

Why Thread.sleep is bad to use

回眸只為那壹抹淺笑 提交于 2019-11-28 05:50:05
Apologies for this repeated question but I haven't found any satisfactory answers yet. Most of the question had their own specific use case: Java - alternative to thread.sleep Is there any better or alternative way to skip/avoid using Thread.sleep(1000) in Java? My question is for the very generic use case. Wait for a condition to complete. Do some operation. Check for a condition. If the condition is not true, wait for some time and again do the same operation. For e.g. Consider a method that creates a DynamoDB table by calling its createAPI table. DynamoDB table takes some time to become

ThreadPoolExecutor with corePoolSize 0 should not execute tasks until task queue is full

China☆狼群 提交于 2019-11-27 06:13:15
问题 I was going through Java Concurrency In Practice and got stuck at the 8.3.1 Thread creation and teardown topic. The following footnote warns about keeping corePoolSize to zero. Developers are sometimes tempted to set the core size to zero so that the worker threads will eventually be torn down and therefore won’t prevent the JVM from exiting, but this can cause some strange-seeming behavior in thread pools that don’t use a SynchronousQueue for their work queue (as newCachedThreadPool does).

If i synchronized two methods on the same class, can they run simultaneously?

旧巷老猫 提交于 2019-11-26 21:26:24
If i synchronized two methods on the same class, can they run simultaneously on the same object ? for example: class A { public synchronized void methodA() { //method A } public synchronized void methodB() { // method B } } I know that I can't run methodA() twice on same object in two different threads. same thing in methodB() . But can I run methodB() on different thread while methodA() is still running? (same object) Both methods lock the same monitor. Therefore, you can't simultaneously execute them on the same object from different threads (one of the two methods will block until the other

Turning an ExecutorService to daemon in Java

旧街凉风 提交于 2019-11-26 18:41:18
I am using an ExecutoreService in Java 1.6, started simply by ExecutorService pool = Executors.newFixedThreadPool(THREADS). When my main thread is finished (along with all the tasks processed by the thread pool), this pool will prevent my program from shutting down until I explicitly call pool.shutdown(); Can I avoid having to call this by somehow turning the internal thread managing used by this pool into a deamon thread? Or am I missing something here. Probably simplest and preferred solution is in Marco13's answer so don't get fooled by vote difference (mine answer is few years older) or

If i synchronized two methods on the same class, can they run simultaneously?

限于喜欢 提交于 2019-11-26 06:58:31
问题 If i synchronized two methods on the same class, can they run simultaneously on the same object ? for example: class A { public synchronized void methodA() { //method A } public synchronized void methodB() { // method B } } I know that I can\'t run methodA() twice on same object in two different threads. same thing in methodB() . But can I run methodB() on different thread while methodA() is still running? (same object) 回答1: Both methods lock the same monitor. Therefore, you can't

Turning an ExecutorService to daemon in Java

自古美人都是妖i 提交于 2019-11-26 06:29:52
问题 I am using an ExecutoreService in Java 1.6, started simply by ExecutorService pool = Executors.newFixedThreadPool(THREADS). When my main thread is finished (along with all the tasks processed by the thread pool), this pool will prevent my program from shutting down until I explicitly call pool.shutdown(); Can I avoid having to call this by somehow turning the internal thread managing used by this pool into a deamon thread? Or am I missing something here. 回答1: Probably simplest and preferred