thread

thread - Thread class(defined in java.lang package Timer...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Thread class (defined in java.lang package) Timer classes are easy to use and if we just need to create a simple program such as an animation, it is suitable. For more complex threading, we can also use Thread class to create concurrent processing threads. Some methods in Thread class public void start() starts the thread, which causes the run() method to be invoked. (It is called by the runnable object in the client class) public void run() This method must be overrode to provide a code to be executed by the thread. It is invoked by the Java runtime system to execute the thread. public static void sleep(long n) throws InterruptedException pauses the current thread for n milliseconds. (It then throws an InterruptedException.) example: try { Thread.sleep(10000); } catch (InterruptedException ex) { return; } Thread class can be used to be inherited by a class that uses a thread, or used together with an object of a class that implements Runnable interface.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
// Custom thread class public class CustomThread extends Thread { ... public CustomThread(. ..) { ... } // Override the run method in Thread public void run() { // Tell system how to run custom thread ... } ... } // Client class public class Client { ... public someMethod() { ... // Create a thread CustomThread thread = new CustomThread(. ..); // Start a thread thread.start(); ... } ... } Creating threads by extending the Thread class Example1: We modify the example2 from the Timer class to print a character 20 times: public class DisplayChar { public static void main(String[] args) { DisplayCharSequence s1 = new DisplayCharSequence('A'); DisplayCharSequence s2 = new DisplayCharSequence('B'); DisplayCharSequence s3 = new DisplayCharSequence('C'); s1.start(); s2.start(); s3.start(); } } public class DisplayCharSequence extends Thread { private char displayChar; // Constructor public DisplayCharSequence(char c) { displayChar = c; } // Overriding run method to define an action. public void run() { for (int i=0; i<20; i++) System.out.print(displayChar); } }
Background image of page 2
Creating threads by implementing the Runnable interface // Custom thread class public class CustomThread implements Runnable { ... public CustomThread(. ..) { ... } // Implement the run method in Runnable public void run() { // Tell system how to run custom thread ... } ... } // Client class public class Client { ... public static void main(String[] args) { ... // Create an instance of CustomThread CustomThread customThread = new CustomThread(. ..); // Create a thread Thread thread = new Thread(customThread); // Start a thread thread.start(); ... } ... } Example2: We modify the example2 from the Timer class to print a character 20 times: public class DisplayChar { public static void main(String[] args) { DisplayCharSequence s1 = new DisplayCharSequence('A'); DisplayCharSequence s2 = new DisplayCharSequence('B'); DisplayCharSequence s3 = new DisplayCharSequence('C'); Thread thread1 = new Thread(s1); Thread thread2 = new Thread(s2); Thread thread3 = new Thread(s3);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/11/2009 for the course CS 205214 taught by Professor Balasooriya/kouvetakis during the Spring '09 term at ASU.

Page1 / 15

thread - Thread class(defined in java.lang package Timer...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online