Threads Section Index | Page 7
How much overhead is associated with management of a ThreadLocal object ? What is the overhead associated with -- the get() method on the object or with the thread change ?
There is no way to control which thread is notified when you call notify(). Neither thread priorities nor order of entering wait() determine what is notified first. [Any pattern you may notice i...more
What causes ThreadDeath to happen? I've noticed that Thread.sleep() invokes them somehow. But why? How to avoid them?
ThreadDeath is a special Java error that is caught by the runtime system to free system resources used by a thread. You can't stop / avoid them. In fact, if you catch them and don't rethrow them, ...more
There is a very good article at JavaSoft regarding thread dumps and strack traces: http://developer.java.sun.com/developer/technicalArticles/Programming/Stacktrace/index.htmlmore
What is the keyword volatile used for? What sort of situations should I use volatile when developing a multi-threaded application?
Volatile modifiers tells the compiler that the variable can be changed unexpectedly by other part of programme. When you have nore than one thread and you expect any thread can change the variable...more
Use the schedule() method of the java.util.Timer class: long now = System.currentTimeMillis(); Date whenToRun = new Date(now+millisecondsInFuture); Timer timer = new Timer(); TimerTask task = new...more
Use the scheduleAtFixedRate() method of the java.util.Timer class: int initialDelay = 30000; // start after 30 seconds int period = 5000; // repeat every 5 seconds Timer timer = new Timer(...more
Summary: consumer grabs lock ( synchronized (lock) ) consumer calls lock.wait(), releasing monitor lock (!!) producer produces resource producer grabs lock ( synchroni...more
How can I actually, really deallocate a Thread to release the memory? Setting thread = null does not work!
Using thread = null will not release a running thread. In order to release the memory associated with a thread, you need to make sure that all of the following are done: Make sure that the thread...more
Is it possible to interrupt a running thread? InterruptedException is only thrown if the thread is interrupted while in the sleep state and calling interrupt() on a running thread has no effect.
[More info from the questioner: Consider a long running task that is not loop based. How can you "cancel" the task before it reaches the end of the run() method and without having to c...more
Apartment threading is a threading model used by Microsoft with COM/ActiveX objects. You can read about it in Microsoft's technical note at http://support.microsoft.com/support/kb/articles/q150/7/...more
Basically, calling a method "thread-safe" means that even if multiple threads try to access it simultaneously, nothing bad happens. Here "bad" usually means that due to race c...more
How can I synchronize access to my service object so that multiple clients may have read-only access, but a client that needs to write gets exclusive access?
Jini 1.1 includes a utility class called com.sun.jini.thread.ReadersWriter which implements reader-writer locks. You may use this class as a barrier - surrounding all critical sections in your a...more
In this case, the Java Virtual Machine will typically simulate the threading model, although it's up to the JVM vendor how they support it. ["Green threads" is the name of the standard t...more
How do I detect end of stream in a non-blocking manner when reading a stream through URL/URLConnection, if available() reports nothing to read on end of stream?
In the fully general case, you can't (since the client can just keep sending you data). Given that Java doesn't have asynchronous I/O (yet), you need to design your system to take i...more