Threads Section Index | Page 3
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
In case of images how does thread spawning work? Can I stop the AWT from creating a thread for the image while loading?
You seem to think that a new thread is created for each image you download. That's simply not the case. Most AWT implementations use a small pool of threads (usually 4) for downloading images. The...more
How soon after calling start() will the run() method be executed?
Your run() method is not guarenteed to run immediately after the Thread.start() method is called.
The Java Language Specification gives implementations lots of legroom with respect to the schedul...more
What is piped I/O used for?
The piped I/O streams are for inter-thread communication. They manage the synchronization across thread boundaries of the buffer.
See Is there any way to communicate between two classes... for an ...more
How do I exacute a block of code after the first of two threads finishes, no matter which one finishes first?
You probably want to use object.notify. Like this:
Object lock = new Object();
Thread t1 = new MyThread(lock);
Thread t2 = new MyThread(lock);
synchronized (lock) {
t1.start();
t2.start();...more