Chapter 11: Threaded Programs - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 11: Threaded Programs

Description:

public class BigMessage extends Applet { private Label ... public void run() { // flash becomes run // unchanged otherwise. Other Java Thread Capabilities ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 16
Provided by: vinod6
Learn more at: https://www.d.umn.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11: Threaded Programs


1
Chapter 11 Threaded Programs
  • Situations where the program is following
    multiple execution paths (how to stop one?)
  • Thread a line of execution
  • Thread class
  • usage
  • extend Thread
  • implement Runnable interface

2
The Need for Threads
  • Consider a simple program performing a repetitive
    graphics action (bouncing a ball, scrolling a
    message, etc.)
  • To stop such a program we want to have the event
    manager catch stop signals
  • But the stop signal may not be caught because the
    processor concentrates on the other process

3
import java.applet. import java.awt. public
class BigMessage extends Applet private Label
prompt new Label("Message ") private
TextField message new TextField(20) private
Button start new Button("Start") private
Button stop new Button("Stop") private Panel
buttonpanel new Panel() private Panel
messagepanel new Panel() private Panel
dummypanel new Panel() private MessageFrame
messageframe null public void init()
messagepanel.add(prompt) messagepanel.add(mes
sage) buttonpanel.add(start)
buttonpanel.add(stop) dummypanel.setLayout(ne
w GridLayout(2,1)) dummypanel.add(messagepane
l) dummypanel.add(buttonpanel)
add(dummypanel)
4
public boolean action(Event e, Object arg)
if (e.target start) if (messageframe
! null) messageframe.dispose()
messageframe new MessageFrame("A Message",

message.getText()) messageframe.flashMessag
e() else if (e.target stop)
if (messageframe ! null)
messageframe.dispose() messageframe
null return true class
MessageFrame extends Frame private
MessageLine display MessageFrame(String
fname, String newm) super(fname)
setForeground(Color.black)
setBackground(Color.white) display new
MessageLine(newm) add(display) pack()
show()
5
public void flashMessage() try
display.initMessage() Thread.sleep(200L)
while (true) display.advanceMessage
() Thread.sleep(200L)
catch (InterruptedException e)
class MessageLine extends Canvas private
final static int VGAP 5 private final static
int HGAP 1 private final static int FSIZE
48 private String themessage private Font
bigFont new Font("Courier",Font.PLAIN,FSIZE)
private int location private int lwidth
6
MessageLine(String newm) int mlength
Math.max(10,2 newm.length()) int lheight
FSIZE 2 VGAP lwidth (FSIZE HGAP)
mlength HGAP resize(lwidth,lheight)
themessage newm public void draw()
Graphics g getGraphics()
g.setXORMode(Color.white) g.setFont(bigFont)
g.drawString(themessage,location,FSIZE
1) public void initMessage()
location lwidth draw() public void
advanceMessage() draw() location -
(FSIZE HGAP) if (location location lwidth draw()
7
Executing Without Threads
  • The message printing process, once started,
    starves out the event handler
  • Events may be recorded, but the system is too
    busy to respond
  • Idea add threads to programs
  • A thread is an executing section of code
  • Threads can be started, stopped, made to sleep,
    suspended, etc.

8
Thread Class
  • Part of java.lang package
  • Thread connected to execution path in program
  • Constructor
  • Thread() - makes a basic thread
  • Threads can be run
  • declare class that extends Thread class, override
    Thread method run() in class
  • declare class that implements interface Runnable
    which provides run() method

9
Thread Methods
  • Methods
  • static void sleep(long ms) throws
    InterruptedException - causes executing thread to
    pause approximately ms milliseconds
  • void start() throws IllegalThreadStateException -
    starts this thread (calls run() at finish)
  • void run() - begin execution of thread (generally
    not called directly)
  • final void stop() throws SecurityException -
    stops execution of this thread

10
First Option Extend Thread
  • Make hog class extend Thread class
  • Add run() method to class
  • Use start() to initiate execution
  • Pause during execution (sleep) to allow others
    access
  • Use stop() to halt thread

11
public class BigMessage extends Applet public
boolean action(Event e, Object arg) if
(e.target start) if (messageframe !
null) messageframe.dispose() messageframe
new MessageFrame("A Message",
message.getText())
messageframe.start() // starts thread running
else if (e.target stop) if
(messageframe ! null) messageframe.stop()
// use stop to halt thread
messageframe.dispose()
messageframe null return true

12
class MessageFrame extends Thread // Thread not
Frame private MessageLine display private
Frame theFrame null // has a Frame object
MessageFrame(String fname, String newm)
theFrame new Frame(fname) // build Frame
aspects theFrame.setForeground(Color.black)
theFrame.setBackground(Color.white)
display new MessageLine(newm)
theFrame.add(display) theFrame.pack()
theFrame.show() public void dispose()
// Add routine to dispose of Frame if
(theFrame ! null) theFrame.dispose()
public void run() // Routine run, override
Thread method try display.initMessage(
) sleep(200L) while (true)
display.advanceMessage() sleep(200L)
catch (InterruptedException e)

13
Second Option Runnable Interf.
  • Have hog implement Runnable interface
  • Add thread instance to class
  • At start of problem routine create thread
  • Have run() implement problem routine
  • Call start() to initiate execution
  • Pause during execution (sleep)
  • Use stop() to halt thread

14
public class BigMessage extends Applet
public boolean action(Event e, Object arg)
if (e.target start) // Unchanged
else if (e.target stop)
messageframe.stopMessage() // Halt execution
// rest unchanged class
MessageFrame extends Frame implements Runnable
private MessageLine display private Thread
displaythread null // Add control thread
public void flashMessage() displaythread
new Thread(this) // Create thread
displaythread.start() // Call start
to run public void stopMessage() // Add
routine to stop thread if (displaythread !
null) displaythread.stop()
displaythread null public void run()
// flash becomes run // unchanged otherwise

15
Other Java Thread Capabilities
  • Methods for stopping execution when a browser
    leaves a page (applet)
  • Methods for synchronizing threads (making
    process(es) wait for others to complete)
  • Methods for grouping threads (and applying
    operators to the entire group)
Write a Comment
User Comments (0)
About PowerShow.com