15
JAVA Thread Prepared by Miss. Arati A. Gadgil

Java thread

Embed Size (px)

Citation preview

Page 1: Java thread

JAVA Thread

Prepared by

Miss. Arati A. Gadgil

Page 2: Java thread

Thread

There are two ways to create a thread

1.By extending Thread class

2. By implementing Runnable interface

2

Page 3: Java thread

Thread States

1.New2.Runnable3.Running4.Non-Runnable (Blocked)5.Terminated

3

Page 4: Java thread

4

New

Runnable

Running

Terminated

Non-Runnable Blocked

start()

run()

Sleep,IO complete

Sleep,bock,IO wait

Page 5: Java thread

Constructors of Thread classThread()Thread(String name)Thread(Runnable r)Thread(Runnable r,String name

Methods of Thread classpublic void run()

is used to perform action for a thread.public void start()

starts the execution of the thread.JVM calls the run() method on the thread.public void sleep(long miliseconds)

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

5

Page 6: Java thread

public int getPriority() returns the priority of the thread.

public int setPriority(int priority)changes the priority of the thread.

public String getName()returns the name of the thread.

public void setName(String name) changes the name of the thread.

public Thread currentThread()returns the reference of currently executing thread.

public boolean isAlive()tests if the thread is alive.

public void stop()is used to stop the thread.

6

Page 7: Java thread

Runnable Interface

Runnable interface have only one method named run().

public void run()is used to perform action for a thread.

7

Page 8: Java thread

class Multi extends Thread{  

public void run(){  

System.out.println("thread is running...");  }  

public static void main(String args[]){  

Multi t1=new Multi();  t1.start();  

  }  }

8

Page 9: Java thread

class Multi implements Runnable{  

public void run(){  

System.out.println("thread is running...");  }  

  public static void main(String args[]){  

Multi3 m1=new Multi3();  Thread t1 =new Thread(m1);  t1.start();  

  }  }

9

Page 10: Java thread

sleep() method

Thread.sleep causes the current thread to suspend execution for a specified period.

public static void sleep(long miliseconds)throws InterruptedException

public static void sleep(long miliseconds, int nanos)throws InterruptedException

10

Page 11: Java thread

join() method

The join method allows one thread to wait for the completion of another.

Overloads of join allow the programmer to specify a waiting period. However, as with sleep, join is dependent on the OS for timing, so we should not assume that join will wait exactly as long as we specify.

public void join()throws InterruptedException

public void join(long milliseconds)throws InterruptedException11

Page 12: Java thread

Thread Priority

Each thread have a priority. Priorities are represented by a number between 1 and 10

3 constants defined in Thread class:

public static int MIN_PRIORITYpublic static int NORM_PRIORITYpublic static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.

12

Page 13: Java thread

Interrupting a Thread

An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.

Methods

public void interrupt()public static boolean interrupted()public boolean isInterrupted()

13

Page 14: Java thread

Synchronization in Java

Java synchronized method

If you declare any method as synchronized, it is known as synchronized method. Synchronized method is used to lock an object for any shared resource.When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.

14

Page 15: Java thread

Thank You

15