Skip to content
Advertisement

Tag: multithreading

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

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

Thread.sleep behaviour with non-volatile boolean variable

According to the JLS 17 specification section 17.3: For example, in the following (broken) code fragment, assume that this.done is a non-volatile boolean field: The compiler is free to read the field this.done just once, and reuse the cached value in each execution of the loop. This would mean that the loop would never terminate, even if another thread changed

Is Reentrant Lock a Object level lock or Class level lock?

I see in many tutorials for Reentrant lock, they create a new Reentrant lock and resource is injected, lock and unlock of reentrant lock is called in try/finally block. I don’t understand the connection between this lock and resource which is used in the thread. Below is an example of tutorial on Reentrant lock Resource code Resource being used in

Read Large file in Chunks and Compare each line in Java

I have a text file with entries like below. I want to read the file as input and compare the time consumed by each event using Java. Like event1 has taken (11025373 – 11025373) = 4ms time. (start – end) event2 has taken (11025397 – 11025387) = 10ms time. I initially thought to read line by line. Considering the input

Advertisement