Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.

Similar presentations


Presentation on theme: "Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results."— Presentation transcript:

1 Concurrency Java Threads

2 Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results before proceeding to the next step. Synchronization is necessary to avoid “race” conditions whenever a resource is shared. –variable –file or database –hardware

3 Basic steps in java Create a thread class In the thread class define the “run” method to define the parallel activity Create an object of the thread class you create. Call “start” on the thread object. Java will in turn call run when it starts up.

4 A simple example from sun.java.com public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((long)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); } public class TwoThreadsDemo { public static void main (String[] args) { new SimpleThread("Jamaica").start(); new SimpleThread("Fiji").start(); } Create a thread class define the “run” method Create an object of the thread class Call “start”

5 Sample Output 0 Jamaica 0 Fiji 1 Fiji 1 Jamaica 2 Jamaica 2 Fiji 3 Fiji 3 Jamaica 4 Jamaica 4 Fiji 5 Jamaica 5 Fiji 6 Fiji 6 Jamaica 7 Jamaica 7 Fiji 8 Fiji 9 Fiji 8 Jamaica DONE! Fiji 9 Jamaica DONE! Jamaica

6 Thread Life Cycle sun.java.com

7 Using timers indirectly sleep() function public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){ // the VM doesn't want us to // sleep anymore, // so get back to work } From an applet which uses stop() to set clockThread=null

8 Using timers directly import java.util.Timer; import java.util.TimerTask; public class Reminder { Timer timer; public Reminder(int seconds) { timer = new Timer(); timer.schedule(new RemindTask(), seconds*1000); } class RemindTask extends TimerTask { public void run() { System.out.println("Time's up!"); timer.cancel(); //Terminate the timer thread } public static void main(String args[]) { System.out.println("About to schedule task."); new Reminder(5); System.out.println("Task scheduled."); } Task scheduled. (5 seconds later) Time's up!

9 Producer Consumer revisited How does Java do it? (sun.java.com)

10 Producer Consumer issues Make sure there is something to consume Make sure there is room to produce Make sure that two entities don’t access the resource at the same time (race condition) How could you implement this in java?

11 Using threads (simple version) Make a thread which produces Make a thread which consumes results in -> parallel activity The resource is generically called a cubbyhole (place to put stuff) Each thread constructor gets –a cubbyhole to use and –an id

12 Producer public class Producer extends Thread { private CubbyHole cubbyhole; private int number; public Producer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { for (int i = 0; i < 10; i++) { cubbyhole.put(i); System.out.println("Producer #" + this.number + " put: " + i); try { sleep((int)(Math.random() * 100)); } catch (InterruptedException e) { } }

13 Consumer public class Consumer extends Thread { private CubbyHole cubbyhole; private int number; public Consumer(CubbyHole c, int number) { cubbyhole = c; this.number = number; } public void run() { int value = 0; for (int i = 0; i < 10; i++) { value = cubbyhole.get(); System.out.println("Consumer #" + this.number + " got: " + value); }

14 So what keeps them under control? Look at the cubbyhole

15 Cubbyhole public class CubbyHole { private int contents; private boolean available = false; public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; } public synchronized void put(int value) { while (available == true) { try {wait(); } catch (InterruptedException e) { } } contents = value; available = true; notifyAll(); }

16 Main routine public class ProducerConsumerTest { public static void main(String[] args) { CubbyHole c = new CubbyHole(); Producer p1 = new Producer(c, 1); Consumer c1 = new Consumer(c, 1); p1.start(); c1.start(); } Output Producer #1 put: 0 Consumer #1 got: 0 Producer #1 put: 1 Consumer #1 got: 1 Producer #1 put: 2 Consumer #1 got: 2 Producer #1 put: 3 Consumer #1 got: 3 Producer #1 put: 4 Consumer #1 got: 4 Producer #1 put: 5 Consumer #1 got: 5 Producer #1 put: 6 Consumer #1 got: 6 Producer #1 put: 7 Consumer #1 got: 7 Producer #1 put: 8 Consumer #1 got: 8 Producer #1 put: 9 Consumer #1 got: 9 Can you see how this locksteps? That’s not necessarily good!


Download ppt "Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results."

Similar presentations


Ads by Google