Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong...

33
Java Threads : An Introduction Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014

Transcript of Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong...

Page 1: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

Java Threads : An IntroductionJava Threads : An Introduction

Ed Armstrong

sharcnet.ca

2014

Page 2: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

ItineraryItinerary

(1) What are threads?

(2) How to create and control a thread.

(3) Synchronization and signals.

(4) Example

Page 3: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

3

What is a Thread?What is a Thread?

● Light-weight process that can run concurrently with other threads.

● All Threads share a node, sharing memory. ● Main() is a thread.● Java garbage collection is a thread.

Page 4: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

4

Single CoreSingle Core

Page 5: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

5

Multi-Core (4 Core)Multi-Core (4 Core)

Page 6: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

6

How to create and control threads.How to create and control threads.

● Thread class and runnable interface● thread.start()● thread.join()● thread.sleep()● Keyword : 'volatile'

Page 7: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

7

Creating a threadCreating a thread

There are 2 ways to create a thread in Java

(1) Extend the 'Thread' class.

(2) Implement the 'Runnable' interface.

Page 8: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

8

Method #1Method #1Extend the 'Thread' classExtend the 'Thread' class

public class Example1 extends Thread{

public static void main(String[] args){ Example1 thread = new Example1(); thread.start(); }

public void run(){ ... }}

Page 9: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

9

Method #2Method #2Implement 'Runnable'Implement 'Runnable'

public class Example1 implements Runnable{

public static void main(String[] args) { Example1 example = new Example1(); Thread thread = new Thread(example); thread.start(); }

public void run() { .... do something .... }}

Page 10: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

10

Common MethodsCommon Methods

start() : Causes a thread to begin execution; the Java Virtual Machine calls the 'run' method of the interface.

join() : Blocks while waiting for a particular thread to terminate.

sleep(long msec) : Causes the currently executing thread to temporarily cease execution.

Page 11: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

11

Example : starting one threadExample : starting one thread

public class Example1 extends Thread{

public static void main(String[] args){ new Example1().start(); }

public void run() { System.out.println("I am an independent thread!"); System.out.println("My Thread ID is " + this.getId()); System.out.println("My Name is " + this.getName()); }}

Page 12: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

12

I am an independent thread!

My Thread ID is 9

My Name is Thread-0

>_

Page 13: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

13

Example : starting two threadsExample : starting two threads

public class Example2 extends Thread{

public Example2() { super(); } public Example2(String s) { super(s); } public static void main(String[] args){ new Example2().start(); new Example2("Albert Threadington").start(); }

@Override public void run() { System.out.println("I am an independent thread!"); System.out.println("My Thread ID is " + this.getId()); System.out.println("My Name is " + this.getName()); }}

Page 14: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

14

I am an independent thread!

I am an independent thread!

My Thread ID is 11

My Thread ID is 10

My Name is Albert Threadington

My Name is Thread-0

>_

Page 15: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

15

Race ConditionRace Condition

A race condition occurs when where some event (in this case the output) is dependent on the sequence or timing of other uncontrollable events; the order in which threads access I/O.

Page 16: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

16

thread.join()thread.join()

void join()

Waits for this thread to terminate.

void join(long millis)

Waits at most millis milliseconds for this thread to terminate.

void join(long millis, int nanos)

Waits at most millis milliseconds plus nanos nanoseconds for this thread to terminate.

These definitions from the java apihttp://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

Page 17: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

17

start - joinstart - join

Page 18: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

18

Example : thread.joinExample : thread.join

public class Example3 extends Thread{

public Example3() { super(); } public Example3(String s){ super(s); }

public static void main(String[] args) throws InterruptedException{ Thread t1 = new Thread(new Example3()); Thread t2 = new Thread(new Example3("Albert Threadington")); t1.start(); t1.join(); t2.start(); }

@Override public void run() { System.out.println("I am an independent thread!"); System.out.println("My Thread ID is " + this.getId()); System.out.println("My Name is " + this.getName()); }}

Page 19: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

19

I am an independent thread!

I am an independent thread!

My Thread ID is 9

My Thread ID is 10

My Name is Albert Threadington

My Name is Thread-0

>_

Page 20: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

20

Stopping a thread.Stopping a thread.

public class Example3 extends Thread{

private volatile boolean isRunning = true;

public static void main(String[] args) throws InterruptedException{ ... stuff ... }

public void run() { while(isRunning){ ... do something ... } }

public void flagStop(){ isRunning = false; }}

Page 21: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

21

VolatileVolatile

public class Example3 extends Thread{

private volatile boolean isRunning = true;

public static void main(String[] args) throws InterruptedException{ ... stuff ... }

@Override public void run() { while(isRunning){ ... do something ... } }

public void flagStop(){ isRunning = false; }

}

● 'isRunning' is actually accessed by 2 (or more) threads, and may be kept locally in machine registers.

● Loading and storing of some variables are atomic.● Their value may be in cache memory or registers.● Volatile ensures the variable is kept in main memory.● More elegant than sync'd getters/setters.

Page 22: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

22

Synchronization locks.Synchronization locks.

Page 23: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

23

Keyword: SynchronizedKeyword: Synchronized

● object's method● class's static method● locking an object

– instance level code block

– class level code block

Page 24: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

24

Keyword: SynchronizedKeyword: Synchronized

● instance method● static method● instance code block● static code block

public class Example {

public synchronized void doSomething(){ .... /* code goes here */ .... }

}

Page 25: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

25

Keyword: SynchronizedKeyword: Synchronized

● instance method● static method● instance code block● static code block

public class Example {

public synchronized static void doSomething(){ .... /* code goes here */ .... }

}

Page 26: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

26

Keyword: SynchronizedKeyword: Synchronized

● instance method● static method● instance code block● static code block

public class Example { public void doSomething(Object obj){ synchronized(obj){ .... } } }

public class Example { public void doSomething(){ -- not synch'd -- synchronized(this){ .... } -- not synch'd -- } }

Page 27: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

27

Keyword: SynchronizedKeyword: Synchronized

● instance method● static method● instance code block● static code block

public class Example { public void doSomething(){ -- not synch'd -- synchronized(SomeClass.class){ .... } -- not synch'd -- } }

Page 28: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

28

wait - notifywait - notify

Page 29: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

29

wait-notifywait-notify

public class ThreadA extends Thread{ public void run(){ synchronized(WaitNotify.class){ try { System.out.println("Thread A - Before"); WaitNotify.class.wait(); System.out.println("Thread A - After"); } catch (InterruptedException ex) {} } } }

public class ThreadB extends Thread{ public void run(){ synchronized(WaitNotify.class){ System.out.println("Thread B - Before"); WaitNotify.someCondition = true; WaitNotify.class.notify(); System.out.println("Thread B - After"); } } }

Page 30: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

30

wait-notify (fixed)wait-notify (fixed)

public class ThreadA extends Thread{ public void run(){ synchronized(WaitNotify.class){ try { System.out.println("Thread A - Before"); while (WaitNotify.someCondition == false) WaitNotify.class.wait(); System.out.println("Thread A - After"); } catch (InterruptedException ex) {} } } }

public class ThreadB extends Thread{ public void run(){ synchronized(WaitNotify.class){ System.out.println("Thread B - Before"); WaitNotify.someCondition = true; WaitNotify.class.notify(); System.out.println("Thread B - After"); } } }

Page 31: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

31

Master-Slave ExampleMaster-Slave Example

Page 32: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

32

Final ExampleFinal Example

/work/edward/Examples/Farmer-Worker/

Page 33: Java Threads : An Introduction - SHARCNET€¦ · Java Threads : An Introduction Ed Armstrong sharcnet.ca 2014. Itinerary ... Keyword : 'volatile' 7 Creating a thread There are 2

33

Thank You,Questions?