Presentation is loading. Please wait.

Presentation is loading. Please wait.

Threads. Thread = independent flow of control e.g. a server needs to communicate with many customers => each customer is served by a separate thread.

Similar presentations


Presentation on theme: "Threads. Thread = independent flow of control e.g. a server needs to communicate with many customers => each customer is served by a separate thread."— Presentation transcript:

1 Threads

2 Thread = independent flow of control e.g. a server needs to communicate with many customers => each customer is served by a separate thread

3 Threads – toy example increments x increments y calls repaint MyApplet

4 Threads – 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); }

5 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.create 2.start

6 Runnable interface run() - method Thread(Runnable r); a constructor of the Thread class

7 Anonymous classes new Thread(new Runnable () { public void run() { while (true) { y++; } }).start(); new ClassName() { { ? } ? } instance initializer local variables

8 Time slicing Thread 1 Thread 2 Thread 3 Thread 1 Thread 2 Thread 3 Thread 2 Thread 3 time

9 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

10 Synchronization problems object Thread 1 Thread 2 Extremely difficult to debug...

11 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

12 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

13 synchronized blocks of code object Thread 1 Thread 2 Do not touch the object.

14 synchronized blocks of code public 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

15 synchronized blocks of code public 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

16 synchronized methods synchronized ? method(?) { ? } ? method(?) { synchronized(this) { ? }

17 stop() deprecated – can leave objects it is manipulating in inconsistent state pleaseStop() define your own

18 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); }

19 Sleeping threads try { Thread.currentThread().sleep(40); } catch (InterruptedException e) {};

20 Exceptions

21 method() some unexpected situation can occur throw an exception try { method(); } catch (TypeOfTheException e) { // deal with the exception }

22 Exceptions try { } catch (TypeOfTheException e) { } Code dealing with “normal” execution Code dealing with “exceptions”

23 Exceptions are objects class NoElectricityException extends Exception { NoElectricityException() { super(“No electricity!”); } NoElectricityException(String message) { super(message); } getMessage() method

24 Exceptions are objects Class Chef {... Food makeRoastBeef(Kitchen k) { if (!k.lightOn()) { k.turnLightOn(); if (!k.lightOn()) throw new NoElectricityException();... }... }

25 Exceptions are objects Food[] prepareDinner() { try { f=makeRoastBeef(k); } catch (NoElectricityException e) { ? }

26 Exceptions Throwable Exception Error RuntimeException checked unchecked

27 Exceptions examples InterruptedException......... RuntimeException ArithmeticException IndexOutOfBoundException ArrayIndexOutOfBoundException NullPointerException........... Error OutOfMemoryError............

28 Throws Food makeRoastBeef(Kitchen k) throws NoElectricityException, NoMeatException,... {... } checked exceptions

29 try/catch/finally try { } catch (TypeOfTheException e){ } finally { }

30 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 be the ouput for a) x=1 b) x=0

31 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 be the ouput for a) MyTest(0); b) MyTest(1); c) MyTest(2); d) MyTest(3);

32 Applets cont.

33 Applet parameters.... balls=Integer.parseInt(getParameter(“BALLS"));.... BouncingBall

34 Why is it flickering? “repaint” thread repaint requests update() clears screen calls paint()

35 Double buffering “repaint” thread repaint requests update() calls paint() clears second-screen draws everything on the second-screen copies second-screen to the screen

36 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); BouncingBall


Download ppt "Threads. Thread = independent flow of control e.g. a server needs to communicate with many customers => each customer is served by a separate thread."

Similar presentations


Ads by Google