15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general,...

14
15.1 Threads and Multi-threading

Transcript of 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general,...

Page 1: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.1 Threads and Multi-threading

Page 2: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.1.1 Understanding threads and multi-threading

• In general, modern computers perform one task at a time

• It is often desirable to perform several tasks simultaneously

• Some operating systems can load several programs at once, then give them shared access to the CPU

• This is multi-threading, and gives the appearance that the programs are running simultaneously

Page 3: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.2.1 Thread class

• An instance of a program that runs simultaneously with other programs is a thread

• The java.lang.Thread class models a thread of execution

• To create a new thread of execution, extend Thread, then put your code in Thread’s run() method

• To use it, instantiate the class and call it’s start() method (NOT run())

Page 4: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.2.2 Runnable interface

• If you do not wish to extend Thread, create a class that implements the Runnable interface

• Define your run() method in this class• Create a new Thread, passing your Runnable

class as an argument, then call the Thread’s start() method

Page 5: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.3 Creating and Running Threads

• Create a class that extends Thread– Override the run() method with your code– Instantiate your new class– Call it’s start() method

• Create a class that implements Runnable– Create a run() method that holds your code– Create an instance of Thread, passing your class to

the constructor– Call the Thread’s start() method

Page 6: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.3.2 Running threads

• The only way to start a Thread is by calling the Thread’s start() method– Not its run() method– Not a start() method

of the class implementing the Runnable interface

Page 7: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.4.1 Thread states • Born

– The Thread object has been created

• Ready– The Thread’s start() method has

been called, but it has not yet been allocated CPU time

• Running– The Thread’s run() method is

being executed by the JVM• Blocked

– The Thread is waiting for an event to occur

• Dead– The Thread’s run method has completed

Page 8: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.1 Blocked state

• Blocked Threads allow other Threads to access the CPU

• Thread.sleep() places the calling Thread into a blocked state for an amount of time

• Thread.join() cause a Thread to wait until the completion of the called Thread

• Thread.yield() allows other Threads of the same priority to access the CPU

Page 9: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.2 Thread scheduling

• Obtain a reference to the current Thread using the Thread.currentThread() static method

• Use the isAlive() method to determine if a Thread is dead or not

• Use the setPriority() method to control the Thread’s priority

Page 10: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.3 Stopping threads

• Older methods that stopped Threads are unreliable

• Use a flag in the run() method to indicate when the Thread should end

Page 11: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.4 Synchronization and deadlocks

• Sometimes, different Threads can attempt to access the same data

• Two Threads may be executing the same method at the same time

• The contents of local variables may becoming unpredictable

Page 12: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.4 Synchronization and deadlocks

• Blocks of statements can be declared synchronized

• Only one Thread at a time can access a synchronized block

• When two Threads try to access the same synchronized block, one must wait

• Every Object has a flag or lock• A Thread must obtain the object’s lock before it

can access the synchronized code

Page 13: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.4 Synchronization and deadlocks

• A Thread waits until the object’s lock becomes available

• It is possible that two Threads could prevent the release of the lock the other is waiting for

• Neither Thread can progress; this is called deadlock

• Java does not have any mechanisms to detect or prevent deadlock

Page 14: 15.1 Threads and Multi- threading. 15.1.1 Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.

15.5.5 Communication between threads

• Threads can be used to query the status of an object

• Polling is an inefficient means of querying• An alternative is to force the querying Thread to wait

• When the object’s status changes, another Thread calls notify for that object

• All waiting Threads then become eligible to continue