Dela via


Thread Class

Definition

A thread is a thread of execution in a program.

[Android.Runtime.Register("java/lang/Thread", DoNotGenerateAcw=true)]
public class Thread : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.IRunnable
[<Android.Runtime.Register("java/lang/Thread", DoNotGenerateAcw=true)>]
type Thread = class
    inherit Object
    interface IRunnable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
Thread
Derived
Attributes
Implements

Remarks

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs: <ul> <li>The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. <li>All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method. </ul>

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows: <hr><blockquote>

class PrimeThread extends Thread {
                    long minPrime;
                    PrimeThread(long minPrime) {
                        this.minPrime = minPrime;
                    }

                    public void run() {
                        // compute primes larger than minPrime
                        &nbsp;.&nbsp;.&nbsp;.
                    }
                }

</blockquote><hr>

The following code would then create a thread and start it running: <blockquote>

PrimeThread p = new PrimeThread(143);
                p.start();

</blockquote>

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following: <hr><blockquote>

class PrimeRun implements Runnable {
                    long minPrime;
                    PrimeRun(long minPrime) {
                        this.minPrime = minPrime;
                    }

                    public void run() {
                        // compute primes larger than minPrime
                        &nbsp;.&nbsp;.&nbsp;.
                    }
                }

</blockquote><hr>

The following code would then create a thread and start it running: <blockquote>

PrimeRun p = new PrimeRun(143);
                new Thread(p).start();

</blockquote>

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

Added in 1.0.

Java documentation for java.lang.Thread.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Thread()

Allocates a new Thread object.

Thread(Action, String)
Thread(Action)
Thread(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Thread(IRunnable, String)

Allocates a new Thread object.

Thread(IRunnable)

Allocates a new Thread object.

Thread(String)

Allocates a new Thread object.

Thread(ThreadGroup, Action, String, Int64)
Thread(ThreadGroup, Action, String)
Thread(ThreadGroup, Action)
Thread(ThreadGroup, IRunnable, String, Int64, Boolean)

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, belongs to the thread group referred to by group, has the specified stackSize, and inherits initial values for InheritableThreadLocal inheritable thread-local variables if inheritThreadLocals is true.

Thread(ThreadGroup, IRunnable, String, Int64)

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group, and has the specified stack size.

Thread(ThreadGroup, IRunnable, String)

Allocates a new Thread object so that it has target as its run object, has the specified name as its name, and belongs to the thread group referred to by group.

Thread(ThreadGroup, IRunnable)

Allocates a new Thread object.

Thread(ThreadGroup, String)

Allocates a new Thread object.

Fields

MaxPriority

The maximum priority that a thread can have.

MinPriority

The minimum priority that a thread can have.

NormPriority

The default priority that is assigned to a thread.

Properties

AllStackTraces

Returns a map of stack traces for all live threads.

Class

Returns the runtime class of this Object.

(Inherited from Object)
ContextClassLoader

Returns the context ClassLoader for this thread. -or- Sets the context ClassLoader for this Thread.

Daemon

Tests if this thread is a daemon thread. -or- Marks this thread as either a #isDaemon daemon thread or a user thread.

DefaultUncaughtExceptionHandler

Returns the default handler invoked when a thread abruptly terminates due to an uncaught exception. -or- Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
Id

Returns the identifier of this Thread.

IsAlive

Tests if this thread is alive.

IsInterrupted

Tests whether this thread has been interrupted.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Name

Returns this thread's name. -or- Changes the name of this thread to be equal to the argument name.

PeerReference (Inherited from Object)
Priority

Returns this thread's priority. -or- Changes the priority of this thread.

ThreadGroup

Returns the thread group to which this thread belongs.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

UncaughtExceptionHandler

Returns the handler invoked when this thread abruptly terminates due to an uncaught exception. -or- Set the handler invoked when this thread abruptly terminates due to an uncaught exception.

Methods

ActiveCount()

Returns an estimate of the number of active threads in the current thread's java.

CheckAccess()

Determines if the currently running thread has permission to modify this thread.

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CountStackFrames()
Obsolete.

Counts the number of stack frames in this thread.

CurrentThread()

Returns a reference to the currently executing thread object.

Destroy()
Obsolete.

Throws UnsupportedOperationException.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
DumpStack()

Prints a stack trace of the current thread to the standard error stream.

Enumerate(Thread[])

Copies into the specified array every active thread in the current thread's thread group and its subgroups.

Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetStackTrace()

Returns an array of stack trace elements representing the stack dump of this thread.

GetState()

Returns the state of this thread.

HoldsLock(Object)

Returns true if and only if the current thread holds the monitor lock on the specified object.

Interrupt()

Interrupts this thread.

Interrupted()

Tests whether the current thread has been interrupted.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Join()

Waits for this thread to die.

Join(Int64, Int32)

Waits at most millis milliseconds plus nanos nanoseconds for this thread to die.

Join(Int64)

Waits at most millis milliseconds for this thread to die.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
OnSpinWait()

Indicates that the caller is momentarily unable to progress, until the occurrence of one or more actions on the part of other activities.

Resume()
Obsolete.

Throws UnsupportedOperationException.

Run()

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.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
Sleep(Int64, Int32)

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

Sleep(Int64)

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

Start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Stop()
Obsolete.

Throws UnsupportedOperationException.

Stop(Throwable)
Obsolete.

Throws UnsupportedOperationException.

Suspend()
Obsolete.

Throws UnsupportedOperationException.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Yield()

A hint to the scheduler that the current thread is willing to yield its current use of a processor.

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to