Skip to content
Advertisement

Tag: multithreading

How do you stop a java method execution with a timer?

I am trying to stop a long running method after 10 seconds of execution, so far i followed the timer instructions on baeldung. https://www.baeldung.com/java-stop-execution-after-certain-time#1-using-a-timer When the method is a simple call to a thread sleep it works, but when I call my function with sub methods it doesn’t stop. My implementation looks like this: And the way I am calling

Java MultiThreading task, three smokers and one agent

the task consists of three smokers which are three threads and one class called Agent which has three attributes Tobacco, Lighters and Paper. Smokers only have one of these items with them, agent class is supposed to put two items randomly on the table and then Smoker is supposed to check if they are missing those two items and pick

Count only currently active threads

I have a list of Thread and I want to count only currently active threads, but method isAlive() don’t work. How correctly is this or exist other method check for currently active threads. Answer tl;dr Compare the state of the thread: Here is an example making a stream from your list of Thread objects. Or count all active threads: Details

What Happens when a thread doesn’t throw an exception?

I notice, in this javadoc, https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.html that an UncaughtExceptionHandler is used for when an exception occurs but is not caught. But, will that thread fail quietly? I guess so, because it is going about its business asynchronously, but I’m investigating a related issue with one of our processes, and am surprised at only being aware of this now, 10+ years

is synchronized needed in getValue() ? & volatile needed?

I’ve a class in multithreading application: Answer The keyword volatile gives you the visibility aspects and without that you may read some stale value. A volatile read adds a memory barrier such that the compiler, hardware or the JVM can’t reorder the memory operations in ways that would violate the visibility guarantees provided by the memory model. According to the

Advertisement