multi threading
Multi threading
Multi threading
Threads A program can be divided into a number of small processes. Each small
process can be addressed as a single thread.
process can be addressed as a single thread.
Multithreading in java is a process of executing multiple threads simultaneously.
In Java, the word thread means two different things.
· An instance of Thread class.
· or, A thread of execution.
Advantages of Multi-threading in Java
1. It allows multiple operations at once, as they are not dependent on each other, thus not blocking the user.
2. It saves time as multiple operations are possible.
3. They are independent thus making the functionality better.
Life cycle of threads
- New : A thread begins its life cycle in the new state. It remains in this state until the start() method is called on it.
- Runnable : After invocation of start() method on new thread, the thread becomes runnable.
- Running : A thread is in running state if the thread scheduler has selected it.
- Waiting : A thread is in waiting state if it waits for another thread to perform a task. In this stage the thread is still alive.
- Terminated : A thread enter the terminated state when it complete its task.
Commonly used methods of Thread class:
1. public void run(): is used to perform action for a thread.
2. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
3. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
4. public void join(): waits for a thread to die.
5. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
6. public int getPriority(): returns the priority of the thread.
7. public int setPriority(int priority): changes the priority of the thread.
8. public String getName(): returns the name of the thread.
9. public void setName(String name): changes the name of the thread.
10. public Thread currentThread(): returns the reference of currently executing thread.
11. public int getId(): returns the id of the thread.
12. public Thread.State getState(): returns the state of the thread.
13. public boolean isAlive(): tests if the thread is alive.
14. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
15. public void suspend(): is used to suspend the thread(depricated).
16. public void resume(): is used to resume the suspended thread(depricated).
17. public void stop(): is used to stop the thread(depricated).
|
Implementing the Runnable Interface
The easiest way to create a thread is to create a class that implements the runnable interface. After implementing runnable interface , the class needs to implement the run() method, which is of form,
publicvoid
run()
· run() method introduces a concurrent thread into your program. This thread will end when run() method terminates.
· You must specify the code that your thread will execute inside run() method.
· run() method can call other methods, can use other classes and declare variables just like any other normal method.
class cse implements Runnable{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
cse obj=new cse();
Thread T1 =new Thread(obj);
T1.start();
}
}
Output
My thread is in running state.
Creating multi threads using extends
The procedure for creating threads based on extending the Thread is as follows:
1. A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.
2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call.
3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running.
Below is a program that illustrates instantiation and running of threads by extending the Thread class instead of implementing the Runnable interface. To start the thread you need to invoke the start()method on your object.
class bkec extends Thread{
public void run(){
System.out.println("BKEC thread ");
}
public static void main(String args[]){
bkec t1=new bkec();
t1.start();
}
}
Thread class with sleep() method
class Test extends Thread
{
public void run(){
for(int i=1;i<5;i++){
try
{
Thread.sleep(500);
}catch(InterruptedException e)
{
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String args[]){
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}
Output 1 2 2 3 3 4 4