threads
DESCRIPTION
Threads. Thread = independent flow of control. e.g. a server needs to communicate with many customers => each customer is served by a separate thread. Threads – toy example. increments x. increments y. calls repaint. MyApplet. Threads – toy example. import java.applet.Applet; - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/1.jpg)
Threads
![Page 2: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/2.jpg)
Thread = independent flow of control
e.g. a server needs to communicate with manycustomers => each customer is served by aseparate thread
![Page 3: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/3.jpg)
Threads – toy example
increments x
increments y
calls repaint MyApplet
![Page 4: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/4.jpg)
Threads – toy exampleimport 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); }}
![Page 5: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/5.jpg)
public void init() { setBackground(Color.black);
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(); }
1. create2. start
![Page 6: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/6.jpg)
Runnable interface
run() - method
Thread(Runnable r); a constructorof the Threadclass
![Page 7: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/7.jpg)
Anonymous classesnew Thread(new Runnable () { public void run() { while (true) { y++; } }}).start(); new ClassName() {
{ ? } ?}
instanceinitializer
local variables
![Page 8: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/8.jpg)
Time slicing
Thread 1Thread 2Thread 3Thread 1Thread 2Thread 3Thread 2Thread 3
time
![Page 9: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/9.jpg)
Threads - priorities new Thread(new Runnable () { public void run() { try { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); } catch (IllegalArgumentException e) {} catch (SecurityException e) {}
while (true) { x++; } }}).start();
MyApplet
![Page 10: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/10.jpg)
Synchronization problems
object
Thread 1 Thread 2
Extremely difficult to debug...
![Page 11: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/11.jpg)
Synchronization problems –toy example
x,dx
Thread 1 Thread 2
while (true) { x+=1000; dx=-1; for (i=0;i<1000;i++) x+=dx;}
while (true) { x-=1000; dx=+1; for (i=0;i<1000;i++) x+=dx;}
MyApplet
![Page 12: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/12.jpg)
public void init() { new Thread(new Runnable () { public void run() { while (true) { x+=1000; dx=-1; for (i=0;i<1000;i++) x+=dx; } }}).start();
new Thread(new Runnable () { public void run() { while (true) { x-=1000; dx=+1; for (i=0;i<1000;i++) x+=dx; } }}).start();
..... }
Synchronization problems –toy example
![Page 13: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/13.jpg)
synchronized blocks of code
object
Thread 1 Thread 2
Do not touch the object.
![Page 14: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/14.jpg)
synchronized blocks of codepublic void init() { new Thread(new Runnable () { public void run() { while (true) { synchronized(lock) { x+=1000; dx=-1; for (i=0;i<1000;i++) x+=dx; } } }}).start();
new Thread(new Runnable () { public void run() { while (true) { synchronized(lock) { x-=1000; dx=+1; for (i=0;i<1000;i++) x+=dx; } } }}).start();
} MyApplet
![Page 15: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/15.jpg)
synchronized blocks of codepublic void init() { new Thread(new Runnable () { public void run() { synchronized(lock) { while (true) { x+=1000; t1++; dx=-1; for (i=0;i<1000;i++) x+=dx; } } }}).start();
new Thread(new Runnable () { public void run() { synchronized(lock) { while(true) { x-=1000; t2++; dx=+1; for (i=0;i<1000;i++) x+=dx; } } }}).start();
} MyApplet
![Page 16: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/16.jpg)
synchronized methods
synchronized ? method(?) { ?}
? method(?) { synchronized(this) { ? }}
![Page 17: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/17.jpg)
stop()
deprecated – can leave objects it is manipulating in inconsistent state
pleaseStop()define your own
![Page 18: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/18.jpg)
wait() and notify()class Pipe { LinkedList l=new 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); }}
![Page 19: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/19.jpg)
Sleeping threadstry { Thread.currentThread().sleep(40);} catch (InterruptedException e) {};
![Page 20: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/20.jpg)
Exceptions
![Page 21: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/21.jpg)
Exceptions
method() some unexpected situation can occur
throw an exception
try { method();} catch (TypeOfTheException e) { // deal with the exception}
![Page 22: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/22.jpg)
Exceptions
try {
} catch (TypeOfTheException e) {
}
Code dealing with “normal” execution
Code dealing with “exceptions”
![Page 23: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/23.jpg)
Exceptions are objectsclass NoElectricityException extends Exception { NoElectricityException() { super(“No electricity!”); } NoElectricityException(String message) { super(message); }}
getMessage() method
![Page 24: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/24.jpg)
Exceptions are objectsClass Chef { ... Food makeRoastBeef(Kitchen k) { if (!k.lightOn()) { k.turnLightOn(); if (!k.lightOn()) throw new NoElectricityException(); ... } ... }}
![Page 25: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/25.jpg)
Exceptions are objects
Food[] prepareDinner() { try { f=makeRoastBeef(k); } catch (NoElectricityException e) { ? }}
![Page 26: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/26.jpg)
Exceptions
Throwable
Exception Error
RuntimeException
checked unchecked
![Page 27: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/27.jpg)
Exceptions examplesInterruptedException.........RuntimeException ArithmeticException IndexOutOfBoundException ArrayIndexOutOfBoundException NullPointerException ...........
Error OutOfMemoryError ............
![Page 28: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/28.jpg)
ThrowsFood makeRoastBeef(Kitchen k) throws NoElectricityException, NoMeatException,... { ...
}
checked exceptions
![Page 29: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/29.jpg)
try/catch/finallytry {
}catch (TypeOfTheException e){
}finally {
}
![Page 30: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/30.jpg)
try { System.out.println(“1”); if (x==0) throw(new MyException());}catch (MyException e){ System.out.println(“3”);}finally { System.out.println(“2”);}
EXERCISE: what will bethe ouput for a) x=1 b) x=0
![Page 31: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/31.jpg)
void Test(int x) throws YourException { try { System.out.println(“1”); if (x==0) throw(new MyException()); if (x==1) throw(new YourException()); if (x==2) 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:
what will bethe ouput for a) MyTest(0); b) MyTest(1); c) MyTest(2); d) MyTest(3);
![Page 32: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/32.jpg)
Applets cont.
![Page 33: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/33.jpg)
Applet parameters<APPLET CODE=“MyApplet.class" WIDTH="200" HEIGHT="200"> <PARAM NAME=“BALLS” VALUE=50> <PARAM NAME=“MINDIAM” VALUE=20><PARAM NAEM=“MAXDIAM” VALUE=50></APPLET>
....balls=Integer.parseInt(getParameter(“BALLS"));....
BouncingBallBouncingBall
![Page 34: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/34.jpg)
Why is it flickering?
“repaint” threadrepaintrequests
update() clears screen
calls paint()
![Page 35: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/35.jpg)
Double buffering
“repaint” threadrepaintrequests
update() calls paint()
clears second-screendraws everything on the second-screencopies second-screen to the screen
![Page 36: Threads](https://reader036.vdocument.in/reader036/viewer/2022081520/56814948550346895db694fa/html5/thumbnails/36.jpg)
BouncingBall 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);
BouncingBallBouncingBall