Title: Threads
1Threads
2Thread independent flow of control
e.g. a server needs to communicate with
many customers gt each customer is served by
a separate thread
3Threads toy example
increments x
increments y
calls repaint
MyApplet
4Threads toy example
import java.applet.Applet import
java.awt. public class FirstApplet extends
Applet public int x,y public void init()
.... public void paint(Graphics
g) g.setColor(Color.white)
g.setFont(new Font("SansSerif",Font.BOLD,32))
g.drawString("x"x,10,40)
g.drawString("y"y,10,80)
g.drawString("x-y"(x-y),10,120)
5public void init() setBackground(Color.blac
k) new Thread(new Runnable () public
void run() while (true) x
).start() new Thread(new
Runnable () public void run() while
(true) y ).start()
new Thread(new Runnable () public void run()
while (true) try
Thread.currentThread().sleep(40)
catch (InterruptedException e)
repaint() ).start()
6Runnable interface
run() - method
Thread(Runnable r)
a constructor of the Thread class
7Anonymous classes
new Thread(new Runnable () public void
run() while (true) y
).start()
instance initializer
new ClassName() ? ?
local variables
8Time slicing
time
Thread 1
Thread 2
Thread 3
Thread 1
Thread 2
Thread 3
Thread 2
Thread 3
9Threads - priorities
new Thread(new Runnable () public void
run() try
Thread.currentThread().setPriority(Thread.MIN_PRIO
RITY) catch (IllegalArgumentExce
ption e) catch (SecurityException e)
while (true) x
).start()
MyApplet
10Synchronization problems
Thread 2
Thread 1
object
Extremely difficult to debug...
11Synchronization problems toy example
Thread 2
Thread 1
x,dx
MyApplet
while (true) x1000 dx-1 for
(i0ilt1000i) xdx
while (true) x-1000 dx1 for
(i0ilt1000i) xdx
12Synchronization problems toy example
public void init() new Thread(new Runnable
() public void run() while (true)
x1000 dx-1 for
(i0ilt1000i) xdx
).start() new Thread(new Runnable ()
public void run() while (true)
x-1000 dx1 for
(i0ilt1000i) xdx
).start() .....
13synchronized blocks of code
Thread 2
Thread 1
object
Do not touch the object.
14synchronized blocks of code
public void init() new Thread(new Runnable
() public void run() while (true)
synchronized(lock) x1000
dx-1 for (i0ilt1000i)
xdx ).start()
new Thread(new Runnable () public void run()
while (true) synchronized(lock)
x-1000 dx1
for (i0ilt1000i) xdx
).start()
MyApplet
15synchronized blocks of code
public void init() new Thread(new Runnable
() public void run()
synchronized(lock) while (true)
x1000 t1 dx-1 for
(i0ilt1000i) xdx
).start() new Thread(new Runnable ()
public void run() synchronized(lock)
while(true) x-1000 t2
dx1 for (i0ilt1000i)
xdx ).start()
MyApplet
16synchronized methods
synchronized ? method(?) ?
? method(?) synchronized(this) ?
17stop()
deprecated can leave objects it is
manipulating in inconsistent state
pleaseStop()
define your own
18wait() and notify()
class Pipe LinkedList lnew LinkedList()
public synchronized void Push(Object o)
l.add(o) this.notify() public
synchronized Object Pop() while
(l.size()0) try this.wait()
catch (InterruptedException e)
return l.remove(0)
19Sleeping threads
try Thread.currentThread().sleep(40)
catch (InterruptedException e)
20Exceptions
21Exceptions
method() some unexpected situation can occur
throw an exception
try method() catch (TypeOfTheException e)
// deal with the exception
22Exceptions
Code dealing with normal execution
try catch (TypeOfTheException e)
Code dealing with exceptions
23Exceptions are objects
class NoElectricityException extends Exception
NoElectricityException() super(No
electricity!) NoElectricityException(Strin
g message) super(message)
getMessage() method
24Exceptions are objects
Class Chef ... Food makeRoastBeef(Kitchen
k) if (!k.lightOn())
k.turnLightOn() if (!k.lightOn())
throw new NoElectricityException() ...
...
25Exceptions are objects
Food prepareDinner() try
fmakeRoastBeef(k) catch
(NoElectricityException e) ?
26Exceptions
Throwable
Error
Exception
RuntimeException
checked
unchecked
27Exceptions examples
InterruptedException ......... RuntimeException
ArithmeticException IndexOutOfBoundExceptio
n ArrayIndexOutOfBoundException
NullPointerException ........... Error
OutOfMemoryError ............
28Throws
Food makeRoastBeef(Kitchen k) throws
NoElectricityException,
NoMeatException,... ...
checked exceptions
29try/catch/finally
try catch (TypeOfTheException e) finally
30EXERCISE
what will be the ouput for a) x1 b) x0
try System.out.println(1) if (x0)
throw(new MyException()) catch (MyException
e) System.out.println(3) finally
System.out.println(2)
31what will be the ouput for a) MyTest(0)
b) MyTest(1) c) MyTest(2) d) MyTest(3)
void Test(int x) throws YourException try
System.out.println(1) if (x0)
throw(new MyException()) if (x1) throw(new
YourException()) if (x2) return
System.out.println(4) catch (MyException
e) System.out.println(3) finally
System.out.println(2) void MyTest(int
y) try System.out.prinln(6)
Test(y) System.out.println(7) catch
(YourException e) System.out.println(5)
EXERCISE
32Applets cont.
33Applet parameters
ltAPPLET CODEMyApplet.class" WIDTH"200"
HEIGHT"200"gt ltPARAM NAMEBALLS VALUE50gt
ltPARAM NAMEMINDIAM VALUE20gt ltPARAM
NAEMMAXDIAM VALUE50gt lt/APPLETgt
.... ballsInteger.parseInt(getParameter(BALLS"))
....
BouncingBall
BouncingBall
34Why is it flickering?
repaint requests
repaint thread
update()
clears screen
calls paint()
35Double buffering
repaint requests
repaint thread
calls paint()
update()
clears second-screen draws everything on the
second-screen copies second-screen to the screen
36BouncingBall revisited
Image offscreenImage Graphics
offscreenGraphics offscreenImage
createImage(width,height) offscreenGraphics
offscreenImage.getGraphics()
offscreenGraphics.setColor(Color.black)
offscreenGraphics.fillRect(0,0,width,height)
g.drawImage(offscreenImage, 0, 0, this)
BouncingBall
BouncingBall