object is then created for each object and is started immediately.
Five Thread constructors enable you to specify a Runnable object:
publicThread(Runnable target)
Constructs a new THRead that uses the run method of the specified
target.
publicThread(Runnable target, String name)
Constructs a new Thread with the specified name and uses the run
method of the specified target.
publicThread(ThreadGroup group, Runnable target)
Constructs a new Thread in the specified ThreadGroup and uses the run
method of the specified target. You will learn about ThreadGroup later.
publicThread(ThreadGroup group, Runnable target, String name)
Constructs a new THRead in the specified THReadGroup with the
specified name and uses the run method of the specified target.
publicTHRead(ThreadGroup group, Runnable target, String name,
long stacksize)
Constructs a new THRead in the specified ThreadGroup with the
specified name and uses the run method of the specified target. The
stackSize parameter allows a stack size for the thread to be suggested.
The values appropriate for a stack size are completely system dependent and
the system does not have to honor the request. Such a facility is not needed
by the vast majority of applications, but like most things of this nature, when
you do need it, it is indispensable.
Classes that have only a run method are not very interesting. Real classes define complete state and behavior,
where having something execute in a separate thread is only a part of their functionality. For example,
consider a print server that spools print requests to a printer. Clients invoke the print method to submit print
jobs. But all the print method actually does is place the job in a queue and a separate thread then pulls jobs
from the queue and sends them to the printer. This allows clients to submit print jobs without waiting for the
actual printing to take place.
class PrintServer implements Runnable {
private final PrintQueue requests = new PrintQueue();
public PrintServer() {
new Thread(this).start();
}
public void print(PrintJob job) {
requests.add(job);
}
public void run() {
for(;;)
realPrint(requests.remove());
}
private void realPrint(PrintJob job) {
// do the real work of printing