Download - 02 Por Que y Como Usar Hilos
-
8/11/2019 02 Por Que y Como Usar Hilos
1/35
V. Desarrollo de aplicaciones con manejo deproceso simultneo y uso de mens
-
8/11/2019 02 Por Que y Como Usar Hilos
2/35
-
8/11/2019 02 Por Que y Como Usar Hilos
3/35
la otra es implementar la interfa& runnable la interfa& runnable tiene solamente un
m*todo definido "ue es el m*todo run
de las dos maneras "ue tenemos de crear un ,ilo la primera es decir e#tendiendo la clase+,read es la "ue ms se utili&a la segunda es simplemente en caso de "ue tengamos unaclase donde "ueremos implementar la ejecuci$n de ,ilos de esa clase ya e#tienda a otraser0a imposible "ue e#tendi*ramos la clase +,read ya "ue en 5a!a no se permite la ,erenciamltiple en este caso ayuda el ,ec,o de tener una interfa& por eso nos dan las dos manerasde crear ,ilos e#tendiendo la clase +,read o implementando la interfa& runnable e#tender laclase +,read es la manera natural de crear ,ilos pero en caso de "ue sea imprescindiblepodemos usar la interfa& runnable.
Arranque de un hilo
%or ltimo el arran"ue de ,ilos se reali&a en dos pasos primero creas un objeto de la clase+,read y en segundo lugar llamas al m*todo run del objeto "ue acabas de crear. /n detallees "ue la llamada al m*todo run no es directa lo "ue tienes "ue ,acer es llamar al m*todostart este llamar a m*todo run. 3dems ,ay algunos m*todos au#iliares "ue nos permiten
una programaci$n ms adecuada "ue usaremos en el ejemplo de la siguiente filmina elm*todo sleep "ue es de la clase +,read en una de sus !ersiones permite un parmetro "uees long lo "ue ,ace esto es "ue el ,ilo el cual llama al m*todo run deja de competir por el%/ durante la cantidad de milisegundos especificada en el parmetro despu*s de "ue esetiempo pase el ,ilo !ol!er a competir por el %/ esto no garanti&a "ue el ,ilo secomen&ar a ejecutar inmediatamente simplemente !a a competir por el %/ con los otros,ilos acti!os al llamar a start automticamente ,ay dos ,ilos ejecutndose el "ue arranc$con start y ob!iamente el "ue ejecut$ la llamada entonces cuando utili&as ,ilosautomticamente siempre tienes por lo menos dos ,ilos compitiendo por el %/. Veamos unejemplo para aterri&ar todos estos conceptos "ue ,emos !isto rpidamente.
l arran"ue de ,ilos se reali&a en dos pasos4
-rear un objeto de la clase +,read.- 1lamar al m*todo run ( ) del objeto creado. 1a llamada al m*todo run ( ) no se ,ace directamente sino "ue se llama al m*todo
start ( ) del objeto esto causa "ue la m"uina !irtual llame a run(). l m*todo sleep(long #) de la clase t,read ocasiona "ue el ,ilo deje de compartir por
los recursos durante # milisegundos despu*s de ese tiempo comien&a a pelear porrecursos otra !e&.
3l llamar a start automticamente ,ay dos ,ilos corriendo el "ue se arranco con start
( ) y el "ue ejecut$ la llamada.
-
8/11/2019 02 Por Que y Como Usar Hilos
4/35
java.lang
Class Thread
java.lang.Object
o java.lang.Thread All Implemented Interfaces:
Runnable
Direct Known ubclasses:!or"#oin$or"erThread
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinWorkerThread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinWorkerThread.html -
8/11/2019 02 Por Que y Como Usar Hilos
5/35
public class Threadextends Objectimplements Runnable
A threadis a thread of e%ecution in a program. The #ava &irtual 'achine allows anapplication to have multiple threads of e%ecution running concurrentl(.
)ver( thread has a priorit(. Threads with higher priorit( are e%ecuted in preference
to threads with lower priorit(. )ach thread ma( or ma( not also be mar"ed as adaemon. $hen code running in some thread creates a new Threadobject* the new
thread has its priorit( initiall( set e+ual to the priorit( of the creating thread* and is a
daemon thread if and onl( if the creating thread is a daemon.
$hen a #ava &irtual 'achine starts up* there is usuall( a single non,daemon thread-which t(picall( calls the method named mainof some designated class. The #ava
&irtual 'achine continues to e%ecute threads until either of the following occurs:
o The exitmethod of class Runtimehas been called and the securit( manager
has permitted the e%it operation to ta"e place.
o All threads that are not daemon threads have died* either b( returning from
the call to the runmethod or b( throwing an e%ception that propagates
be(ond the runmethod.
There are two wa(s to create a new thread of e%ecution. One is to declare a class to
be a subclass of Thread. This subclass should override the runmethod of class
Thread. An instance of the subclass can then be allocated and started. !or e%ample*
a thread that computes primes larger than a stated value could be written as follows:
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; }
public oid run() { !! compute primes larger than minPrime
. . . } }
The following code would then create a thread and start it running:
http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html -
8/11/2019 02 Por Que y Como Usar Hilos
6/35
PrimeThread p = ne" PrimeThread(#$%); p.start();
The other wa( to create a thread is to declare a class that implements the Runnable
interface. That class then implements the runmethod. An instance of the class can
then be allocated* passed as an argument when creating Thread* and started. Thesame e%ample in this other st(le loo"s li"e the following:
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; }
public oid run() {
!! compute primes larger than minPrime . . . } }
The following code would then create a thread and start it running:
PrimeRun p = ne" PrimeRun(#$%); ne" Thread(p).start();
)ver( thread has a name for identification purposes. 'ore than one thread ma( havethe same name. If a name is not specified when a thread is created* a new name is
generated for it.
/nless otherwise noted* passing a nullargument to a constructor or method in this
class will cause a &ullPointer'xceptionto be thrown.
ince:
#DK0.1
ee Also:
Runnable* Runtime.exit(int)* run()* stop()
o Nested Class Summary
2ested 3lasses
Modifier and
Type Class and Description
http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exit(int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exit(int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop() -
8/11/2019 02 Por Que y Como Usar Hilos
7/35
staticclass
Thread.State
A thread state.
staticinterace
Thread.UncaughtExceptionHandler
Interface for handlers invo"ed when a Threadabruptl(
terminates due to an uncaught e%ception.
o Field Summary
!ields
Modifier and Type Field and Description
static intMAX_PRIORITY
The ma%imum priorit( that a thread can have.
static intMIN_PRIORITY
The minimum priorit( that a thread can have.
static intNORM_PRIORITY
The default priorit( that is assigned to a thread.
o Constructor Summary
3onstructors
Constructor and Description
Thread()
Allocates a new Threadobject.
Thread(Runnabletarget)
Allocates a new Threadobject.
Thread(Runnabletarget *tringname)Allocates a new Threadobject.
Thread(*tringname)
Allocates a new Threadobject.
Thread(Thread+roupgroup Runnabletarget)
Allocates a new Threadobject.
Thread(Thread+roupgroup Runnabletarget *tringname)
Allocates a new Threadobject so that it has targetas its run object* has the
specified nameas its name* and belongs to the thread group referred to b(
group.
Thread(Thread+roupgroup Runnabletarget *tringnamelong stac,*i-e)
Allocates a new Threadobject so that it has targetas its run object* has the
specified nameas its name* and belongs to the thread group referred to b(
group* and has the specifiedstack size.
Thread(Thread+roupgroup *tringname)
Allocates a new Threadobject.
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MAX_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MAX_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MIN_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MIN_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#NORM_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#NORM_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable)http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String,%20long)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MAX_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#MIN_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#NORM_PRIORITYhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable)http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String,%20long)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.html -
8/11/2019 02 Por Que y Como Usar Hilos
8/35
o Method Summary
'ethods
Modifier and Type Method and Description
static int
actie!ount()
Returns an estimate of the number of active threads
in the current thread4sthread groupand its
subgroups.
oid
chec"Acce##()
Determines if the currentl( running thread haspermission to modif( this thread.
protected Object
clone()
Throws 3lone2otupported)%ception as a Thread
can not be meaningfull( cloned.
int
countStac"$ra%e#()
Deprecated.The definition of this call depends on suspend(),
which is deprecated. Further, the results of this call
were never well-defined.
static Thread
currentThread()
Returns a reference to the currentl( e%ecuting thread
object.
oid
de#tro&()
Deprecated.
This method was originally designed to destroy thisthread without any cleanup. Any monitors it held
would have remained locked. However, the methodwas never implemented. If if were to eimplemented, it would e deadlock-prone in much
the manner of suspend(). If the target thread held
a lock protecting a critical system resource when itwas destroyed, no thread could ever access this
resource again. If another thread ever attempted to
lock this resource, deadlock would result. !uchdeadlocks typically manifest themselves as "frozen"
processes. For more information, see #hy are
Thread.stop, Thread.suspend and Thread.resume
$eprecated%.
static oid
du%pStac"()
5rints a stac" trace of the current thread to the
standard error stream.
static int
enu%erate(Thread/ tarra0)
3opies into the specified arra( ever( active thread in
the current thread4s thread group and its subgroups.
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#clone()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#countStackFrames()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#currentThread()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#destroy()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#dumpStack()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#enumerate(java.lang.Thread[])http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#clone()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#countStackFrames()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#currentThread()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#destroy()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#dumpStack()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#enumerate(java.lang.Thread[])http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html -
8/11/2019 02 Por Que y Como Usar Hilos
9/35
static1ap2Thread*tac,Trace'lement/3
getAllStac"Trace#()
Returns a map of stac" traces for all live threads.
4lass5oaderget!ontext!la##'oader()
Returns the conte%t 3lass6oader for this Thread.
staticThread.6ncaught'xception7andler
get(e)aultUncaughtExceptionHandler()
Returns the default handler invo"ed when a thread
abruptl( terminates due to an uncaught e%ception.
longgetId()
Returns the identifier of this Thread.
*tringgetNa%e()
Returns this thread4s name.
intgetPriorit&()
Returns this thread4s priorit(.
*tac,Trace'lement/
getStac"Trace()
Returns an arra( of stac" trace elements
representing the stac" dump of this thread.
Thread.*tategetState()
Returns the state of this thread.
Thread+roup
getThread*roup()
Returns the thread group to which this thread
belongs.
Thread.6ncaught'xception7andler
getUncaughtExceptionHandler()
Returns the handler invo"ed when this thread
abruptl( terminates due to an uncaught e%ception.
static boolean
hold#'oc"(Objectobj)
Returns trueif and onl( if the current thread holdsthe monitor loc" on the specified object.
oidinterrupt()
Interrupts this thread.
static boolean
interrupted()
Tests whether the current thread has been
interrupted.
booleani#Alie()
Tests if this thread is alive.
booleani#(ae%on()
Tests if this thread is a daemon thread.
booleani#Interrupted()Tests whether this thread has been interrupted.
oid+oin()
$aits for this thread to die.
oid
+oin(long millis)
$aits at most millismilliseconds for this thread to
die.
http://docs.oracle.com/javase/7/docs/api/java/util/Map.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getAllStackTraces()http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getContextClassLoader()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getDefaultUncaughtExceptionHandler()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getId()http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getName()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getPriority()http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getStackTrace()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getState()http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getUncaughtExceptionHandler()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#holdsLock(java.lang.Object)http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupted()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isAlive()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isDaemon()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isInterrupted()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long)http://docs.oracle.com/javase/7/docs/api/java/util/Map.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getAllStackTraces()http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getContextClassLoader()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getDefaultUncaughtExceptionHandler()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getId()http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getName()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getPriority()http://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getStackTrace()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getState()http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getUncaughtExceptionHandler()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#holdsLock(java.lang.Object)http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupted()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isAlive()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isDaemon()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isInterrupted()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long) -
8/11/2019 02 Por Que y Como Usar Hilos
10/35
oid
+oin(long millis int nanos)
$aits at most millismilliseconds plus nanos
nanoseconds for this thread to die.
oid
re#u%e()
Deprecated.
This method e&ists solely for use with suspend(),which has een deprecated ecause it is deadlock-
prone. For more information, see#hy are
Thread.stop, Thread.suspend and Thread.resume$eprecated%.
oid
run()
If this thread was constructed using a separate
Runnablerun object* then that Runnableobject4s
runmethod is called7 otherwise* this method does
nothing and returns.
oid
#et!ontext!la##'oader(4lass5oadercl)
ets the conte%t 3lass6oader for this Thread.
oid
#et(ae%on(boolean on)
'ar"s this thread as either a daemonthread or a
user thread.
static oid
#et(e)aultUncaughtExceptionHandler(Thread.6ncaught'xception7andlereh)
et the default handler invo"ed when a thread
abruptl( terminates due to an uncaught e%ception*
and no other handler has been defined for thatthread.
oid
#etNa%e(*tringname)
3hanges the name of this thread to be e+ual to theargument name.
oid#etPriorit&(int ne"Priorit0)
3hanges the priorit( of this thread.
oid
#etUncaughtExceptionHandler(Thread.6ncaught'xception7andlereh)
et the handler invo"ed when this thread abruptl(
terminates due to an uncaught e%ception.
static oid
#leep(long millis)
3auses the currentl( e%ecuting thread to sleep-temporaril( cease e%ecution for the specified
number of milliseconds* subject to the precision and
accurac( of s(stem timers and schedulers.
static oid #leep(long millis int nanos)
3auses the currentl( e%ecuting thread to sleep
-temporaril( cease e%ecution for the specifiednumber of milliseconds plus the specified number of
nanoseconds* subject to the precision and accurac(
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long,%20int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#resume()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setContextClassLoader(java.lang.ClassLoader)http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isDaemon()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setName(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setPriority(int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long,%20int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long,%20int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#resume()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setContextClassLoader(java.lang.ClassLoader)http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isDaemon()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setName(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setPriority(int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long,%20int) -
8/11/2019 02 Por Que y Como Usar Hilos
11/35
of s(stem timers and schedulers.
oid
#tart()
3auses this thread to begin e%ecution7 the #ava
&irtual 'achine calls the runmethod of this thread.
oid
#top()
Deprecated.This method is inherently unsafe. !topping a thread
with Thread.stop causes it to unlock all of the
monitors that it has locked 'as a naturalconse(uence of the unchecked ThreadDeath
e&ception propagating up the stack). If any of the
o*ects previously protected y these monitors were
in an inconsistent state, the damaged o*ectsecome visile to other threads, potentially
resulting in aritrary ehavior. +any uses of stop
should e replaced y code that simply modifies
some variale to indicate that the target threadshould stop running. The target thread should check
this variale regularly, and return from its runmethod in an orderly fashion if the variale
indicates that it is to stop running. If the target
thread waits for long periods 'on a conditionvariale, for e&le), the interruptmethod
should e used to interrupt the wait. For more
information, see #hy are Thread.stop,Thread.suspend and Thread.resume $eprecated%.
oid
#top(Thro"ableobj)
Deprecated.This method is inherently unsafe. !ee stop()for
details. An additional danger of this method is that
it may e used to generate e&ceptions that the targetthread is unprepared to handle 'including checked
e&ceptions that the thread could not possily throw,
were it not for this method). For more information,see #hy are Thread.stop, Thread.suspend and
Thread.resume $eprecated%.
oid #u#pend()
Deprecated.
This method has een deprecated, as it is inherentlydeadlock-prone. If the target thread holds a lock on
the monitor protecting a critical system resource
when it is suspended, no thread can access thisresource until the target thread is resumed. If the
thread that would resume the target thread attempts
to lock this monitor prior to calling resume,
deadlock results. !uch deadlocks typically manifest
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop(java.lang.Throwable)http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop(java.lang.Throwable)http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend() -
8/11/2019 02 Por Que y Como Usar Hilos
12/35
themselves as "frozen" processes. For more
information, see #hy are Thread.stop,Thread.suspend and Thread.resume $eprecated%.
*tring
toString()
Returns a string representation of this thread*
including the thread4s name* priorit(* and threadgroup.
static oid
&ield()
A hint to the scheduler that the current thread is
willing to (ield its current use of a processor.
Methods inherited fromclass java.lang.Object
e8uals inali-e get4lass hash4ode noti0noti09ll "ait "ait "ait
o Field Detail
MIN_PRIORITY
public static inal int 1:&PR:OR:TThread?>@n* where nis an integer.
5arameters:
target, the object whose runmethod is invo"ed when this thread is started. If
null* this classes runmethod does nothing.
Thread public Thread(Thread+roupgroup
Runnabletarget)
Allocates a new Threadobject. This constructor has the same effect
as Thread(group target gname)*where gnameis a newl(
generated name. Automaticall( generated names are of the form
>Thread?>@n* where nis an integer.
5arameters:
group, the thread group. If nulland there is a securit( manager* the group is
determined b( ecurit('anager.getThread8roup-.If there is not a securit(
manager or *ecurit01anager.getThread+roup()returns null* the group is set to
the current thread4s thread group.
target, the object whose runmethod is invo"ed when this thread is started. If
null* this thread4s run method is invo"ed.
Throws:
*ecurit0'xception, if the current thread cannot create a thread in the specified
thread group
Thread
public Thread(*tringname)
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.html -
8/11/2019 02 Por Que y Como Usar Hilos
14/35
Allocates a new Threadobject. This constructor has the same effect
as Thread(null null name).
5arameters:
name, the name of the new thread
Thread public Thread(Thread+roupgroup
*tringname)
Allocates a new Threadobject. This constructor has the same effect
as Thread(group null name).
5arameters:
group, the thread group. If nulland there is a securit( manager* the group is
determined b( ecurit('anager.getThread8roup-.If there is not a securit(
manager or *ecurit01anager.getThread+roup()returns null* the group is set to
the current thread4s thread group.
name, the name of the new thread
Throws:
*ecurit0'xception, if the current thread cannot create a thread in the specified
thread group
Thread public Thread(Runnabletarget
*tringname)
Allocates a new Threadobject. This constructor has the same effect
as Thread(null target name).
5arameters:
target, the object whose runmethod is invo"ed when this thread is started. If
null* this thread4s run method is invo"ed.
name, the name of the new thread
Thread public Thread(Thread+roupgroup
Runnabletarget *tringname)
Allocates a new Threadobject so that it has targetas its run object*
has the specified nameas its name* and belongs to the thread group
referred to b( group.
If there is a securit( manager* itschec,9ccessmethod is invo"ed
with the Thread8roup as its argument.
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.ThreadGroup)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.ThreadGroup)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.ThreadGroup) -
8/11/2019 02 Por Que y Como Usar Hilos
15/35
In addition* its chec,Permissionmethod is invo"ed with theRuntimePermission(>enable4ontext4lass5oaderOerride>)
permission when invo"ed directl( or indirectl( b( the constructor ofa subclass which overrides the get4ontext4lass5oaderor
set4ontext4lass5oadermethods.
The priorit( of the newl( created thread is set e+ual to the priorit( of
the thread creating it* that is* the currentl( running thread. Themethod set5riorit(ma( be used to change the priorit( to a new value.
The newl( created thread is initiall( mar"ed as being a daemon
thread if and onl( if the thread creating it is currentl( mar"ed as a
daemon thread. The methodsetDaemonma( be used to changewhether or not a thread is a daemon.
5arameters:
group, the thread group. If nulland there is a securit( manager* the group is
determined b( ecurit('anager.getThread8roup-.If there is not a securit(
manager or *ecurit01anager.getThread+roup()returns null* the group is set to
the current thread4s thread group.
target, the object whose runmethod is invo"ed when this thread is started. If
null* this thread4s run method is invo"ed.
name, the name of the new thread
Throws:
*ecurit0'xception, if the current thread cannot create a thread in the specified
thread group or cannot override the conte%t class loader methods.
Thread public Thread(Thread+roupgroup Runnabletarget
*tringname long stac,*i-e)
Allocates a new Threadobject so that it has targetas its run object*
has the specified nameas its name* and belongs to the thread group
referred to b( group* and has the specifiedstack size.
This constructor is identical to
Thread(Thread+roupRunnable*tring) with the e%ception of the
fact that it allows the thread stac" si9e to be specified. The stac" si9eis the appro%imate number of b(tes of address space that the virtual
machine is to allocate for this thread4s stac". The effect of the#tac"Si,epara!eter" if any" is hi#hly platfor! dependent.
On some platforms* specif(ing a higher value for the stac,*i-e
parameter ma( allow a thread to achieve greater recursion depth
before throwing a *tac,Oerlo"'rror. imilarl(* specif(ing a
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setPriority(int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setPriority(int)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html -
8/11/2019 02 Por Que y Como Usar Hilos
16/35
lower value ma( allow a greater number of threads to e%ist
concurrentl( without throwing an OutO1emor0'rror-or other
internal error. The details of the relationship between the value of
the stac,*i-eparameter and the ma%imum recursion depth and
concurrenc( level are platform,dependent. On so!e platfor!s" the
$alue of the #tac"Si,epara!eter !ay ha$e no effect%hatsoe$er.
The virtual machine is free to treat the stac,*i-eparameter as a
suggestion. If the specified value is unreasonabl( low for theplatform* the virtual machine ma( instead use some platform,specific
minimum value7 if the specified value is unreasonabl( high* the
virtual machine ma( instead use some platform,specific ma%imum.6i"ewise* the virtual machine is free to round the specified value up
or down as it sees fit -or to ignore it completel(.
pecif(ing a value of 9ero for the stac,*i-eparameter will causethis constructor to behave e%actl( li"e the Thread(Thread+roup
Runnable *tring)constructor.
$ue to the platform-dependent nature of the ehavior of this
constructor, e&treme care should e e&ercised in its use. The threadstack size necessary to perform a given computation will likely vary
from one implementation to another. In light of this variation,
careful tuning of the stack size parameter may e re(uired, and the
tuning may need to e repeated for each implementation onwhich an application is to run.
Implementation note: #ava platform implementers are encouraged to
document their implementation4s behavior with respect to the
stac,*i-eparameter.
5arameters:
group, the thread group. If nulland there is a securit( manager* the group is
determined b( ecurit('anager.getThread8roup-.If there is not a securit(manager or *ecurit01anager.getThread+roup()returns null* the group is set to
the current thread4s thread group.
target, the object whose runmethod is invo"ed when this thread is started. If
null* this thread4s run method is invo"ed.name, the name of the new thread
stac,*i-e, the desired stac" si9e for the new thread* or 9ero to indicate that this
parameter is to be ignored.
Throws:
*ecurit0'xception, if the current thread cannot create a thread in the specified
thread group
ince:
http://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#getThreadGroup()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html -
8/11/2019 02 Por Que y Como Usar Hilos
17/35
0.
o Method Detail currentThread
public static ThreadcurrentThread()
Returns a reference to the currentl( e%ecuting thread object.
Returns:
the currentl( e%ecuting thread.
yield
public static oid 0ield()
A hint to the scheduler that the current thread is willing to (ield its
current use of a processor. The scheduler is free to ignore this hint.
;ield is a heuristic attempt to improve relative progression between
threads that would otherwise over,utilise a 35/. Its use should becombined with detailed profiling and benchmar"ing to ensure that it
actuall( has the desired effect.
It is rarel( appropriate to use this method. It ma( be useful for
debugging or testing purposes* where it ma( help to reproduce bugsdue to race conditions. It ma( also be useful when designing
concurrenc( control constructs such as the ones in the
jaa.util.concurrent.loc,spac"age.
sleep public static oid sleep(long millis)
thro"s :nterrupted'xception
3auses the currentl( e%ecuting thread to sleep -temporaril( cease
e%ecution for the specified number of milliseconds* subject to theprecision and accurac( of s(stem timers and schedulers. The thread
does not lose ownership of an( monitors.
5arameters:millis, the length of time to sleep in milliseconds
Throws:
:llegal9rgument'xception, if the value of millisis negative
:nterrupted'xception, if an( thread has interrupted the current thread. The
interrupted statusof the current thread is cleared when this e%ception is thrown.
sleep
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/package-summary.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/package-summary.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html -
8/11/2019 02 Por Que y Como Usar Hilos
18/35
public static oid sleep(long millis
int nanos) thro"s :nterrupted'xception
3auses the currentl( e%ecuting thread to sleep -temporaril( ceasee%ecution for the specified number of milliseconds plus the
specified number of nanoseconds* subject to the precision andaccurac( of s(stem timers and schedulers. The thread does not loseownership of an( monitors.
5arameters:
millis, the length of time to sleep in milliseconds
nanos, A?BBBBBBadditional nanoseconds to sleep
Throws:
:llegal9rgument'xception, if the value of millisis negative* or the value of
nanosis not in the range A?BBBBBB
:nterrupted'xception, if an( thread has interrupted the current thread. The
interrupted statusof the current thread is cleared when this e%ception is thrown.
clone protected Objectclone()
thro"s 4lone&ot*upported'xception
Throws 3lone2otupported)%ception as a Thread can not be
meaningfull( cloned. 3onstruct a new Thread instead.
O$errides&
clonein class Object
Returns:a clone of this instance.
Throws:
4lone&ot*upported'xception, alwa(s
ee Also:4loneable
start
public oid start()
3auses this thread to begin e%ecution7 the #ava &irtual 'achine callsthe runmethod of this thread.
The result is that two threads are running concurrentl(: the current
thread -which returns from the call to the startmethod and the
other thread -which e%ecutes its runmethod.
http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/CloneNotSupportedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/CloneNotSupportedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/CloneNotSupportedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/CloneNotSupportedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html -
8/11/2019 02 Por Que y Como Usar Hilos
19/35
It is never legal to start a thread more than once. In particular* a
thread ma( not be restarted once it has completed e%ecution.
Throws:
:llegalThread*tate'xception, if the thread was alread( started.
ee Also:run()* stop()
run
public oid run()
If this thread was constructed using a separate Runnablerun object*
then that Runnableobject4s runmethod is called7 otherwise* this
method does nothing and returns.
ubclasses of Threadshould override this method.
'pecified (y&
runin interfaceRunnable
ee Also:
start()* stop()* Thread(Thread+roup Runnable *tring)
stop CDeprecated
public inal oid stop()
Deprecated. This method is inherently unsafe. !topping a thread withThread.stop causes it to unlock all of the monitors that it has locked
'as a natural conse(uence of the unchecked ThreadDeathe&ception
propagating up the stack). If any of the o*ects previously protectedy these monitors were in an inconsistent state, the damaged o*ects
ecome visile to other threads, potentially resulting in aritrary
ehavior. +any uses of stopshould e replaced y code that simply
modifies some variale to indicate that the target thread should stop
running. The target thread should check this variale regularly, and
return from its run method in an orderly fashion if the varialeindicates that it is to stop running. If the target thread waits for long
periods 'on a condition variale, for e&le), the interrupt
method should e used to interrupt the wait. For more information,see#hy are Thread.stop, Thread.suspend and Thread.resume
$eprecated%.
!orces the thread to stop e%ecuting.
http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#Thread(java.lang.ThreadGroup,%20java.lang.Runnable,%20java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html -
8/11/2019 02 Por Que y Como Usar Hilos
20/35
If there is a securit( manager installed* its chec,9ccessmethod is
called with thisas its argument. This ma( result in a
*ecurit0'xceptionbeing raised -in the current thread.
If this thread is different from the current thread -that is* the current
thread is tr(ing to stop a thread other than itself* the securit(manager4s chec,Permissionmethod -with a
RuntimePermission(>stopThread>)argument is called in
addition. Again* this ma( result in throwing a *ecurit0'xception
-in the current thread.
The thread represented b( this thread is forced to stop whatever it is
doing abnormall( and to throw a newl( created ThreadDeathobject
as an e%ception.
It is permitted to stop a thread that has not (et been started. If the
thread is eventuall( started* it immediatel( terminates.
An application should not normall( tr( to catch ThreadDeathunless
it must do some e%traordinar( cleanup operation -note that thethrowing of ThreadDeathcauses inall0clauses of tr0statements
to be e%ecuted before the thread officiall( dies. If a catchclause
catches a ThreadDeathobject* it is important to rethrow the object
so that the thread actuall( dies.
The top,level error handler that reacts to otherwise uncaught
e%ceptions does not print out a message or otherwise notif( the
application if the uncaught e%ception is an instance of ThreadDeath.
Throws:
*ecurit0'xception, if the current thread cannot modif( this thread.
ee Also:
interrupt()* chec,9ccess()* run()* start()* ThreadDeath*
Thread+roup.uncaught'xception(ThreadThro"able) *
*ecurit01anager.chec,9ccess(Thread)**ecurit01anager.chec,Permission(jaa.securit0.Permission)
stop
CDeprecatedpublic inal oid stop(Thro"ableobj)
Deprecated. This method is inherently unsafe. !ee stop()for
details. An additional danger of this method is that it may e used togenerate e&ceptions that the target thread is unprepared to handle
'including checked e&ceptions that the thread could not possily
http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadDeath.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html#uncaughtException(java.lang.Thread,%20java.lang.Throwable)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.Thread)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadDeath.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html#uncaughtException(java.lang.Thread,%20java.lang.Throwable)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.Thread)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop() -
8/11/2019 02 Por Que y Como Usar Hilos
21/35
throw, were it not for this method). For more information, see #hy
are Thread.stop, Thread.suspend and Thread.resume $eprecated%.
!orces the thread to stop e%ecuting.
If there is a securit( manager installed* the chec,9ccessmethod ofthis thread is called* which ma( result in a *ecurit0'xception
being raised -in the current thread.
If this thread is different from the current thread -that is* the currentthread is tr(ing to stop a thread other than itself or objis not an
instance of ThreadDeath* the securit( manager4s chec,Permission
method -with the RuntimePermission(>stopThread>)argument
is called in addition. Again* this ma( result in throwing a
*ecurit0'xception-in the current thread.
If the argument objis null* a &ullPointer'xceptionis thrown -inthe current thread.
The thread represented b( this thread is forced to stop whatever it is
doing abnormall( and to throw the Thro"ableobject objas an
e%ception. This is an unusual action to ta"e7 normall(* the stop
method that ta"es no arguments should be used.
It is permitted to stop a thread that has not (et been started. If the
thread is eventuall( started* it immediatel( terminates.
5arameters:obj, the Throwable object to be thrown.
Throws:
*ecurit0'xception, if the current thread cannot modif( this thread.
&ullPointer'xception, if obj is null.
ee Also:
interrupt()* chec,9ccess()* run()* start()* stop()*
*ecurit01anager.chec,9ccess(Thread)**ecurit01anager.chec,Permission(jaa.securit0.Permission)
interrupt
public oid interrupt()
Interrupts this thread.
/nless the current thread is interrupting itself* which is alwa(s
permitted* the chec,9ccessmethod of this thread is invo"ed* which
ma( cause a *ecurit0'xceptionto be thrown.
http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.Thread)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#stop()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.Thread)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html -
8/11/2019 02 Por Que y Como Usar Hilos
22/35
If this thread is bloc"ed in an invocation of the "ait()*"ait(long)*
or "ait(long int)methods of the Objectclass* or of the join()*
join(long)* join(long int)* sleep(long)* or sleep(long
int)* methods of this class* then its interrupt status will be cleared
and it will receive an :nterrupted'xception.
If this thread is bloc"ed in an I
-
8/11/2019 02 Por Que y Como Usar Hilos
23/35
-
8/11/2019 02 Por Que y Como Usar Hilos
24/35
Deprecated. This method has een deprecated, as it is inherently
deadlock-prone. If the target thread holds a lock on the monitor
protecting a critical system resource when it is suspended, no threadcan access this resource until the target thread is resumed. If the
thread that would resume the target thread attempts to lock this
monitor prior to calling resume, deadlock results. !uch deadlockstypically manifest themselves as "frozen" processes. For more
information, see #hy are Thread.stop, Thread.suspend and
Thread.resume $eprecated%.
uspends this thread.
!irst* the chec,9ccessmethod of this thread is called with no
arguments. This ma( result in throwing a *ecurit0'xception -in
the current thread.
If the thread is alive* it is suspended and ma"es no further progressunless and until it is resumed.
Throws:
*ecurit0'xception, if the current thread cannot modif( this thread.
ee Also:chec,9ccess()
resu!e CDeprecated
public inal oid resume()
Deprecated. This method e&ists solely for use with suspend(), whichhas een deprecated ecause it is deadlock-prone. For more
information, see #hy are Thread.stop, Thread.suspend and
Thread.resume $eprecated%.
Resumes a suspended thread.
!irst* the chec,9ccessmethod of this thread is called with no
arguments. This ma( result in throwing a *ecurit0'xception-in
the current thread.
If the thread is alive but suspended* it is resumed and is permitted toma"e progress in its e%ecution.
Throws:
*ecurit0'xception, if the current thread cannot modif( this thread.
ee Also:
chec,9ccess()* suspend()
http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend() -
8/11/2019 02 Por Que y Como Usar Hilos
25/35
setPriority
public inal oid setPriorit0(int ne"Priorit0)
3hanges the priorit( of this thread.
!irst the chec,9ccessmethod of this thread is called with no
arguments. This ma( result in throwing a *ecurit0'xception.
Otherwise* the priorit( of this thread is set to the smaller of the
specified ne"Priorit0and the ma%imum permitted priorit( of the
thread4s thread group.
5arameters:
ne"Priorit0, priorit( to set this thread to
Throws:
:llegal9rgument'xception, If the priorit( is not in the range 1:&PR:OR:T
-
8/11/2019 02 Por Que y Como Usar Hilos
26/35
ee Also:
get&ame()* chec,9ccess()
#etNa!e
public inal *tringget&ame()
Returns this thread4s name.
Returns:
this thread4s name.
ee Also:set&ame(*tring)
#etThread)roup
public inal Thread+roupgetThread+roup()
Returns the thread group to which this thread belongs. This method
returns null if this thread has died -been stopped.
Returns:
this thread4s thread group.
acti$eCount
public static int actie4ount()
Returns an estimate of the number of active threads in the currentthread4s thread groupand its subgroups. Recursivel( iterates over all
subgroups in the current thread4s thread group.
The value returned is onl( an estimate because the number of threads
ma( change d(namicall( while this method traverses internal datastructures* and might be affected b( the presence of certain s(stem
threads. This method is intended primaril( for debugging and
monitoring purposes.
Returns:
an estimate of the number of active threads in the current thread4s thread group andin an( other thread group that has the current thread4s thread group as an ancestor
enu!erate
public static int enumerate(Thread/ tarra0)
http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getName()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setName(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getName()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setName(java.lang.String)http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html -
8/11/2019 02 Por Que y Como Usar Hilos
27/35
3opies into the specified arra( ever( active thread in the current
thread4s thread group and its subgroups. This method simpl( invo"esthe Thread+roup.enumerate(Thread/)method of the current
thread4s thread group.
An application might use the active3ountmethod to get an estimateof how big the arra( should be* however if the array is too short tohold all the threads, the e&tra threads are silently ignored.If it is
critical to obtain ever( active thread in the current thread4s thread
group and its subgroups* the invo"er should verif( that the returnedint value is strictl( less than the length of tarra0.
Due to the inherent race condition in this method* it is recommended
that the method onl( be used for debugging and monitoring
purposes.
5arameters:tarra0, an arra( into which to put the list of threads
Returns:
the number of threads put into the arra(Throws:
*ecurit0'xception, if Thread+roup.chec,9ccess()determines that the current
thread cannot access its thread group
count'tac*Fra!es CDeprecated
public int count*tac,Frames()
Deprecated. The definition of this call depends on suspend(), which
is deprecated. Further, the results of this call were never well-
defined.
3ounts the number of stac" frames in this thread. The thread must be
suspended.
Returns:
the number of stac" frames in this thread.
Throws:
:llegalThread*tate'xception, if this thread is not suspended.
+oin public inal oid join(long millis)
thro"s :nterrupted'xception
$aits at most millismilliseconds for this thread to die. A timeout
of Ameans to wait forever.
http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html#enumerate(java.lang.Thread[])http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html#enumerate(java.lang.Thread[])http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#activeCount()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ThreadGroup.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Deprecated.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#suspend()http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html -
8/11/2019 02 Por Que y Como Usar Hilos
28/35
This implementation uses a loop of this."aitcalls conditioned on
this.is9lie. As a thread terminates the this.noti09llmethod
is invo"ed. It is recommended that applications not use "ait*
noti0* or noti09llon Threadinstances.
5arameters:millis, the time to wait in milliseconds
Throws:
:llegal9rgument'xception, if the value of millisis negative
:nterrupted'xception, if an( thread has interrupted the current thread. The
interrupted statusof the current thread is cleared when this e%ception is thrown.
+oin public inal oid join(long millis
int nanos) thro"s :nterrupted'xception
$aits at most millismilliseconds plus nanosnanoseconds for this
thread to die.
This implementation uses a loop of this."aitcalls conditioned on
this.is9lie. As a thread terminates the this.noti09llmethod
is invo"ed. It is recommended that applications not use "ait*
noti0* or noti09llon Threadinstances.
5arameters:
millis, the time to wait in milliseconds
nanos, A?BBBBBBadditional nanoseconds to waitThrows:
:llegal9rgument'xception, if the value of millisis negative* or the value of
nanosis not in the range A?BBBBBB
:nterrupted'xception, if an( thread has interrupted the current thread. The
interrupted statusof the current thread is cleared when this e%ception is thrown.
+oin public inal oid join()
thro"s :nterrupted'xception
$aits for this thread to die.
An invocation of this method behaves in e%actl( the same wa( as the
invocation
join(A)
Throws:
http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long)http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long) -
8/11/2019 02 Por Que y Como Usar Hilos
29/35
:nterrupted'xception, if an( thread has interrupted the current thread. The
interrupted statusof the current thread is cleared when this e%ception is thrown.
du!p'tac*
public static oid dump*tac,()
5rints a stac" trace of the current thread to the standard error stream.
This method is used onl( for debugging.
ee Also:Thro"able.print*tac,Trace()
setDae!on
public inal oid setDaemon(boolean on)
'ar"s this thread as either a daemonthread or a user thread. The#ava &irtual 'achine e%its when the onl( threads running are all
daemon threads.
This method must be invo"ed before the thread is started.
5arameters:
on, if true* mar"s this thread as a daemon thread
Throws:
:llegalThread*tate'xception, if this thread is alive
*ecurit0'xception, if chec,9ccess()determines that the current thread cannot
modif( this thread
isDae!on
public inal boolean isDaemon()
Tests if this thread is a daemon thread.
Returns:
trueif this thread is a daemon thread7 alseotherwise.
ee Also:
setDaemon(boolean)
chec*Access
public inal oid chec,9ccess()
Determines if the currentl( running thread has permission to modif(
this thread.
http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#printStackTrace()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isDaemon()http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isAlive()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean)http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#printStackTrace()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isDaemon()http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalThreadStateException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#isAlive()http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#checkAccess()http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setDaemon(boolean) -
8/11/2019 02 Por Que y Como Usar Hilos
30/35
If there is a securit( manager* its chec,9ccessmethod is called with
this thread as its argument. This ma( result in throwing a
*ecurit0'xception.
Throws:
*ecurit0'xception, if the current thread is not allowed to access this thread.ee Also:*ecurit01anager.chec,9ccess(Thread)
to'trin#
public *tringto*tring()
Returns a string representation of this thread* including the thread4s
name* priorit(* and thread group.
O$errides&
to*tringin class Object
Returns:
a string representation of this thread.
#etConte,tClass-oader
public 4lass5oaderget4ontext4lass5oader()
Returns the conte%t 3lass6oader for this Thread. The conte%t3lass6oader is provided b( the creator of the thread for use b( code
running in this thread when loading classes and resources. If not set*
the default is the 3lass6oader conte%t of the parent Thread. Theconte%t 3lass6oader of the primordial thread is t(picall( set to the
class loader used to load the application.
If a securit( manager is present* and the invo"er4s class loader is not
nulland is not the same as or an ancestor of the conte%t class loader*
then this method invo"es the securit( manager4s chec,Permission
method with a RuntimePermission(>get4lass5oader>)
permission to verif( that retrieval of the conte%t class loader ispermitted.
Returns:the conte%t 3lass6oader for this Thread* or nullindicating the s(stem class loader
-or* failing that* the bootstrap class loaderThrows:
*ecurit0'xception, if the current thread cannot get the conte%t 3lass6oader
ince:0.=
http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.Thread)http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setContextClassLoader(java.lang.ClassLoader)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimePermission.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkAccess(java.lang.Thread)http://docs.oracle.com/javase/7/docs/api/java/lang/String.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()http://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#setContextClassLoader(java.lang.ClassLoader)http://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimePermission.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.html -
8/11/2019 02 Por Que y Como Usar Hilos
31/35
setConte,tClass-oader
public oid set4ontext4lass5oader(4lass5oadercl)
ets the conte%t 3lass6oader for this Thread. The conte%t
3lass6oader can be set when a thread is created* and allows thecreator of the thread to provide the appropriate class loader* through
get4ontext4lass5oader* to code running in the thread when
loading classes and resources.
If a securit( manager is present* its chec,Permissionmethod is
invo"ed with a RuntimePermission(>set4ontext4lass5oader>)
permission to see if setting the conte%t 3lass6oader is permitted.
5arameters:
cl, the conte%t 3lass6oader for this Thread* or null indicating the s(stem class
loader -or* failing that* the bootstrap class loaderThrows:
*ecurit0'xception, if the current thread cannot set the conte%t 3lass6oader
ince:
0.=
holds-oc*
public static boolean holds5oc,(Objectobj)
Returns trueif and onl( if the current thread holds the monitor loc"
on the specified object.
This method is designed to allow a program to assert that the current
thread alread( holds a specified loc":
assert Thread.holds5oc,(obj);
5arameters:
obj, the object on which to test loc" ownership
Returns:
trueif the current thread holds the monitor loc" on the specified object.
Throws:
&ullPointer'xception, if obj is nullince:
0.
#et'tac*Trace
public *tac,Trace'lement/ get*tac,Trace()
http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimePermission.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityManager.html#checkPermission(java.security.Permission)http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimePermission.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/SecurityException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.htmlhttp://docs.oracle.com/javase/7/docs/api/java/lang/StackTraceElement.html -
8/11/2019 02 Por Que y Como Usar Hilos
32/35
Returns an arra( of stac" trace elements representing the stac" dump
of this thread. This method will return a 9ero,length arra( if thisthread has not started* has started but has not (et been scheduled to
run b( the s(stem* or has terminated. If the returned arra( is of non,
9ero length then the first element of the arra( represents the top of
the stac"* which is the most recent method invocation in these+uence. The last element of the arra( represents the bottom of the
stac"* which is the least recent method invocation in the se+uence.
If there is a securit( manager* and this thread is not the current
thread* then the securit( manager4s chec,Permissionmethod is
called with a RuntimePermission(>get*tac,Trace>)permission
to see if it4s o" to get the stac" trace.
ome virtual machines ma(* under some circumstances* omit one ormore stac" frames from the stac" trace. In the e%treme case* a virtual
machine that has no stac