Docsity

Static Thread Methods: Understanding Thread Operations in Java, Slides of Computer Engineering and Programming

Partial preview of the text

Download Static Thread Methods: Understanding Thread Operations in Java and more Slides Computer Engineering and Programming in PDF only on Docsity!

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.

—During this sleep time the thread does not

use any resources of the processor.

(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.

Attention!

The static thread methods

have one thing in common:

they all operate on the

current thread.

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:

  • start() has been called for the thread, and
  • the thread is not yet dead, meaning
  • the run() method has not yet finished and died.

2 A Survey of the Thread Methods

String getName()

—returns the thread name for the thread.

  • don’t mistake this for the reference to the thread.

2 A Survey of the Thread Methods

static void dumpStack()

—causes a dump of the execution stack at this instant.

  • this has great potential—learn how to use it.

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.