CO2101 Threads in Java - Tom Ridge · 15/10/2019 · Java Thread class, and Runnable interface...
Transcript of CO2101 Threads in Java - Tom Ridge · 15/10/2019 · Java Thread class, and Runnable interface...
CO2101 — Threads in Java
Tom Ridge (tr61)
15th October 2019
tr61 Java Threads 15th October 2019 1 / 14
Recap/overview
Recap and Lecture Overview
Recap
The nature of concurrent programming is multiple processes/threadsrunning in parallelProcesses may share resourcesSolutions need to guarantee mutual exclusion, progress, bounded waiting
Overview of this lecture
How can Java support concurrent programming?
Java Thread class, and Runnable interfaceJava Executors and ThreadPoolsSimple examples
tr61 Java Threads 15th October 2019 2 / 14
Recap/overview
Recap and Lecture Overview
Recap
The nature of concurrent programming is multiple processes/threadsrunning in parallelProcesses may share resourcesSolutions need to guarantee mutual exclusion, progress, bounded waiting
Overview of this lecture
How can Java support concurrent programming?
Java Thread class, and Runnable interfaceJava Executors and ThreadPoolsSimple examples
tr61 Java Threads 15th October 2019 2 / 14
Concurrency in Java
Concurrency in Java
Java Virtual Machine (JVM) allows an application to have multiplethreads running concurrently
the thread concept is supported by
the classes java.lang.Thread and java.util.concurrentthe interface java.lang.Runnable
Two ways to create a new thread
Extending the Thread classImplementing the Runnable interface
tr61 Java Threads 15th October 2019 3 / 14
Concurrency in Java
Concurrency in Java
Java Virtual Machine (JVM) allows an application to have multiplethreads running concurrently
the thread concept is supported by
the classes java.lang.Thread and java.util.concurrentthe interface java.lang.Runnable
Two ways to create a new thread
Extending the Thread classImplementing the Runnable interface
tr61 Java Threads 15th October 2019 3 / 14
Concurrency in Java
Concurrency in Java
Java Virtual Machine (JVM) allows an application to have multiplethreads running concurrently
the thread concept is supported by
the classes java.lang.Thread and java.util.concurrentthe interface java.lang.Runnable
Two ways to create a new thread
Extending the Thread classImplementing the Runnable interface
tr61 Java Threads 15th October 2019 3 / 14
Concurrency in Java
Two ways to create a thread
Extend Thread class
Implement Runnable interface
MyRunnable
+run(): void
Thread
+run(): void
ExampleThread
+run(): void
Runnable
+run(): voidThread
tr61 Java Threads 15th October 2019 4 / 14
Concurrency in Java Extend Thread class
Extending Java Thread Class
Declare a subclass of Thread, overriding its run() method
public class ExampleThread extends Thread {int parameter;ExampleThread(int para) { parameter = para; }
public void run() {... // what the thread should do
}}
Create an instance of the subclass and start executing it
ExampleThread t = new ExampleThread(10);t.start();
tr61 Java Threads 15th October 2019 5 / 14
Concurrency in Java Extend Thread class
Extending Java Thread Class
Declare a subclass of Thread, overriding its run() method
public class ExampleThread extends Thread {int parameter;ExampleThread(int para) { parameter = para; }
public void run() {... // what the thread should do
}}
Create an instance of the subclass and start executing it
ExampleThread t = new ExampleThread(10);t.start();
tr61 Java Threads 15th October 2019 5 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable
Java does not permit multiple inheritance
So usually more convenient to implement run() from Java interfaceRunnable instead of deriving from Thread
Runnable declares (just) the run() method
public interface Runnable {public abstract void run();
}
We can extend other classes while implementing Runnable
In fact, the Thread class just implements Runnable
public class Threadextends Objectimplements Runnable
tr61 Java Threads 15th October 2019 6 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable
Java does not permit multiple inheritance
So usually more convenient to implement run() from Java interfaceRunnable instead of deriving from Thread
Runnable declares (just) the run() method
public interface Runnable {public abstract void run();
}
We can extend other classes while implementing Runnable
In fact, the Thread class just implements Runnable
public class Threadextends Objectimplements Runnable
tr61 Java Threads 15th October 2019 6 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable
Java does not permit multiple inheritance
So usually more convenient to implement run() from Java interfaceRunnable instead of deriving from Thread
Runnable declares (just) the run() method
public interface Runnable {public abstract void run();
}
We can extend other classes while implementing Runnable
In fact, the Thread class just implements Runnable
public class Threadextends Objectimplements Runnable
tr61 Java Threads 15th October 2019 6 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable
Java does not permit multiple inheritance
So usually more convenient to implement run() from Java interfaceRunnable instead of deriving from Thread
Runnable declares (just) the run() method
public interface Runnable {public abstract void run();
}
We can extend other classes while implementing Runnable
In fact, the Thread class just implements Runnable
public class Threadextends Objectimplements Runnable
tr61 Java Threads 15th October 2019 6 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable
Java does not permit multiple inheritance
So usually more convenient to implement run() from Java interfaceRunnable instead of deriving from Thread
Runnable declares (just) the run() method
public interface Runnable {public abstract void run();
}
We can extend other classes while implementing Runnable
In fact, the Thread class just implements Runnable
public class Threadextends Objectimplements Runnable
tr61 Java Threads 15th October 2019 6 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable (Cont’d)
First declare a class implementing Runnable
public class MyRunnable extends SomeClassimplements Runnable {
int parameter;MyRunnable (int para) { parameter = para; }
public void run() {... // what should the thread do?
}}
Threads can then be allocated and started by
MyRunnable r = new MyRunnable(10);Thread t = new Thread(r);t.start();
tr61 Java Threads 15th October 2019 7 / 14
Concurrency in Java Implement Runnable interface
Implementing the Interface Runnable (Cont’d)
First declare a class implementing Runnable
public class MyRunnable extends SomeClassimplements Runnable {
int parameter;MyRunnable (int para) { parameter = para; }
public void run() {... // what should the thread do?
}}
Threads can then be allocated and started by
MyRunnable r = new MyRunnable(10);Thread t = new Thread(r);t.start();
tr61 Java Threads 15th October 2019 7 / 14
Concurrency in Java Thread methods
Methods of Thread Class
Some principal methods of the Thread class
t.start(): Causes thread t to begin execution; (JVM will call run()concurrently)
t.run(): Call the run method of thread t directly (with no concurrency;typically you don’t do this)Thread.sleep(ms): Cease the execution of current thread for msmillisecondst.join(): Causes the calling thread to wait (block) for the thread t tofinish
Refer to the Java documents about Thread class and Runnableinterface.
tr61 Java Threads 15th October 2019 8 / 14
Concurrency in Java Thread methods
Methods of Thread Class
Some principal methods of the Thread class
t.start(): Causes thread t to begin execution; (JVM will call run()concurrently)t.run(): Call the run method of thread t directly (with no concurrency;typically you don’t do this)
Thread.sleep(ms): Cease the execution of current thread for msmillisecondst.join(): Causes the calling thread to wait (block) for the thread t tofinish
Refer to the Java documents about Thread class and Runnableinterface.
tr61 Java Threads 15th October 2019 8 / 14
Concurrency in Java Thread methods
Methods of Thread Class
Some principal methods of the Thread class
t.start(): Causes thread t to begin execution; (JVM will call run()concurrently)t.run(): Call the run method of thread t directly (with no concurrency;typically you don’t do this)Thread.sleep(ms): Cease the execution of current thread for msmilliseconds
t.join(): Causes the calling thread to wait (block) for the thread t tofinish
Refer to the Java documents about Thread class and Runnableinterface.
tr61 Java Threads 15th October 2019 8 / 14
Concurrency in Java Thread methods
Methods of Thread Class
Some principal methods of the Thread class
t.start(): Causes thread t to begin execution; (JVM will call run()concurrently)t.run(): Call the run method of thread t directly (with no concurrency;typically you don’t do this)Thread.sleep(ms): Cease the execution of current thread for msmillisecondst.join(): Causes the calling thread to wait (block) for the thread t tofinish
Refer to the Java documents about Thread class and Runnableinterface.
tr61 Java Threads 15th October 2019 8 / 14
Concurrency in Java Thread methods
Methods of Thread Class
Some principal methods of the Thread class
t.start(): Causes thread t to begin execution; (JVM will call run()concurrently)t.run(): Call the run method of thread t directly (with no concurrency;typically you don’t do this)Thread.sleep(ms): Cease the execution of current thread for msmillisecondst.join(): Causes the calling thread to wait (block) for the thread t tofinish
Refer to the Java documents about Thread class and Runnableinterface.
tr61 Java Threads 15th October 2019 8 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
Outline code using Threads
public class T1 extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}......public class Tn extends Thread {// attributes and constructorpublic void run() { /* thread functionality */ }
}public class MainProgram {public static void main(String[] args) {
B;T1 t1 = new T1(); ...; Tn tn = new Tn();t1.start(); ...; tn.start();I;t1.join(); ...; tn.join();F; }
}
tr61 Java Threads 15th October 2019 9 / 14
Concurrency in Java Using threads
The Semantics of start() and join()
The execution of B finishes before t1,. . . ,tn as they have not beenstarted yet;
The effect of t1.start(),. . . ,tn.start() is equivalent to interleavingthe atomic actions of the started threads;
Execution of statement I can interleave with t1,. . . ,tn
Execution of statement F does not begin until all of t1,. . . ,tn havecompleted
Execution of main() terminates only if all of the started threads (andI and F) terminate
If we replace t1.start(),. . . ,tn.start() with t1.run(),. . . ,tn.run(),the program will become a sequential program
tr61 Java Threads 15th October 2019 10 / 14
Concurrency in Java Using threads
The Semantics of start() and join()
The execution of B finishes before t1,. . . ,tn as they have not beenstarted yet;
The effect of t1.start(),. . . ,tn.start() is equivalent to interleavingthe atomic actions of the started threads;
Execution of statement I can interleave with t1,. . . ,tn
Execution of statement F does not begin until all of t1,. . . ,tn havecompleted
Execution of main() terminates only if all of the started threads (andI and F) terminate
If we replace t1.start(),. . . ,tn.start() with t1.run(),. . . ,tn.run(),the program will become a sequential program
tr61 Java Threads 15th October 2019 10 / 14
Concurrency in Java Using threads
The Semantics of start() and join()
The execution of B finishes before t1,. . . ,tn as they have not beenstarted yet;
The effect of t1.start(),. . . ,tn.start() is equivalent to interleavingthe atomic actions of the started threads;
Execution of statement I can interleave with t1,. . . ,tn
Execution of statement F does not begin until all of t1,. . . ,tn havecompleted
Execution of main() terminates only if all of the started threads (andI and F) terminate
If we replace t1.start(),. . . ,tn.start() with t1.run(),. . . ,tn.run(),the program will become a sequential program
tr61 Java Threads 15th October 2019 10 / 14
Concurrency in Java Using threads
The Semantics of start() and join()
The execution of B finishes before t1,. . . ,tn as they have not beenstarted yet;
The effect of t1.start(),. . . ,tn.start() is equivalent to interleavingthe atomic actions of the started threads;
Execution of statement I can interleave with t1,. . . ,tn
Execution of statement F does not begin until all of t1,. . . ,tn havecompleted
Execution of main() terminates only if all of the started threads (andI and F) terminate
If we replace t1.start(),. . . ,tn.start() with t1.run(),. . . ,tn.run(),the program will become a sequential program
tr61 Java Threads 15th October 2019 10 / 14
Concurrency in Java Using threads
The Semantics of start() and join()
The execution of B finishes before t1,. . . ,tn as they have not beenstarted yet;
The effect of t1.start(),. . . ,tn.start() is equivalent to interleavingthe atomic actions of the started threads;
Execution of statement I can interleave with t1,. . . ,tn
Execution of statement F does not begin until all of t1,. . . ,tn havecompleted
Execution of main() terminates only if all of the started threads (andI and F) terminate
If we replace t1.start(),. . . ,tn.start() with t1.run(),. . . ,tn.run(),the program will become a sequential program
tr61 Java Threads 15th October 2019 10 / 14
Concurrency in Java Using threads
The Semantics of start() and join()
The execution of B finishes before t1,. . . ,tn as they have not beenstarted yet;
The effect of t1.start(),. . . ,tn.start() is equivalent to interleavingthe atomic actions of the started threads;
Execution of statement I can interleave with t1,. . . ,tn
Execution of statement F does not begin until all of t1,. . . ,tn havecompleted
Execution of main() terminates only if all of the started threads (andI and F) terminate
If we replace t1.start(),. . . ,tn.start() with t1.run(),. . . ,tn.run(),the program will become a sequential program
tr61 Java Threads 15th October 2019 10 / 14
Concurrency in Java Examples
Thread Example
class ExampleThread extends Thread {int _id;ExampleThread(int id) { _id = id; }public void run() {
System.out.println("This is thread: " + _id);}public static void main(String[] args) {
ExampleThread t1 = new ExampleThread(42);ExampleThread t2 = new ExampleThread(43);ExampleThread t3 = new ExampleThread(44);t1.start();t2.start();t3.start();System.out.println("All threads have terminated.");
}}
tr61 Java Threads 15th October 2019 11 / 14
Concurrency in Java Examples
Thread Example
class ExampleThread extends Thread {int _id;ExampleThread(int id) { _id = id; }public void run() {
System.out.println("This is thread: " + _id);}public static void main(String[] args) {
ExampleThread t1 = new ExampleThread(42);ExampleThread t2 = new ExampleThread(43);ExampleThread t3 = new ExampleThread(44);t1.start();t2.start();t3.start();System.out.println("All threads have terminated.");
}}
tr61 Java Threads 15th October 2019 11 / 14
Concurrency in Java Examples
Thread Example
class ExampleThread extends Thread {int _id;ExampleThread(int id) { _id = id; }public void run() {
System.out.println("This is thread: " + _id);}public static void main(String[] args) {
ExampleThread t1 = new ExampleThread(42);ExampleThread t2 = new ExampleThread(43);ExampleThread t3 = new ExampleThread(44);t1.start();t2.start();t3.start();System.out.println("All threads have terminated.");
}}
tr61 Java Threads 15th October 2019 11 / 14
Concurrency in Java Examples
Thread Example
class ExampleThread extends Thread {int _id;ExampleThread(int id) { _id = id; }public void run() {
System.out.println("This is thread: " + _id);}public static void main(String[] args) {
ExampleThread t1 = new ExampleThread(42);ExampleThread t2 = new ExampleThread(43);ExampleThread t3 = new ExampleThread(44);t1.start();t2.start();t3.start();System.out.println("All threads have terminated.");
}}
tr61 Java Threads 15th October 2019 11 / 14
Concurrency in Java Examples
Execution Result
A possible result is as follows:
This is thread 42This is thread 43All threads have terminated.This is thread 44
The statements in main() after the start() statements can interleavewith the statements of the started threads.
The result is non-deterministic, and can differ with each run of theprogram.
tr61 Java Threads 15th October 2019 12 / 14
Concurrency in Java Examples
Runnable example
class ExampleRunnable implements Runnable {int _id;
ExampleRunnable(int id){ _id = id; }public void run() {
System.out.println("This is runnable thread: " + _id);}public static void main(String[] args) {
Thread t1 = new Thread(new ExampleRunnable(42));Thread t2 = new Thread(new ExampleRunnable(43));Thread t3 = new Thread(new ExampleRunnable(44));t1.start();t2.start();t3.start();System.out.println("All threads have terminated."); }
}
tr61 Java Threads 15th October 2019 13 / 14
Concurrency in Java Examples
Runnable example
class ExampleRunnable implements Runnable {int _id;
ExampleRunnable(int id){ _id = id; }public void run() {
System.out.println("This is runnable thread: " + _id);}public static void main(String[] args) {
Thread t1 = new Thread(new ExampleRunnable(42));Thread t2 = new Thread(new ExampleRunnable(43));Thread t3 = new Thread(new ExampleRunnable(44));t1.start();t2.start();t3.start();System.out.println("All threads have terminated."); }
}
tr61 Java Threads 15th October 2019 13 / 14
Concurrency in Java Examples
Runnable example
class ExampleRunnable implements Runnable {int _id;
ExampleRunnable(int id){ _id = id; }public void run() {
System.out.println("This is runnable thread: " + _id);}public static void main(String[] args) {
Thread t1 = new Thread(new ExampleRunnable(42));Thread t2 = new Thread(new ExampleRunnable(43));Thread t3 = new Thread(new ExampleRunnable(44));t1.start();t2.start();t3.start();System.out.println("All threads have terminated."); }
}
tr61 Java Threads 15th October 2019 13 / 14
Concurrency in Java Examples
Runnable example
class ExampleRunnable implements Runnable {int _id;
ExampleRunnable(int id){ _id = id; }public void run() {
System.out.println("This is runnable thread: " + _id);}public static void main(String[] args) {
Thread t1 = new Thread(new ExampleRunnable(42));Thread t2 = new Thread(new ExampleRunnable(43));Thread t3 = new Thread(new ExampleRunnable(44));t1.start();t2.start();t3.start();System.out.println("All threads have terminated."); }
}
tr61 Java Threads 15th October 2019 13 / 14
Summary
Summary
A thread in Java can be created via:
Extending Thread classImplementing Runnable interface
The result of running programs with multiple threads may benon-deterministic
We have not yet really considered critical sections
tr61 Java Threads 15th October 2019 14 / 14