13.7 - 13.7 Java Threads concurrent units in Java are...

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

View Full Document Right Arrow Icon
1 13.7 Java Threads concurrent units in Java are methods named run() the process in which the run method executes is called a thread a thread is a program unit that is executed independently of other parts of the program Sources for this section Sebesta 13.7 and Horstmann's Big Java, Ch. 23
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Java threads vs Ada tasks Java threads are lightweight tasks – they all run in the same address space Ada tasks are heavy-weight threads – they run in their own address space Ada tasks can run on different processors with different memories, possibly on diff. computers so Java threads require less overhead but cannot run on different machines with different memories
Background image of page 2
3 Java thread introduction ways to define a class with a run method make a subclass of Thread and override Thread's run method make your class (which might already have a superclass different from Thread) implement the interface Runnable, which provides the run method protocol Java threads can be used to implement monitors
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 the Thread class Thread provides services to its subclasses, but is otherwise computationally unrelated the run method describes the actions of the thread and is always overridden the start method starts its thread as a concurrent unit by calling the run method after calling start, control immediately returns to the caller, which continues in parallel
Background image of page 4
5 Extending Thread Class class MyThread extends Thread { public void run() { //Task statements go here } } ... Thread t1 = new MyThread() ; t1.start() ;
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Using the Runnable interface public class MyRunnable implements Runnable { public void run() { //task statements go here } } Runnable r = new MyRunnable() ; Thread t = new Thread(r) ; t.start() ; public interface Runnable { void run() ; }
Background image of page 6
7 Example to print 10 greetings, 1 per second, with time stamp Thu Apr 9 10:00:01 EST 2009 Hello World Thu Apr 9 10:00:02 EST 2009 Hello World Thu Apr 9 10:00:03 EST 2009 Hello World Thu Apr 9 10:00:04 EST 2009 Hello World Thu Apr 9 10:00:05 EST 2009 Hello World Thu Apr 9 10:00:06 EST 2009 Hello World Thu Apr 9 10:00:07 EST 2009 Hello World Thu Apr 9 10:00:08 EST 2009 Hello World Thu Apr 9 10:00:09 EST 2009 Hello World Thu Apr 9 10:00:10 EST 2009 Hello World
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 import java.util.Date ; public class GreetingThread extends Thread { private String greeting ; private static final int INTERVAL = 1000 ; private static final int ITERATIONS = 30 ; public GreetingThread(String greet) { greeting = greet ; } public void run() { try { //for (int i = 0 ; i < ITERATIONS & !isInterrupted() ; i++) { for (int i = 0 ; i < ITERATIONS ; i++) { System.out.println(i + ". " + greeting + " " + new Date() ) ; sleep(INTERVAL) ; } } catch (InterruptedException ie) { System.out.println("Thread " + greeting + " signing off.") ;
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/29/2010 for the course COMPUTER S cps615 taught by Professor Pro during the Spring '10 term at Randolph College.

Page1 / 25

13.7 - 13.7 Java Threads concurrent units in Java are...

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

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