Java Reference
In-Depth Information
threadingscenarios.Also,youmighthavetoanalyzesomeoneelse'ssourcecodethat
depends on Threading.
Runnable and Thread
Java provides the Runnable interface to identify those objects that supply code for
threadstoexecuteviathisinterface'ssolitary void run() method—athreadreceives
noargumentsandreturnsnovalue.Classesimplement Runnable tosupplythiscode,
and one of these classes is Thread .
Thread providesaconsistentinterfacetotheunderlyingoperatingsystem'sthread-
ing architecture. (The operating system is typically responsible for creating and man-
agingthreads.) Thread makesitpossibletoassociatecodewiththreads,aswellasstart
and manage those threads. Each Thread instance associates with a single thread.
Thread declaresseveralconstructorsforinitializing Thread objects.Someofthese
constructorstake Runnable arguments:youcansupplycodetorunwithouthavingto
extend Thread . Other constructors do not take Runnable arguments: you must ex-
tend Thread and override its run() method to supply the code to run.
Forexample, Thread(Runnable runnable) initializesanew Thread object
to the specified runnable whose code is to be executed. In contrast, Thread()
doesnotinitialize Thread toa Runnable argument.Instead,your Thread subclass
providesaconstructorthatcalls Thread() ,andthesubclassalsooverrides Thread 's
run() method.
Intheabsenceofanexplicitnameargument,eachconstructorassignsauniquedefault
name (starting with Thread- )to the Thread object. Names make it possible to dif-
ferentiate threads. In contrast to the previous two constructors, which choose default
names, Thread(String threadName) lets you specify your own thread name.
Thread also declares methods for starting and managing threads. Table 4-7 de-
scribes many of the more useful methods.
Search WWH ::




Custom Search