Download Java Threads

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Threads
•
•
Java Threads May be Created by:
– Extending Thread class
– Implementing the Runnable interface
Extending Thread Class
– calling the start() method for the new object
✴ allocates memory and initializes a new thread in the JVM, and
✴ calls the run() method making the thread eligible to be run by the
JVM.
Extending the Thread Class
class Worker1 extends Thread
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
public class First
{
public static void main(String args[]) {
Worker runner = new Worker1();
runner.start();
System.out.println(“I am the main thread”);
}
}
Implementing the Runnable Interface
public interface Runnable
{
public abstract void run();
}
•
When a class implements Runnable, it must define a run() method
class Worker2 implements Runnable
{
public void run() {
System.out.println(“I am a Worker Thread”);
}
}
Creating the Thread
•
The new class Worker2 does not have access to the static or instance methods such as start() method as it does not extend the Thread class.
• An object of the Thread class is needed because the start() method creates a new
thread of control.
public class Second
{
public static void main(String args[]) {
Runnable runner = new Worker2();
Thread thrd = new Thread(runner);
thrd.start();
System.out.println(“I am the main thread”);
}
}
Java Thread Management
•
•
APIs for managing threads: (deprecated methods)
– suspend() – suspends execution of the currently running thread.
– sleep() – puts the currently running thread to sleep for a specified amount
of time.
– resume() – resumes execution of a suspended thread.
– stop() – stops execution of a thread.
Example - Applets
– Applets have graphics, animation and audio which can be managed as
separate threads.
– There is no sense in running an applet if they are not being displayed
– They may run as a separate thread of control, suspending the thread when
the applet is not being displayed and resuming it when it displayed again.
ClockApplet
import java.applet.*;
import java.awt.*;
public class ClockApplet extends Applet implements Runnable
{
public void run() {
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {}
repaint();
}
}
// this method is called when the applet is started or
// we return to the applet
public void start() {
if ( clockThread == null ) {
clockThread = new Thread(this);
clockThread.start();
}
else
clockThread.resume();
}
// this method is called when we leave the page the applet is on
public void stop() {
if (clockThread != null)
clockThread.suspend();
}
// this method is called when the applet is removed from the cache
public void destroy() {
if (clockThread != null) {
clockThread.stop();
clockThread = null;
}
}
public void paint(Graphics g) {
g.drawString( new java.util.Date().toString(), 10, 30);
}
private Thread clockThread;
}
Java Thread States
•
•
•
•
•
New: When an object for the thread is created (new statement)
Runnable: When a thread’s run() method is invoked, the thread moves from the
New to the Runnable state.
– Calling the start() method allocates memory for the new thread in the JVM
and calls the run() method for the object.
– A running thread and a thread eligible to run are both in the Runnable state.
Blocked: A thread becomes blocked if it performs a blocking statement (doing
I/O), or if it invokes certain Java Thread methods (sleep(), suspend(), etc.).
Dead: When the run() method terminates or when stop() method is called.
isAlive() method returns a Boolean value indicating if the thread is in the Dead
state.
Threads and the JVM
•
•
•
•
•
•
Several threads handle system-level tasks such as memory management and
graphics control which run asynchronously .
Other system-level threads include
– garbage-collector,
– one that handles timer-events, such as call to the sleep() method
– handles events from graphics controls, such as pressing a button, and one
that updates the screen.
The typical implementation of the JVM is on top of a host operating system.
JVM is responsible for thread management.
Windows NT uses one-to-one model.
Solaris 2.6 implements JVM as many-to-many model.