Title: Event Handling
1Week Four
- Event Handling
- Inner classes
- Event Handling and Inner Classes
- GUI Programming
- Threads and Web Servers
2Event Handling
- Used by the Abstract Window Toolkit (AWT) for
basic GUI - programming
Java 1.0 - Used by Swing -- Better components than
AWT Java 2 - Used by JavaBeans -- reusable software
components, like - Visual
Basic, that can be manipulated - in a
builder tool
3Babysitting
A baby in the home needs attention. Many events
surrounding the baby are not easily
ignored. Events can be of different types.
4One possible source of events
5Babysitting
Before responding to an event, a babysitter
typically takes note of the source and the type
of event.
6Babysitting
Handler
Event type
Event source
The sitter needs to know both the event type and
the event source.
7Event Handling
The window manager software may generate hundreds
of different events. Examples include mouse
clicks, mouse movements, key strokes, and timer
ticks. A programmer is typically interested in a
small subset of all of the possible events that
may occur.
8Events and Event Objects
- Since events may be of different types but still
exhibit some - shared traits (inheritance) Java represents the
event classes - in a hierarchy.
- The root class is called java.util.EventObject.
The only common - feature shared by all events is a source
object. So we find the - following method in the EventObject class
- public Object
getSource()
9Object
Some classes and methods in the event hierarchy.
EventObject
AWTEvent
ActionEvent
ComponentEvent
String getActionCommand()
WindowEvent
InputEvent
boolean isAltDown()
Window getWindow()
MouseEvent
KeyEvent
int getX()
char getKeyChar()
10Event handling usually involves three types of
objects
- Objects are used to hold and report on
information about the event. - Objects are typically the source of events.
- Objects, called listeners, are used to handle
events.
11Example
A mouse object
An event object that describes, say, the x and y
coordinate of where the mouse was clicked
Event data and methods
Event Source
Listener
The listener object has methods that are called
for particular events
12Example
A mouse object
An event object that describes, say, the x and y
coordinate of where the mouse was clicked
Implements MouseListener
A mouse object must be told who its listener is.
The listener object has methods that are called
for particular events
The event object is sent to a listener method
13The Listener
There are different types of Listeners. MouseList
eners WindowListeners ScrollBarListeners Etc.. Th
ese Listeners are interfaces. Remember what an
interface provides? If class X implements an
interface then class X promises to provide (at
least) the methods declared in the interface.
14Some of the Listener Hierarchy
EventListener
KeyListener
ActionListener
MouseListener
abstract void actionPerformed(ActionEvent e)
abstract void mouseClicked(MouseEvent e)
15A Listener Interface
public interface MouseListener void
mouseClicked(MouseEvent e) void
mouseEntered(MouseEvent e) void
mouseExited(MouseEvent e) void
mouseReleased(MouseEvent e)
//What does it mean if I claim that I implement
this interface?
16An Example
// MouseSpyApplet.java import java.applet.Applet
import java.awt.event.MouseEvent import
java.awt.event.MouseListener
17class MouseSpy implements MouseListener public
void mouseClicked(MouseEvent event)
System.out.println("Mouse clicked. x "
event.getX() " y " event.getY())
public void mouseEntered(MouseEvent event)
System.out.println("Mouse entered. x "
event.getX() " y " event.getY())
public void mouseExited(MouseEvent event)
System.out.println("Mouse exited. x "
event.getX() " y " event.getY())
public void mousePressed(MouseEvent event)
System.out.println("Mouse pressed. x "
event.getX() " y " event.getY())
public void mouseReleased(MouseEvent event)
System.out.println("Mouse released. x "
event.getX() " y " event.getY())
I have to provide ALL of these methods!!
18public class MouseSpyApplet extends Applet
public MouseSpyApplet() MouseSpy listener
new MouseSpy() addMouseListener(listener)
19Java Applets need an HTML file.
lthtmlgt ltheadgt lt/bodygt lt/headgt ltBgtSpying on the
mouselt/bgtltpgt ltapplet code"MouseSpyApplet.class"
width400 height200gt lt/appletgt lt/htmlgt
20Another approach
Suppose a friendly sole created this
class public class MouseAdapter implements
MouseListener void mouseClicked(MouseEvent
e) void mouseEntered(MouseEvent e)
void mouseExited(MouseEvent e) void
mouseReleased(MouseEvent e) Now, suppose I
extend this class. What must I provide?
21Only those methods that I am interested in. The
other methods, when called, do nothing. Well
visit this issue again later.
22Inner Classes
- Nested Top Level Classes (not inner)
- Member Classes
- Local Classes
- Anonymous Classes
23Nested Top Level Class
- Nested top-level classes are not inner classes.
- Use as a convenient way to group related classes
- Since the class must be static and has no 'this'
pointer, it - has no access to the instance data of objects
for its - enclosing class.
- It behaves just like a 'normal' class or
interface.
24//NestedTopLevelExample.java class Top
int i,j static class SomeClass //
static makes it top-level nested
int k SomeClass()
System.out.println("Constructi
ng SomeClass")
void foo() System.out.println("Hello")
Top()
System.out.println("Constructing a Top object")
25public class NestedTopLevelExample
public static void main(String args)
Top myTop new Top()
Top.SomeClass myObject new Top.SomeClass()
myObject.foo()
Output Constructing a Top object Constructing
SomeClass Hello
26Member Classes
- Member classes (there is no such thing as a
'member - interface)
- This inner class (it's not a top-level class)
has no static - keyword and can access the members of each
object of - its outer class.
- The class 'appears in every instance'.
-
27- The parent class must declare an instance of an
inner class, before it can invoke the inner
class methods, assign to data fields (including
private ones), and so on. - Unlike nested top-level classes, inner classes
are not directly part of a package and are not
visible outside the class in which they are
nested. - Inner classes are often used for GUI event
handlers.
28// MemberClassExample.java class Top int
i 33 public class SomeClass
// access the outer object's state.
private int k i
SomeClass()
System.out.println("Constructing SomeClass")
void foo()
System.out.println("Hello")
Top() System.out.println("Cons
tructing a Top object")
SomeClass sc new SomeClass()
System.out.println(sc.k)
29 public class MemberClassExample public
static void main(String args) Top
myObject new Top() //
OUTPUT Constructing a Top object Constructing
SomeClass 33
30Local Classes
- A Local class is an inner class. Typically, a
local class - is declared within a method. It is not a member
of an - enclosing class. It is visible only within the
block. - These classes are used primarily as "adapter
classes". - For example, a block of code that creates a
Button object - could use a local class to define a simple
implementation - of the ActionListener Interface. Then it could
instantiate - this simple implementation and pass the
resulting object - to the button's ActionListener method, thereby
connecting - the button to the "callback" code that is
executed when - the button is pressed.
31// Local Class example class Top int i
33 Top()
System.out.println("Constructing a Top object")
// define a class within a
method class Wow
int t Wow()
System.out.println("Building a Wow")
i 8
t 9
Wow h new Wow()
System.out.println(" h.t "
h.t) System.out.println(" i
" i)
32 public class LocalExample public
static void main(String args) Top
myObject new Top() //
OUTPUT Constructing a Top object Building a Wow
h.t 9 i 8
33Anonymous Classes
- An anonymous class is refinement of inner
classes. - It allows you to combine the definition of the
class - with the instance allocation.
- Since it is instantiated in the same expression
that defines - it, it can only be instantiated once. This is
very similar to - local classes.
- When writing a simple adapter class, the choice
between - a named local class and an unnamed anonymous
class - typically comes down to a matter of style and
code clarity, - rather than any difference in functionality.
- The new class can't have a constructor.
34// Anonymous.java interface SmallClass
public void foo() class Top int i
33 void someMethod(SmallClass s)
s.foo() void anotherMethod()
someMethod(new SmallClass()
public void foo()
System.out.println(
"Really fun")
)
35 Top()
System.out.println("Constructing a Top object")
someMethod(new SmallClass()
public void foo()
System.out.println("
Strange but fun")
)
36 public class Anonymous public static
void main(String args) // We
can't create interface objects //
error SmallClass s new SmallClass()
Top myObject new Top()
myObject.anotherMethod() //
OUTPUT Constructing a Top object Strange but
fun Really fun
37Event Handling and Inner Classes
Source Object
Listener Object
Event object
fire events
After the listener object registers itself with
the source object, the source object calls a
method found in the listener object and passes an
object that describes the event.
38Event Handling
Suppose we have a Button object Button b new
Button() We must determine what events a
particular component generates. A Button
component may generate an ActionEvent object.
Button b
ActionEvent Object
39Implementing a Listener
- We need an object that will listen for the
ActionEvent. - We implement the ActionListener class (this class
listens - for ActionEvents from buttons, menus, etc.) and
override - its actionPerformed method.
class BabySitter implements ActionListener
public void actionPerformed(ActionEvent e)
// handle the event object e in some
way
40Create a BabySitter object
BabySitter sitter new BabySitter()
sitter
41Registering The Listener
- We want to listen for the ActionEvent object
coming from the button. - So, we tell the button what object will listen
for the ActionEvent object - b.addActionListener(sitter)
42The button and its listener
addActionListener
Button Object
Sitter Object
The button calls the actionPerformed() method of
the sitter object and passes an ActionEvent
object as a parameter
43Once again but with a window
Hit the X and the program exits
44Create a WindowCloseSitter Class
import javax.swing. import java.awt.event. p
ublic class CloseDemo public static void
main(String args) JFrame f new
JFrame("Example") f.setSize(400,100)
f.setVisible(true) WindowCloseSitter h
new WindowCloseSitter()
f.addWindowListener(h)
45 But we have to implement ALL of the
functions !!
class WindowCloseSitter implements WindowListener
public void windowClosing(WindowEvent e)
System.exit(0) public
void windowClosed(WindowEvent e) public
void windowOpened(WindowEvent e) public
void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
46Java Provides Adapter Classes
- ComponentAdapter
- MouseMotionAdapter
- WidowAdapter
- ContainerAdapter
- MouseAdapter
- FocusAdapter
- KeyAdapter
47The WindowAdapter class
public abstract class WindowAdapter implements
WindowListener public void
windowClosing(WindowEvent e) public void
windowClosed(WindowEvent e) public void
windowOpened(WindowEvent e) public void
windowIconified(WindowEvent e) public
void windowDeiconified(WindowEvent e)
public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
A built in class -- we already have the empty
bodies!!
48The Window again
import javax.swing. import java.awt.event. pub
lic class CloseDemo2 extends WindowAdapter
public static void main(String args)
JFrame f new JFrame("Example")
f.setSize(400,100) f.setVisible(true)
f.addWindowListener(new CloseDemo2())
public void windowClosing(WindowEvent
e) System.exit(0)
49Again with anonymous classes
import javax.swing. import java.awt.event. pub
lic class CloseDemo3 public static void
main(String args) JFrame f new
JFrame("Example") f.setSize(400,100)
f.setVisible(true) f.addWindowListener(
new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0)
)
50GUI Programming with threads
Java GUI programming and Java Threads GUI
example taken from Computing Concepts with Java
2 by Cay Horstmann Thread examples taken from
The Java Programming Language By Arnold and
Gosling and from Cay Horstmanns Core Java 2
Advanced
51Frame Windows
A Frame window has a border and a title bar A
Frame window has an addWindowListener method. We
can use this method to add listeners to our frame
window.
52An example
javax means Java standard extension
import javax.swing. import java.awt.event. pu
blic class InternetFrameExample public
static void main(String args) JFrame
f new InternetFrame("Example")
f.setTitle("Internet browser") f.show()
Tell the window manager to display the frame.
53class InternetFrame extends JFrame public
InternetFrame(String s)
setSize(300,300)
WindowCloser listener new WindowCloser()
addWindowListener(listener) private
class WindowCloser extends WindowAdapter
public void windowClosing(WindowEvent e)
System.exit(0)
Add the handler
windowOpened() widowClosed() windowClosing()
54(No Transcript)
55Adding User Interface Components to a Frame
- Do not draw directly on the surface of a frame.
- Frames have been designed to arrange user
interface components. - User interface components are such things as
buttons, menus, - scroll bars, and so on.
- If you want to draw on a frame, draw on a
separate component and - then add that component to the frame. The Swing
UI toolkit - provides the Jpanel class for this purpose.
56Drawing on a JPanel
To draw on a Jpanel you override the
paintComponent method. Make sure that from
within your paintComponent method you call
super.paintComponent() so that the superclass
method paintComponent has a chance to erase the
existing contents, redraw the borders and
decorations, etc.
57Adding the Panel to the JFrame
- The surface of a Swing frame is covered with four
panes. - Each of these four has a purpose.
- We are interested in getting access to the
- JFrames content pane.
- So, call the getContentPane on the JFrame.
- This call returns a Container object.
- Add your Panel object to the content pane
Container.
58Adding a Panel to a JFrame
x getContentPane (a contentPane
holds components for display).
JFrame
getContentPane()
x refers to a Container (may contain other
components)
c a Panel or some other component.
Add c to x using xs layout manager (a content
pane uses border layout)
59Adding a JTextfield to the JFrame
JFrame
class MyFrame extends JFRAME private
JTextField textField public MyFrame()
Container cp getContentPane()
textField new JTextField()
cp.add(textField, South)
getContentPane()
cp
60We may want to add a listener to the TextField
JFrame
Class MyFrame extends JFRAME private
JTextField textField public myFrame()
Container cp getContentPane()
textField new JTextField()
cp.add(textField, South)
textField.addActionListener( new
TextFieldListener())
getContentPane()
cp
61Output first! -- user enters number of eggs and
we draw them
62Strategy
- Think about
- What do we want on the screen?
- What events should we listen for?
- What should we do when those events occur?
- What processing will we do when user input
arrives? - What object has responsibilities for what
activities?
- Think about
- The has-a relationship,e.g., the Jframes
ContentPane has-a - Panel and a TextField.
- The is-a relationship,e.g., The
TextFieldListener is-an - actionListener.
63// Example // Eggs.java import
java.awt.Container import java.awt.Graphics impo
rt java.awt.Graphics2D import java.awt.event.Acti
onEvent import java.awt.event.ActionListener imp
ort java.awt.event.WindowAdapter import
java.awt.event.WindowEvent import
java.awt.geom.Ellipse2D import
java.util.Random import javax.swing.JFrame impor
t javax.swing.JPanel import javax.swing.JTextFiel
d
We need classes from the awt, util, and
swing packages.
64 public class Eggs public static void
main(String args) EggFrame frame new
EggFrame() frame.setTitle("Enter number of
eggs") frame.show()
This thread is done after creating a frame
and starting up the frame thread.
A frame now exists and is running.
65The EggFrame Constructor
- Set the size of the frame
- Add a listener to listen for the stop event
- Create a JPanel object to draw on and a
JTextField object to interact with the user via
the keyboard - Add a listener for the JTextField
- Add the Jpanel and the JTextField to the
- contentPane container.
66class EggFrame extends JFrame private
JTextField textField private EggPanel
panel public EggFrame() final
int DEFAULT_FRAME_WIDTH 300 final int
DEFAULT_FRAME_HEIGHT 300
setSize(DEFAULT_FRAME_WIDTH,
DEFAULT_FRAME_HEIGHT)
addWindowListener(new WindowCloser())
panel new EggPanel() textField new
JTextField() textField.addActionListener(n
ew TextFieldListener()) Container
contentPane getContentPane()
contentPane.add(panel, "Center")
contentPane.add(textField, "South")
A listener for the jframe window
A textField listener
As before
67Event driven programming
The constructor will be called from our main
thread. The other thread operates
asynchronously. What do we mean by asynchronous
execution? Who is running the show? Dont
programs run sequentially? We have to think
differently.
68The TextField
The TextField object will call us when it
detects an event. We dont read the input. We
set up a babysitter to respond. The TextField
object sends us an event object.
69 // Use an inner class to listen on
the text field private class
TextFieldListener implements ActionListener
public void actionPerformed(Action
Event event)
String input textField.getText()
panel.setEggCount(Integer.parseInt(input))
textField.setText("")
- We do two things when we
- have a textfield event.
- Get the data
- Tell the panel the number
- of eggs to display
70 // Use an inner class to listen on
the text field private class
TextFieldListener implements ActionListener
public void actionPerformed(Action
Event event)
String input textField.getText()
panel.setEggCount(Integer.parseInt(input))
textField.setText("")
Note how handy the inner class is. Both panel and
textField are available. What if the listener
were not an object of an inner class. how would
it get access to these variables? We cant just
pass the variables on the call because we dont
make the call.
71 private class WindowCloser extends
WindowAdapter public void
windowClosing(WindowEvent event)
System.exit(0)
This is how we respond when the close signal is
received. system.exit(0) stops the java virtual
machine. 0 means normal termination.
72How about the panel object
What are the panel objects responsibilities? ge
t input? _____ repaint itself?
_____ keep track of the egg count?
_____ hold the data it needs to
repaint itself? _____ Do we want our panel to
inherit properties and methods from any existing
classes? _____
73How about the panel object
What are the panel objects responsibilities? ge
t input? No, thats the TextFields job.
repaint itself? Sure, thats its main job.
keep track of the egg count? Yes,
better here where its
needed
hold the data it needs to repaint itself? Yes Do
we want our panel to inherit properties from any
existing classes? Sure, we want to re-use
existing code whenever possible.
74How about the panel object
When should the panel object repaint
itself? What will the panel need to repaint
itself? Who actually calls the paintComponent
method?
75How about the panel object
When should the panel object repaint itself?
When a new input arrives from the user.
When the egg count changes. What will the
panel need to repaint itself? A graphics
object to draw on. Who actually calls the
paintComponent method? While we have to
provide a paintComponent method we dont
call it directly. Its called by the Java
run-time environment after we make a call
on repaint.
76class EggPanel extends JPanel public void
paintComponent(Graphics g)
super.paintComponent(g) Graphics2D g2
(Graphics2D)g // draw eggCount
ellipses with random centers Random
generator new Random() for (int i 0 i
lt eggCount i) double x getWidth()
generator.nextDouble() double y
getHeight() generator.nextDouble()
Ellipse2D.Double egg new Ellipse2D.Double(x, y,
EGG_WIDTH, EGG_HEIGHT)
g2.draw(egg)
77 public void setEggCount(int count)
eggCount count repaint()
private int eggCount private static final
double EGG_WIDTH 30 private static final
double EGG_HEIGHT 50
78Java Threads
- Four kinds of thread programming
- Applications
- A GUI application
- A server application
79Four kinds of thread programming
- 1) Unrelated threads
- 2) Related but unsynchronized threads
- 3) Mutually-exclusive threads
- 4) Communicating mutually-exclusive
- threads
We will look at only the first two kinds.
80 Unrelated threads
class Coffee extends Thread Coffee(String
name) super(name) public void run()
for(int n 1 n lt 3 n)
System.out.println("I like coffee") yield()
System.out.println(this.getName()) yield()
81class Tea extends Thread Tea(String name)
super(name) public void run()
for(int n 1 n lt 3 n) System.out.printl
n("I like tea") yield() System.out.println(thi
s.getName()) yield()
82public class Drinks public static void
main(String args)
System.out.println("I am main")
Coffee t1 new Coffee("Wawa Coffee") Tea
t2 new Tea(Sleepy Time Tea") t1.start()
t2.start()
System.out.println("Main is done")
83Output
I am main Main is done I like coffee I like
tea Wawa Coffee Sleepy Time Tea I like coffee I
like tea Wawa Coffee Sleepy Time Tea I like
coffee I like tea Wawa Coffee Sleepy Time Tea
Main finishes right away
Threads are sharing time
This program has three threads.
84Unrelated Threads Part II
- Using sleep() in unrelated threads
- The call sleep(millis) puts the currently
executing thread to - sleep for at least the specified number of
milliseconds. "At - least means there is no guarantee the thread
will wake up - in exactly the specified time. Other thread
scheduling can - interfere.
-
85class Coffee extends Thread Coffee(String
name) super(name) public void run()
for(int n 1 n lt 3 n)
System.out.println("I like coffee")
try
sleep(1000) // 1 second
catch(InterruptedExce
ption e) System.out.println(this.
getName())
86class Tea extends Thread
Tea(String name) super(name)
public void run() for(int n 1 n lt 5
n) System.out.println("I like
tea") System.out.println(getName())
87public class Drinks2 public static void
main(String args)
System.out.println("I am main")
Coffee t1 new Coffee("Wawa Coffee")
Tea t2 new Tea("China Tea")
t1.start() t2.start()
System.out.println("Main is done")
88I am main Main is done I like coffee I like
tea China Tea I like tea China Tea I like
tea China Tea I like tea China Tea I like
tea China Tea Wawa Coffee I like coffee Wawa
Coffee I like coffee Wawa Coffee
After I like coffee, the coffee thread goes to
sleep and the tea thread gets to finish and die.
1 second pausing after each I like coffee
89Yield() and Sleep()
- Yield() may have no effect on some
implementations. - The thread scheduler might make no effort toward
fairness. - The yielding thread may be picked again even
though other threads want a turn. - It is a good idea to call sleep() instead.
90An Example Without Threads
Black ball bounces for awhile and then stops. If
you then click start, a new ball bounces for
awhile and then stops. Close only works between
balls. If the ball is moving and you click close,
the close message is queued.
91// From Cay Horstmann Core Java 2 Advanced import
java.awt. import java.awt.event. import
javax.swing. public class Bounce public
static void main(String args) JFrame
frame new BounceFrame() frame.show()
92class BounceFrame extends JFrame public
BounceFrame() setSize(300, 200)
setTitle("Bounce")
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0)
)
93 Container contentPane getContentPane()
canvas new JPanel()
contentPane.add(canvas, "Center") JPanel p
new JPanel() addButton(p,
"Start", new ActionListener()
public void actionPerformed(ActionEvent evt)
Ball b new Ball(canvas)
b.bounce() )
94 addButton(p, "Close", new
ActionListener() public void
actionPerformed(ActionEvent evt)
System.exit(0) )
contentPane.add(p, "South") public
void addButton(Container c, String title,
ActionListener a) JButton b new
JButton(title) c.add(b)
b.addActionListener(a) private JPanel
canvas
95 class Ball public Ball(JPanel b) box b
public void draw() Graphics g
box.getGraphics() g.fillOval(x, y, XSIZE,
YSIZE) g.dispose()
96public void move() Graphics g
box.getGraphics() g.setXORMode(box.getBackg
round()) g.fillOval(x, y, XSIZE, YSIZE)
x dx y dy Dimension d
box.getSize() if (x lt 0) x 0 dx
-dx if (x XSIZE gt d.width)
x d.width - XSIZE dx -dx if (y lt
0) y 0 dy -dy if (y YSIZE
gt d.height) y d.height - YSIZE dy
-dy g.fillOval(x, y, XSIZE, YSIZE)
g.dispose()
97 public void bounce() draw() for
(int i 1 i lt 1000 i) move()
try Thread.sleep(5)
catch(InterruptedException e)
private JPanel box private static final int
XSIZE 10 private static final int YSIZE
10 private int x 0 private int y 0
private int dx 2 private int dy 2
98Bouncing With Threads
The close button works in an instant. Each time
the start button is clicked a new ball appears.
The screen above shows four fast moving bouncing
balls.
99We use start() rather than bounce() on the ball
object
addButton(p, "Start", new
ActionListener() public void
actionPerformed(ActionEvent evt)
Ball b new Ball(canvas)
b.start() )
100and have the Ball class extend Thread and
implement run() rather than bounce().
class Ball extends Thread public void run()
try draw() for (int i 1
i lt 1000 i) move()
sleep(5)
catch(InterruptedException e)
101Ping Pong
- Adapted from "The Java Programming Language",
Arnold - and Gosling
- After a thread is created, you can configure it
set its name, - its initial priority, and so on.
- The start() method spawns a new thread of control
based on - the data in the thread object and then returns.
Now, the - Java virtual machine invokes the new thread's run
method, - making the thread active.
- When a thread's run method returns, the thread
has exited. - The thread may be manipulated with a number of
methods, - including the interrupt() method as shown in
this example.
102public class PingPong extends Thread private
String word private int delay public
PingPong(String whatToSay, int delayTime)
word whatToSay delay delayTime
103public void run() try for()
System.out.println(word" ")
sleep(delay)
catch (InterruptedException e)
System.out.println("Interrupted!!!!!") return
104 public static void main(String args)
PingPong t1 new PingPong("\tping",3
3) t1.start()
PingPong t2 new PingPong("Pong",100)
t2.start()
try Thread.sleep(5000)
catch(InterruptedException e) // will not be
printed System.out.println("Good morning")
return
105Thread myThread Thread.currentThread()
for (int t 1 t lt 10 t)
System.out.println("In Main..."
myThread.getName())
t1.interrupt()
106C\McCarthy\threads\PingPonggtjava PingPong
ping Pong ping ping
ping Pong ping ping
ping Pong ping ping ping
Main is asleep.
For 5 seconds ping and pong take turns
sleeping and running
107Pong ping ping
ping Pong In Main...main In Main...main In
Main...main In Main...main In Main...main In
Main...main In Main...main In Main...main In
Main...main In Main...main Interrupted!!!!! Pong P
ong Pong Pong Pong
Main wakes up
Main interrupts Ping and ping dies.
Pongs forever or until until ctrl-c
108A Thread Application --A Simple Web Server
- Responds by sending the same file on each hit
- Creates a new thread on each hit
109// A simple web server // Responds with the same
file on each hit import java.net. import
java.io. import java.util. public class
OneFile extends Thread static String
theData "" static String contentType
static int contentLength Socket
theConnection
110 // construct each OneFile object with an
existing socket public OneFile(Socket s)
theConnection s // run
the following code on each object public void
run() try // get a
PrintStream attached to this socket
PrintStream os new PrintStream(
theConnection.getOutputStream())
// get a DataInputStream attached to this socket
DataInputStream is new
DataInputStream(
theConnection.getInputStream())
// read a line from the socket
String request is.readLine()
111 // HTTP/1.0 and later send a MIME
header if(request.indexOf("HTTP/")
! -1) // we need to
read the rest of the MIME header
while(true)
String thisLine is.readLine()
if(thisLine.trim().equals("")) break
// respond to the client
os.print("HTTP/1.0 200 OK\r\n")
// send the date
Date now new Date()
os.print("Date " now "\r\n")
// send our name
os.print("Server OneFile 1.0\r\n")
112 // send the
contentLength
os.print("Content-length " contentLength
"\r\n") //
send the content type
os.print("Content-type " contentType
"\r\n\r\n") //
send the file in the string
os.println(theData)
theConnection.close()
catch(IOException e)
113 // main loads the file and creates the object
on every hit public static void main(String
args ) int thePort ServerSocket
ss Socket theConnection
FileInputStream theFile // cache the
file try // open file and create
a DataInputStream theFile new
FileInputStream(args0)
DataInputStream dis new DataInputStream(theFile)
114 // determine the content type of this
file if(args0.endsWith(".html")
args0.endsWith(".htm") )
contentType "text/html"
else contentType "text/plain"
// read the file into the
string theData try
String thisLine
while((thisLine dis.readLine()) ! null)
theData thisLine "\n"
catch(Exception e)
System.err.println("Error " e)
115catch(Exception e) System.err.println(
e) System.err.println("usage java
onefile filename port")
System.exit(1) // set the port to
listen on try thePort
Integer.parseInt(args1) if(thePort lt
0 thePort gt 65535) thePort 80
catch(Exception e) thePort 80
116// create a server socket try ss
new ServerSocket(thePort)
System.out.println("Accepting connections on port
"
ss.getLocalPort()) System.out.println("
Data to be sent") System.out.println(t
heData) while(true) //
stop and wait for a connection
Socket socketTemp ss.accept() //
we have a socket so create a handler
OneFile fs new OneFile(socketTemp)
// start the handler running
fs.start()
catch(IOException e) System.out.println("Socket
error")