Skip to content

Tag: multithreading

Can Java handle 10 Thread pool at the same time

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

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

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

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