Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

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

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(4)

39 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2 A Survey of static Thread Methods
Many thread methods are static :
A static method belongs to the classnot to
any one instance of the class.
Therefore, you can execute a static method
even if you havent instantiated the classsuch as the
case of main.
( Review: what does it mean when a method is
static? )
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

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.