Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multithreaded Programming in Java

Similar presentations


Presentation on theme: "Multithreaded Programming in Java"— Presentation transcript:

1 Multithreaded Programming in Java

2 What is concurrent software?
A program with more than one thread of execution—more than one thing happens at once!

3 Two Units of Execution Process Thread
Has self-contained execution environment Processes do not share memory (variables) Interact via some form of message passing Thread AKA lightweight process Every process has at least one (the main thread) Faster to spawn than processes Threads share parent processes resources (e.g., memory) Threads interact by reading/writing variables Previously, you learned to do program processes with MPI Today, we’ll learn thread programming in Java

4 Consider this sequential program…
public class MyMain { public static int x = 0; public static int y = 0; public static void main(String[] args) { x = 10; y = x + 10; x = y; y = 0; System.out.println("x=" + x + " y=" + y); } What result would this program give? Answer: x=20 y=0

5 Consider this sequential program…
public class MyMain { public static int x = 0; public static int y = 0; public static void main(String[] args) { x = 10; y = x + 10; x = y; y = 0; System.out.println("x=" + x + " y=" + y); } What if you wanted these two sections to execute concurrently?

6 Concurrent program example
public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); What result would this program give?

7 Thread States Ready Running Blocked/Waiting Terminated

8 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); Initially, the main thread runs

9 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

10 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); Executing start() spawns a new thread Let’s call it T1; initially, T1 is in the Ready state

11 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); Now, a context switch occurs

12 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); And T1 begins executing

13 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

14 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); Another context switch occurs

15 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); And the main thread executes again

16 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

17 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

18 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

19 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); Executing join() causes the main thread to enter the Blocking state until all the threads it spawned terminate

20 Concurrent program example
y = 0 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

21 Concurrent program example
y = 10 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

22 Concurrent program example
y = 10 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); T1 finishes executing and enters the Terminated state

23 Concurrent program example
y = 10 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); … which causes the main thread to unblock

24 Concurrent program example
y = 10 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); … and eventually reenter the Running state

25 Concurrent program example
y = 10 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y);

26 Concurrent program example
y = 10 public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { x = 10; y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); x = y; y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); Finally, the main thread terminates

27 Race condition: Some schedules produce errors
Nondeterministic scheduling increases complexity considerably and causes nasty problems public class MyMain implements Runnable { private static volatile int x = 0; private static volatile int y = 0; public void run() { A: x = 10; B: y = x + 10; } public static void main(String[] args) throws … { Thread myT = new Thread(new MyMain()); myT.start(); C: x = y; D: y = 0; myT.join(); System.out.println("x=" + x + " y=" + y); schedule x y ABCD 20 ACBD ACDB 10 CABD CADB CDAB Race condition: Some schedules produce errors

28 What possible results might this program return?
What possible results might this program return? public class MainThread { public static volatile int x = 0; public static void main(String[] args) throws … { Thread myT = new Thread(new MyThread()); if (x > 0) { x = 0; } else { x = 6; myT.join(); System.out.println("x = " + x); public class MyThread implements Runnable { public void run() { MainThread.x += 5; MainThread.x -= 5; }

29 Let’s build a producer/consumer program!


Download ppt "Multithreaded Programming in Java"

Similar presentations


Ads by Google