Threads Section Index | Page 2
How can I create a daemon thread?
The Thread API allows Threads to be created which are at the mercy of their user threads. This is accomplished simply by passing true to the setDaemon() method and invoking the method off an insta...more
How do I configure a default handler for exceptions that happen in my threads?
The setUncaughtExceptionHandler() method of Thread, introduced in JDK 5.0, allows you to configure this behavior by letting you attach an implementation of Thread.UncaughtExceptionHandler. The int...more
What happens if two threads perform a get of one hashmap at the same time?
[I know a HashMap object is not synchronized if not done explicitly, so in a case where you have to write and read data in a hashmap from different threads you need to synchronize it (or use anoth...more
Is it possible to wake up a sleeping thread?
[I have a thread which is sleeping for a long period of time.Another object has the object of this sleeping thread. In some event , the second object wants to wake up the sleeping thread before i...more
Thread synchronization seems to work using a queue -- that the waiting threads will acquire the released lock in first-in, first-out order. Is this behavior reliable?
Any ordering that you are observing is not mandated by the spec. It is therefore subject to change and should not be relied on (even using a multi-processor machine instead of a single processor o...more
Is there an example of a chat server (aka Threaded Virtual Meeting Place)?
Is there an example of a chat server (aka Threaded Virtual Meeting Place)
?
How do I capture an exception stack trace and put it into a string?
Here's how to print the trace in a string:
Exception ex = new Exception("something went wrong");
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String stacktrace =...more
What is double-checked locking? Does it work?
Double-checked locking (DCL) is an idiom recommended by a number of Java books and articles as a way to reduce synchronization overhead when performing lazy initialization.
The DCL idiom was des...more
What are the threading options available when using hotspot on solaris? What are the performance implications? and defaults for various JVM versions?
http://java.sun.com/docs/hotspot/threads/threads.html
What does it mean to lock an object?
Every object instance in Java has a little piece of data hanging off of it called a "monitor lock." This is similar to a semaphore.
When you use the synchronized
keyword, the current thread at...more
How do I reset a scheduled task so that it stops and then is rescheduled for the next cycle?
I can do it in the following way
class RemindTask extends TimerTask {
public void run() {
System.out.println("run task");
}
}
Timer timer = new Timer();
ti...more
Is Vector's clone method thread-safe?
Sure it is, since it is a Vector which is thread-safe.
Do I have to synchronize read-only access to a text file?
My servlet reads the contents of a text file which I access using a File object.
What issues are involved when this file is read concurrently by several different threads? Do I have to synchronize...more
How to stop a thread which is waiting for a client socket connection?
[Short answer: you can't stop the thread, but you can close the socket, which causes accept to return. -Alex]
Hope that I've understood you properly. So I assume, that you've got something like:
...more
Can we fork another process from Java?
Yes. See Is there some way to implement multi-tasking in Java? and also look at the docs for java.lang.Runtime.exec().
See also What is the difference between multithreading and multitasking (et ...more