I have a Java Application which handles different region for example 10 regions. Each region have different number of tasks. I encounter a problem is that one particular region (Region A) have lots of tasks and the processing time of each task for that region is very long. Therefore, if I use a Single Thread Pool, and tasks of all
Tag: multithreading
java concurrency vs parallelism
I know the difference between the two clearly. Concurrency is performing multiple tasks alternately, while parallelism is performing multiple tasks at once. However, there is confusion in this code because a certain instructor has a different opinion than me. Here is code: I thought that running this code on multiple cores would run with parallelism, not concurrency, the instructor says
Java Socket how to simulate disconection from server
I have a Java program with a client and server sockets, there I want to test that an exception is raised after the server is down. This is the server: This is the client: I start the server socket thread with: And the JUnit test is this: However the test is not performing as I intended, I thought that this
Locking execution within CompletableFuture
I need to post-process result of CompletableFuture.supplyAsync execution to get intermediate result. My code looks following In a result I’m facing with java.util.concurrent.CompletionException: java.lang.IllegalMonitorStateException on .thenAccept(resultPerBatch -> { line Seems like I’m using lock in wrong way but I cannot figure out how to avoid this kind of exception. Answer There’s no guarantee that the Function passed to thenApply and
How to properly null check/lazy load
I have a resource file that I load, when needed, to check if a certain word is among the ones in the file. I want to get rid of the method-wide synchronized keyword. There’s no point in all threads waiting while wordsInFile.contains(word.toLowerCase()); runs. Original: Is this OK? Answer This is a variant of @Stewart answer. You can handle lazy load
How to set the Thread priority on parallel iteration with standard Java API?
I found in this tutorial a way to iterate in parallel a collection of objects by setting threads priority, but this example uses the Burningwave Core library: is there a way to do the same with the standard Java API? I found in this tutorial a way to iterate in parallel a collection of objects by setting threads priority, but
Two threads prints one character of a string twice, one by one?
I have troubles with the following task: Both threads access the object of the class Print and print in reverse order the value of the variable type String, letter by letter, where each letter is printed with a hyphen (–). Example : Input = NAME. Output = E-E-M-M-A-A-N-N. What I’ve done is the following: And it prints usually E-M-A-N-E-M-A-N, but
Why assigning instance variable to local variable?
This is something I see in Spring Boot code for example (in the catch block with webServer variable): Why not just doing this.webServer.stop()? What is the purpose of local variable webServer? Answer The main purpose of the assignment is to avoid producing a NullPointerException when the this.webServer is set to null by a concurrent thread after the null-check and before
Calling a Method in a running Thread Object [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 months ago. Improve this question I’m a Java beginner. I want to call a Method in a running Java Thread Object. It always throws this
Why can’t I add tasks to the thread pool the second time in Java?
I create a thread pool to deal with the task, after deal with the task,I find I can not add and start the other task? How to fix it? If I change the executor by executor = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory(“timeOutThread”)); ,It will run OK.But if the task is canceled because of timeout,do this will