Skip to content

Tag: multithreading

Java: why Thread.sleep() and yield() are static?

Why sleep() and yield() methods are defined as static methods in java.lang.Thread class? Answer The code would only execute when someXThread was executing, in which case telling someYThread to yield would be pointless. So since the only thread worth calling yield on is the current thread, they make the method…

Thread Safe singleton class

I wrote a below Singleton class. I am not sure whether this is thread safe singleton class or not? Can anyone help me with this? Any thoughts on my above Singleton class will be of great help. Updated Code:- I am trying to incorporate Bohemian suggestion in my code. Here is the updated code, I got- Can anyone…

Java Thread Sleep and Interrupted Exception

Why does a sleep thread need a try catch to catch Interrupted Exception? Why does a sleep even emit an Interrupted Exception error? This are the two questions I really wanna find out about in java programming I’ve been searching through google and i’ve still haven’t found a clear explanation…

Thread interruptions are not caught by InterruptedException

I have a controller and a thread that does some work, the controller has an interrupt function that shuts off the threads in emergency situation. the skeleton of my code looks something like this: However, when interrupt is called, the InterruptedException is never caught. What am I doing wrong here? Answer T…