Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 4
See "Understanding Java threads, Part 3: Thread scheduling and wait/notify" to learn more about problems associated with using wait()/notify() to manage thread priority.
These days wait/notify has been mostly replaced by java.util.concurrent.locks, which I strongly recommend, especially because thewait/notify construct is often fragile. For instance, code with wait/notify usually doesn't account for spurious wake-ups, making it subject to sporadic errors. java.util.concurrent.locks is not only easier to get right, it's more flexible and performs at least as well. Other (and older) well-designed, high-quality
concurrency constructs for the Java platform include semaphores and atomic variables.
Listing 5 is an example of inter-thread communication using java.util.concurrent.locks.
import java.util.concurrent.locks.*;
import java.util.*;
import java.text.*;
public class lock_example {
public static Lock this_lock = new ReentrantLock();
public static void main(String[] args) throws Exception {
ListenerThread lt = new ListenerThread();
SpeakerThread st = new SpeakerThread();
lt.start();
st.start();
}
public static int message = 0;
public static void report(String label) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss.S");
String time_of_day = sdf.format(now.getTime());
System.out.format("%s: %s\n", time_of_day, label);
}
}
class ListenerThread extends Thread {
public void run() {
Thread this_thread = Thread.currentThread();
while (true) {
lock_example.report("The Listener requests the lock.");
lock_example.this_lock.lock();
lock_example.report("The Listener acquires the lock.");
if (lock_example.message == 0) {
lock_example.report("The Speaker has sent nothing.");
lock_example.report("The Listener will check again later.");
try {
this_thread.sleep(800);
} catch (InterruptedException ie) {
// ie
}
} else {
lock_example.report("The Listener just received " + Integer.toString(lock_example.message) + " from the Speaker.");
lock_example.message = 0;
}
lock_example.report("The Listener releases the lock.");
lock_example.this_lock.unlock();
}
}
}
class SpeakerThread extends Thread {
int counter = 0;
public void run() {
Thread this_thread = Thread.currentThread();
while (true) {
while (0 != lock_example.message) {
try {
lock_example.report("The Speaker waits for the Listener to catch up.");
this_thread.sleep(30);
} catch (InterruptedException ie) {
}
}
counter++;
lock_example.report("The Speaker requests the lock.");
lock_example.this_lock.lock();
lock_example.report("The Speaker acquires the lock.");
lock_example.report("The Speaker takes a nap--a model for real work it might otherwise do.");
try {
this_thread.sleep((int) (2000 * Math.random()));
} catch (InterruptedException ie) {
}
lock_example.message = counter;
lock_example.report("The Speaker releases the lock.");
lock_example.this_lock.unlock();
}
}
}
Listing 6 shows the output from a run of this program.
Learn more about topics related to multithreaded programming and concurrency on the Java platform.
java.util.concurrent
Callable and Runnable
Wait/notify
Thread safety