Java Reference
In-Depth Information
async low
perform
asynchronous
work
make
data
available
perform
security
check
perform
asynchronous
work
request
some data
display
response
classic asynchronous request/response low
FIGUREĀ 9-1: Asynchronous l ow diagram
IMPLEMENTING ASYNCHRONOUS PATTERN
IN PLAIN CODE
Java has supported threads that you can easily use for asynchronous code execution from its initial
design:
public class AsyncRunnable implements Runnable {
public void run() {
System.out.println("Running!");
}
}
To execute the Runnable class, initialize it in a thread and invoke the run method by calling the
start() method on the newly created thread:
(new Thread(new AsyncRunnable())).start();
Although the preceding example is the preferred way to start a thread process, another approach is
to extend the thread class and override the run() method:
public class AsyncThread extends Thread {
public void run() {
System.out.println("Running!");
}
}
To execute the class, instantiate it and then call the start() method:
(new HelloThread()).start();
 
Search WWH ::




Custom Search