Java lang thread class methods

Java lang thread class methods

Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

Copyright © 2006 Sun Microsystems, Inc. All rights reserved.

java.lang
Class Thread

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.

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:

class PrimeThread extends Thread < long minPrime; PrimeThread(long minPrime) < this.minPrime = minPrime; >public void run() < // compute primes larger than minPrime . . . >>

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

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

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:

class PrimeRun implements Runnable < long minPrime; PrimeRun(long minPrime) < this.minPrime = minPrime; >public void run() < // compute primes larger than minPrime . . . >>

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

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

Field Summary
static int MAX_PRIORITY
The maximum priority that a thread can have.
static int MIN_PRIORITY
The minimum priority that a thread can have.
static int NORM_PRIORITY
The default priority that is assigned to a thread.
Constructor Summary
Thread ()
Allocates a new Thread object.
Thread (Runnable target)
Allocates a new Thread object.
Method Summary
static int activeCount ()
Returns the current number of active threads in the VM.
static Thread currentThread ()
Returns a reference to the currently executing thread object.
int getPriority ()
Returns this thread’s priority.
boolean isAlive ()
Tests if this thread is alive.
void join ()
Waits for this thread to die.
void 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.
void setPriority (int newPriority)
Changes the priority of this thread.
static void sleep (long millis)
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
void start ()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
String toString ()
Returns a string representation of this thread, including a unique number that identifies the thread and the thread’s priority.
static void yield ()
Causes the currently executing thread object to temporarily pause and allow other threads to execute.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

MIN_PRIORITY

public static final int MIN_PRIORITY

NORM_PRIORITY

public static final int NORM_PRIORITY

MAX_PRIORITY

public static final int MAX_PRIORITY

Thread

Allocates a new Thread object. This constructor has the same effect as Thread(null, null, gname ) , where gname is a newly generated name. Automatically generated names are of the form «Thread-«+ n, where n is an integer.

Threads created this way must have overridden their run() method to actually do anything. An example illustrating this method being used follows:

import java.lang.*; class plain01 implements Runnable < String name; plain01() < name = null; >plain01(String s) < name = s; >public void run() < if (name == null) System.out.println("A new thread created"); else System.out.println("A new thread with name " + name + " created"); >> class threadtest01 < public static void main(String args[] ) < int failed = 0 ; Thread t1 = new Thread(); if (t1 != null) System.out.println("new Thread() succeed"); else < System.out.println("new Thread() failed"); failed++; >> >

Thread


Allocates a new Thread object. This constructor has the same effect as Thread(null, target, gname ) , where gname is a newly generated name. Automatically generated names are of the form «Thread-«+ n, where n is an integer. Parameters: target — the object whose run method is called.
Method Detail

currentThread

Returns a reference to the currently executing thread object. Returns: the currently executing thread.

yield

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

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. Parameters: millis — the length of time to sleep in milliseconds. Throws: InterruptedException — if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown. See Also: Object.notify()

start

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). Throws: IllegalThreadStateException — if the thread was already started. See Also: run()

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.

Subclasses of Thread should override this method. Specified by: run in interface Runnable See Also: start() , Runnable.run()

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. Returns: true if this thread is alive; false otherwise.

setPriority

public final void setPriority(int newPriority)

Changes the priority of this thread. Parameters: newPriority — priority to set this thread to Throws: IllegalArgumentException — If the priority is not in the range MIN_PRIORITY to MAX_PRIORITY . See Also: getPriority() , getPriority() , MAX_PRIORITY , MIN_PRIORITY

getPriority

public final int getPriority()

Returns this thread’s priority. Returns: this thread’s name. See Also: setPriority(int) , setPriority(int)

activeCount

public static int activeCount()

Returns the current number of active threads in the VM. Returns: the current number of threads in this thread’s thread group.

join

Waits for this thread to die. Throws: InterruptedException — if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

toString

Returns a string representation of this thread, including a unique number that identifies the thread and the thread’s priority. Overrides: toString in class Object Returns: a string representation of this thread. Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to License Terms. Your use of this web site or any of its content or software indicates your agreement to be bound by these License Terms.

For more information, please consult the JSR 30 specification.

Источник

Читайте также:  Php com dotnet so
Оцените статью