Skip to content
Advertisement

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 static so you won’t waste time trying to call yield

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 take a

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 is to why this two things happen. Answer Because a

Designing a Guava LoadingCache with variable entry expiry

I am using Guava’s LoadingCache into my project to handle thread-{safe,friendly} cache loading and it works wonderfully well. However, there is a limitation. The current code defining the cache looks like this: I don’t specify an expiry time. The problem is that according to the values of the key, some associated values may expire and others may not. And CacheLoader

Run multi-thread at a time and make thread to run fast

I am trying to run two different threads at a time, but unable to do that. Thread_1 & Thread_2 runs, but difference between them is around 500ms. I am not using wait() or sleep() anywhere in my code. Questions: How to make run thread simultaneously or in parallel? How to make thread run fast? For second question this I used

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 The only possibilities that come to mind: you are

Advertisement