Overview

Appendix A - The Thread API

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

static methods
Thread has several static methods that are used without reference to a specific thread. Generally, they implicitly refer to the current thread.
Thread.activeCount()
public static int activeCount()
Returns the number of active threads in the invoking threads ThreadGroup . Convenience method for:
Thread.currentThread().getThreadGroup().activeCount()
See Chapter 10, Thread Groups, for more information.
Thread.currentThread()
public static native Thread currentThread()
Returns a reference to the Thread object associated with the thread that invoked the method. Used in code to determine which thread is currently executing the section of code. See Chapter 3 for more information.
Thread.dumpStack()
public static void dumpStack()
Used only during debugging to print a stack trace for the current thread. Convenience method for:
(new Exception(Trace)).printStackTrace()
Thread.enumerate()
public static int enumerate(Thread[] dest) throws SecurityException
Collects a reference to all the threads in the current threads thread group (and recursively in all its subgroups) and copies these Thread references into dest . A SecurityException might be thrown if the operation is not allowed (by the checkAccess() method of ThreadGroup ). The number of references copied into dest is returned. If dest is too small, the extra Thread references are quietly thrown away. The activeCount() method can be used to estimate the size needed for the dest array. In general, dest should be about twice the size you think youll need to be sure that all the threads get copied into it. This is a convenience method for:
Thread.currentThread().getThreadGroup().enumerate(dest)
See Chapter 10 for more information.
Thread. interrupted ()
public static boolean interrupted()
Returns the interrupted status of the current thread and sets the status false . If this method is called twice in a row, the second call will always return false (provided that the current thread is not re-interrupted between calls!). Related methods: interrupt() and isInterrupted() . See Chapter 5, Gracefully Stopping Threads, for more information.
Thread.sleep(long)
public static native void sleep(long ms) throws InterruptedException
Causes the current thread to stop execution for the specified number of milliseconds (approximately). If the thread is interrupted while sleeping, it wakes up early and throws an InterruptedException . Unlike the wait() method on Object , sleep() does not release any locks while resting. Related method: interrupt() . See Chapter 3 for more information.
Thread.sleep(long, int)
public static void sleep(long ms, int nanoseconds)
        throws InterruptedException
Causes the current thread to stop execution for the specified number of milliseconds plus the specified number of nanoseconds (approximately). If the thread is interrupted while sleeping, it wakes up early and throws an InterruptedException . Unlike the wait() method on Object , sleep() does not release any locks while resting. Related method: interrupt() . See Chapter 3 for more information.
Thread.yield()
public static native void yield()
Causes the current thread to yield the processor to other threads. Generally only threads of equal priority that were waiting to run get a chance. Depending on the VM implementation, lower-priority threads might or might not get a chance to run. This method can be used to better share the processor resources. Related method: Thread.sleep() . See Chapter 6 for more information.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net