Date post: | 13-Apr-2017 |
Category: |
Education |
Upload: | arati-gadgil |
View: | 179 times |
Download: | 0 times |
JAVA Thread
Prepared by
Miss. Arati A. Gadgil
Thread
There are two ways to create a thread
1.By extending Thread class
2. By implementing Runnable interface
2
Thread States
1.New2.Runnable3.Running4.Non-Runnable (Blocked)5.Terminated
3
4
New
Runnable
Running
Terminated
Non-Runnable Blocked
start()
run()
Sleep,IO complete
Sleep,bock,IO wait
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
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
Runnable Interface
Runnable interface have only one method named run().
public void run()is used to perform action for a thread.
7
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
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
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
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
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
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
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
Thank You
15