






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of static thread methods in java, their functionality, and how they operate on the current thread. Topics covered include sleep(), currentthread(), yield(), interrupted(), isinterrupted(), interrupt(), wait(), isalive(), getname(), setname(), dumpstack(), setpriority(), and getpriority(). The document also explains thread states and their life cycle.
Typology: Slides
1 / 30
This page cannot be seen from the preview
Don't miss anything!
2 A Survey of static Thread Methods
Many thread methods are static :
A static method belongs to the class—not to any one instance of the class. Therefore, you can execute a static method even if you haven’t instantiated the class—such as the case of main.
( Review: what does it mean when a method is static? )
2 An Overview of the Thread Methods
static void sleep( long millisecs )
—Causes the currently executing thread to sleep (meaning temporarily cease execution) for the specified number of milliseconds.
(Good Test Question…)
2 An Overview of the Thread Methods
static void yield()
—Causes the currently executing thread object to temporarily pause and allow other threads of equal priority to execute.
—You would use the method yield() only if your operating system did not support preemptive multithreading.
static boolean interrupted()
boolean isInterrupted()
void interrupt()
Is the current thread interrupted?
Is a certain named thread interrupted?
Interrupt this thread!
2 A Survey of the Thread Methods
static boolean interrupted()
—True or False.
—Tests whether or not the current thread has been interrupted recently.
—This static method discovers whether or not the current thread has been interrupted.
—Calling this method also resets the “interrupted” status of its argument.
2 A Survey of the Thread Methods
boolean isInterrupted()
—Tests whether or not THIS thread has been interrupted. This method can be used to discover whether any thread has been interrupted.
—Also, calling this method does NOT change the interrupted status of its argument.
2 A Survey of the Thread Methods
void interrupt()
—Interrupts this thread. If called on a thread object that is already currently blocked, the blocking call (such as sleep() or wait() complains about the interrupt() call and terminates with an InterruptedException.
—Sends an interrupt message to a thread. The “interrupted” status of the thread is set to true.
This doesn’t ask a question, it performs an action
2 A Survey of the Thread Methods
boolean isAlive()
—returns true if:
2 A Survey of the Thread Methods
String getName()
—returns the thread name for the thread.
2 A Survey of the Thread Methods
static void dumpStack()
—causes a dump of the execution stack at this instant.
public class MyThread extends Thread { public MyThread() { System.out.println( “MyThread Constructor” ); } }
We start off with our typical class MyThread that extends Thread.
MyThread Constructor java.lang.Exception: Stack trace java.lang.Throwable(java.lang.String) java.lang.Exception(java.lang.String) void java.lang.Thread.dumpStack() void TestMyThread.main(java.lang.String []) After dumpStack()
You see main() was the first thing executed. You read a stack dump from the bottom up, the way it was stacked.
Next, Thread’s static method dumpStack() was called.
Finally Exception was called, followed by Throwable, the Superclass.
2 A Survey of the Thread Methods
final void setPriority( int p )
—This method allows you the change the priority to an integer between 1 and 10.