Class Thread
java.lang.Object
java.lang.Thread
- All Implemented Interfaces:
Runnable
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceHandles what a thread's run method throws. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe maximum priority that a thread can have.static final intThe minimum priority that a thread can have.static final intThe default priority that is assigned to a thread. -
Constructor Summary
ConstructorsConstructorDescriptionThread()Allocates a new Thread object.Allocates a new Thread object with a specific target object whose run method is called.Allocates a new Thread object with the given target and name.Allocates a new Thread object with the given name. -
Method Summary
Modifier and TypeMethodDescriptionstatic intReturns the current number of active threads in the virtual machine.static ThreadReturns a reference to the currently executing Thread object.The handlersetDefaultUncaughtExceptionHandlerset, or null.longgetId()A number that identifies this thread for its lifetime.final StringgetName()Returns this thread's name.final intReturns this thread's priority.This thread's handler, or the default handler when it has none.voidInterrupts this thread.static booleanTests and clears the current thread's interrupt status.final booleanisAlive()Tests if this thread is alive.final booleanisDaemon()Returns whether this thread is a daemon.booleanTests this thread's interrupt status without clearing it.final voidjoin()Waits for this thread to die.final voidjoin(long millis) Waits at mostmillismilliseconds for this thread to die; 0 waits for as long as it takes.final voidjoin(long millis, int nanos) Waits at mostmillismilliseconds plusnanosnanoseconds for this thread to die.voidrun()If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.final voidsetDaemon(boolean on) Marks this thread as a daemon before it is started.static voidSets the handler for what any thread without its own handler throws.final voidChanges this thread's name.final voidsetPriority(int newPriority) Changes the priority of this thread.voidSets the handler for what this thread's run method throws; null leaves it to the default handler.static voidsleep(long millis) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.static voidsleep(long millis, int nanos) Sleeps formillismilliseconds plusnanosnanoseconds.voidstart()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.toString()Returns a string representation of this thread, including the thread's name and priority.static voidyield()Causes the currently executing thread object to temporarily pause and allow other threads to execute.
-
Field Details
-
MAX_PRIORITY
public static final int MAX_PRIORITYThe maximum priority that a thread can have. See Also:Constant Field Values- See Also:
-
MIN_PRIORITY
public static final int MIN_PRIORITYThe minimum priority that a thread can have. See Also:Constant Field Values- See Also:
-
NORM_PRIORITY
public static final int NORM_PRIORITYThe default priority that is assigned to a thread. See Also:Constant Field Values- See Also:
-
-
Constructor Details
-
Thread
public Thread()Allocates a new Thread object. Threads created this way must have overridden their run() method to actually do anything. -
Thread
Allocates a new Thread object with a specific target object whose run method is called. target - the object whose run method is called. -
Thread
-
Thread
Allocates a new Thread object with the given name. Threads created this way must have overridden their run() method to actually do anything. name - the name of the new thread.
-
-
Method Details
-
activeCount
public static int activeCount()Returns the current number of active threads in the virtual machine. -
currentThread
Returns a reference to the currently executing Thread object. -
getName
Returns this thread's name. Note that in CLDC the name of the thread can only be set when creating the thread. -
getPriority
public final int getPriority()Returns this thread's priority. -
interrupt
public void interrupt()Interrupts this thread. In an implementation conforming to the CLDC Specification, this operation is not required to cancel or clean up any pending I/O operations that the thread may be waiting for. -
interrupted
public static boolean interrupted()Tests and clears the current thread's interrupt status. Compile-time declaration; each platform supplies the runtime implementation. -
isInterrupted
public boolean isInterrupted()Tests this thread's interrupt status without clearing it. -
setDaemon
public final void setDaemon(boolean on) Marks this thread as a daemon before it is started. -
isDaemon
public final boolean isDaemon()Returns whether this thread is a daemon. -
isAlive
public final boolean isAlive()Tests if this thread is alive. A thread is alive if it has been started and has not yet died. -
join
Waits for this thread to die.- Throws:
InterruptedException
-
join
Waits at mostmillismilliseconds for this thread to die; 0 waits for as long as it takes.- Throws:
InterruptedException
-
join
Waits at mostmillismilliseconds plusnanosnanoseconds for this thread to die.- Throws:
InterruptedException
-
sleep
Sleeps formillismilliseconds plusnanosnanoseconds.- Throws:
InterruptedException
-
setName
Changes this thread's name. -
getId
public long getId()A number that identifies this thread for its lifetime. -
setUncaughtExceptionHandler
Sets the handler for what this thread's run method throws; null leaves it to the default handler. -
getUncaughtExceptionHandler
This thread's handler, or the default handler when it has none. -
setDefaultUncaughtExceptionHandler
Sets the handler for what any thread without its own handler throws. In a Codename One application it also receives what an event dispatch thread task throws, which then no longer reaches the platform's own error handling. -
getDefaultUncaughtExceptionHandler
The handlersetDefaultUncaughtExceptionHandlerset, or null. -
run
-
setPriority
public final void setPriority(int newPriority) Changes the priority of this thread. -
sleep
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.- Throws:
InterruptedException
-
start
public void start()Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method). -
toString
-
yield
public static void yield()Causes the currently executing thread object to temporarily pause and allow other threads to execute. -
getStackTrace
-