Multithreading

Multithreading - 1 Summary on Java Multithreading 1. Thread...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1 Summary on Java Multithreading 1. Thread Concept A program may consist of many tasks that can run concurrently. A thread is the flow of execution of a task. In Java, you can launch multiple threads from a program concurrently When your program executes an application, the JRE starts a thread for the main method. When your program executes an applet, the Web browser (or Applet container) starts a thread to run the applet. You can create additional threads to run concurrent tasks in the program. In Java, each task is an instance of the Runnable interface, also called runnable object. A thread is essentially an object that facilitates the execution of a task Multiple threads on multiple CPUs Multiple threads on a single CPU 2. Creating Tasks and Threads Example: Create and run three threads: The first thread prints the letter a 100 times. The second thread prints the letter b 100 times. The third thread prints the integers 1 through 100. T h r e a d 3 T h r e a d 2 T h r e a d 1 T hrea d 3 T hrea d 2 T hrea d 1 // Custom task class public class TaskClass implements Runnable { ... public TaskClass(...) { ... } // Implement the run method in Runnable public void run() { // Tell system how to run custom thread ... } ... } // Client class public class Client { ... public void someMethod() { ... // Create an instance of TaskClass TaskClass task = new TaskClass(...); // Create a thread Thread thread = new Thread(task); // Start a thread thread.start(); ... } ... } java.lang.Runnable TaskClass 2 printChar.java // The task for printing a specified character in specified times public class PrintChar implements Runnable { private char charToPrint; // The character to print private int times; // The times to repeat /** Construct a task with specified character and number of * times to print the character */ public PrintChar( char c, int t) { charToPrint = c; times = t; } /** Override the run() method to tell the system * what the task to perform */ public void run() { for ( int i = 0; i < times; i++) { System. out .print(charToPrint); } } } printNum.java //The task class for printing number from 1 to n for a given n class PrintNum implements Runnable { private int lastNum; /** Construct a task for printing 1, 2, ... i */ public PrintNum( int n) { lastNum = n; } /** Tell the thread how to run */ public void run() { for ( int i = 1; i <= lastNum; i++) { System. out .print(" " + i); } } } TaskThreadDemo.java public class TaskThreadDemo { public static void main(String args) { // Create tasks 3 Runnable printA = new PrintChar('a', 100); Runnable printB = new PrintChar('b', 100); Runnable print100 = new PrintNum(100); // Create threads Thread thread1 = new Thread(printA); Thread thread2 = new Thread(printB); Thread thread3 = new Thread(print100); // Start threads thread1.start(); thread2.start(); thread3.start(); } } 3. The Thread Class The Static yield() Method You can use the yield() method to temporarily release time for other threads. For example, suppose you You can use the yield() method to temporarily release time for other threads....
View Full Document

This note was uploaded on 03/01/2012 for the course CMP 426 taught by Professor Gwangs.jung during the Spring '12 term at CUNY Lehman.

Page1 / 36

Multithreading - 1 Summary on Java Multithreading 1. Thread...

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