Threads in Java - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Threads in Java

Description:

A computer program is a set of instructions that the machine follows ... It still depends on operating system, how threads within a process are managed and executed ... – PowerPoint PPT presentation

Number of Views:540
Avg rating:3.0/5.0
Slides: 19
Provided by: bas44
Category:
Tags: java | method | threads

less

Transcript and Presenter's Notes

Title: Threads in Java


1
Threads in Java
  • Programming parallel processes

2
Single Threaded Programs
  • A computer program is a set of instructions that
    the machine follows
  • A program that is in its execution is known as
    process
  • The order in which the instructions in a program
    are executed is known as flow of control.

3
Single Threaded Program
  • Usually, a process has only one flow of control
    in a program
  • At times it is necessary to have several flows of
    control in a program that work in parallel
  • For example
  • A program may be taking input from file, writing
    to network, displaying animation on screen and
    playing music on the sound card at the same time

4
Threads
  • The flow of control in a program is called the
    thread
  • A program with multiple flows of control is known
    as multi-threaded
  • Most of the program we have written in java so
    far are single threaded

5
An example
Single Thread
Input 1
Calculate
Output
Input 2
Calculate
Output
Input 3
Calculate
Output
Multiple Threads
Input 1
Calculate
Output
Input 2
Calculate
Output
Input 3
Calculate
Output
Time
6
Threads on a single processor
  • When using threads with machines having one
    processor, actually only one thread executes at
    one time.
  • Threads take turns, one by one to execute their
    code.
  • It happens so quickly that it appears to the user
    that threads are executing in parallel

7
Thread
  • Threads are useful on single processor machines
    as well as multiple processor machines
  • As seen in example, one thread may be waiting for
    I/O, one thread calculating the result and so
    on. can improve the efficiency of the program.

8
Process vs Threads
  • Every running program is known as a process
  • Operating system handles the processes running on
    a machine
  • Threads are actually sub-processes within a
    process that execute in parallel
  • It still depends on operating system, how threads
    within a process are managed and executed

9
Threads
  • A nice example of a multi-threaded program is
    MS-Word
  • While you type a document in Word, a thread keeps
    checking the spelling mistakes in your document

10
How to create threads in Java
  • java.lang.Thread is the basic thread class
    provided by JDK
  • Creating Thread is a 3 step process in java
  • Extend your class by java.lang.Thread
  • Define a function public void run() in it
  • Start the thread

11
Creating ThreadExtending Thread
  • public class ThreadTest extends Thread
  • This makes our ThreadTest class capable of
    running a separate thread.

12
Define a function public void run()
  • The code you want to run in parallel should be
    written as a body of run method of the thread
  • public class ThreadTest extends Thread
  • public void run()
  • for(int i0 ilt20 i)
  • System.out.println("Running thread step
    "i" of 20000")

13
Starting the thread
  • public static void main(String args)
  • ThreadTest t1 new ThreadTest()
  • ThreadTest t2 new ThreadTest()
  • t1.start() // first thread started
  • t2.start() // second thread started
  • // without waiting for t1

14
Tentative OutputOutput will vary from machine
to machine
  • Running thread step 0 of 20000
  • Running thread step 1 of 20000
  • Running thread step 2 of 20000
  • Running thread step 3 of 20000
  • Running thread step 4 of 20000
  • Running thread step 5 of 20000
  • Running thread step 6 of 20000
  • Running thread step 7 of 20000
  • Running thread step 8 of 20000
  • Running thread step 0 of 20000
  • Running thread step 1 of 20000
  • Running thread step 2 of 20000
  • Running thread step 3 of 20000
  • Running thread step 4 of 20000
  • Running thread step 9 of 20000
  • Running thread step 10 of 20000
  • Running thread step 11 of 20000
  • Running thread step 12 of 20000
  • Running thread step 13 of 20000

Thread 1
Thread 2
Thread 1
Thread 2
Thread 1
15
How does thread stop ?
  • Thread automatically stop when run() method
    finishes it execution.

16
Thread Priorities
  • All threads have by default the same priority in
    the JVM
  • You can set one thread to have higher or lower
    priority
  • Thread.setPriority( )and Thread.getPriority(
    )are the methods that can change or check the
    priority of a thread

17
Priorities in java.lang.Thread
  • static int MAX_PRIORITY The maximum priority
    that a thread can have.
  • static int MIN_PRIORITY  The minimum priority
    that a thread can have.
  • static int NORM_PRIORITY The default priority
    that is assigned to a thread.

18
Trym out
Write a Comment
User Comments (0)
About PowerShow.com