Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.

Similar presentations


Presentation on theme: "1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of."— Presentation transcript:

1 1 Threads in Java Jingdi Wang

2 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of control within a single program Threads makes a program do more than one thing at a time

3 3 History of threads Early DOS and Windows systems were single tasking The concept of multithreading first appeared with time sharing systems High-performance graphical and networking applications promoted the growth of multithread operating systems

4 4 Threads in Java Java has built-in support for multithreading Java provides a unified multithreading API supported by all Java virtual machines on all platforms other languages (C, C++) use external thread package to do multithreading

5 5 Two ways to create threads Extend java.lang.Thread Implement the runnable interface In both cases must implement public void run(); which is the main logic of a thread

6 6 Extend java.lang.Thread public class Mythread extends Thread { public void run() { doWork(); } new Mythread().start();//the thread won’t run until you call start(), which is a method inherited from the parent class Thread

7 7 Implement the runnable interface public class MyRunner implements runnable{ public void run() { doWork(); } Thread myThread=new Thread(new MyRunner()); myThread.start(); //the thread won’t run until you call start(), which is a method inherited from the parent class Thread

8 8 Thread states - new The thread object is first created It is not yet executing When the thread's start() method is invoked, it changes to the runnable state

9 9 Thread states - runnable The thread is eligible for execution However, it is not necessarily running It can be allocated CPU time by the system when the CPU is available The CPU may not always be available

10 10 Thread states - blocked The thread is waiting for an I/O operation to complete Someone calls its sleep() method The thread calls its wait() method Someone calls its suspend() method The thread tries to obtained an object lock currently owned by another thread

11 11 Thread states - dead The thread’s run() method exits normally An uncaught exception terminates the thread’s run() method

12 12 Thread Priorities Priority values range from 1 (minimum) to 10 (maximum) Every thread has a priority When a thread is created, it inherits the priority of the thread that created it Priority can be set and changed

13 13 Thread scheduling Determines how runnable threads are allocated CPU time A thread-scheduling mechanism is either preemptive or non-preemptive A preemptive scheduler can be either time- sliced or non-time-sliced

14 14 Preemptive vs. non-preemptive A preemptive scheduler pauses a running thread to allow different threads to execute A non-preemptive scheduler never interrupts a running thread Instead, it relies on the running thread to yield control of CPU to other threads Threads may suffer from CPU starvation

15 15 Time slicing The scheduler allocates a period of time for which each thread can use the CPU When that amount of time has elapsed, the scheduler preempts the thread and switches to a different thread

16 16 Thread scheduling in Java Java threads are preemptive, but not guaranteed to be time sliced The current thread is preempted if a higher priority thread becomes runnable If an equal priority thread becomes runnable, there is no guarantee that it will be allocated CPU time until it becomes the highest priority runnable thread

17 17 Four kinds of threads programming Unrelated threads –simplest situation –threads do different things and don’t interact with each other Related but unsynchronized threads –threads work on different pieces of the same data structure –the threads don’t interact with each other

18 18 Four kinds of threads programming Mutually exclusive threads –threads share the same data –must avoid data inconsistency Communicating mutually exclusive threads –must notify other threads when data is produced or removed –must wait when the data buffer is empty or full

19 19 Pitfalls in multithreading Race condition –two or more threads update the same value simultaneously without synchronization –remedy: mutual exclusion Deadlock –remedy: careful programming

20 20 When to use threads  Lengthy processing: CPU-intensive calculation  Graphical user interface (GUI) display  Background processing: non-time critical tasks that need to execute continuously, eg. Garbage collection  Time-consuming I/O: avoid delay in unrelated parts of application


Download ppt "1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of."

Similar presentations


Ads by Google