Why do we call the start() method, which in turn calls the run() method? Can’t we directly make a call to run()? Please give an example where there is a difference.
Tag: multithreading
java thread reuse
I have always read that creating threads is expensive. I also know that you cannot rerun a thread. I see in the doc of Executors class: Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. Mind the word ‘reuse’. How do thread pools ‘reuse’ threads? Answer I think I
How do you name a SwingWorker thread? Open to code or best practices
I’m trying to debug a horrible exception that is occurring in a SwingWorker thread. If I could name my SwingWorker threads, then I could better interpret the stack traces I am receiving. It doesn’t seem possible to name a SwingWorker thread (is it?). I’m looking for any best practices or ‘tricks’ to help me properly identify my worker threads, short
Java long running task Thread interrupt vs cancel flag
I have a long running task, something like: The task can be cancelled (a cancel is requested and checkIfCancelRequested() checks the cancel flag). Generally when I write cancellable loops like this, I use a flag to indicate that a cancel has been requested. But, I know I could also use Thread.interrupt and check if the thread has been interrupted. I’m
Impossible to make a cached thread pool with a size limit?
It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create. Here is how static Executors.newCachedThreadPool is implemented in the standard Java library: So, using that template to go on to create a fixed sized cached thread pool: Now if you use this and submit 3 tasks, everything
How to wait for a number of threads to complete?
What is a way to simply wait for all threaded process to finish? For example, let’s say I have: How do I alter this so the main() method pauses at the comment until all threads’ run() methods exit? Thanks! Answer You put all threads in an array, start them all, and then have a loop Each join will block until
Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()
newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization? Answer I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads
Performance of ThreadLocal variable
How much is read from ThreadLocal variable slower than from regular field? More concretely is simple object creation faster or slower than access to ThreadLocal variable? I assume that it is fast enough so that having ThreadLocal<MessageDigest> instance is much faster then creating instance of MessageDigest every time. But does that also apply for byte[10] or byte[1000] for example? Edit:
Thread safety of static blocks in Java
Let’s say I have some Java code: If a thread is initializing SomeClass’s Class object and is in the middle of initializing the values in the static block when a second thread wants to load SomeClass’s Class again, what happens to the static block? Does the second thread ignore it assuming it’s already initialized even though the first thread is