Multithreading:
Execution of more than one thread at a time is called as multithreading.
Thread:
A thread is a piece of code that executes independently.
Every program contains at least one thread. i.e. main thread
Main thread default name is main only.
Main thread default priority is normal priority(Priority number is 5)
There are two ways to create a new thread:
1) By extending java.lang.Thread class
2) By implementing a java.lang.Runnable interface
Life cycle of a thread (or) thread states:
Whenever thread class constructor is called new thread will born.
Thread comes to a ready state whenever start() method is called.
Thread will run whenever run() method is called.
Thread comes to a sleeping state whenever sleep() method is called.
Sleeping thread will wake up automatically whenever time interval is finished.
Thread is suspended whenever suspend() method is called.
Suspended thread will run whenever resume() method is called.
Thread comes to a waiting state whenever wait() method is called.
Waiting thread will run whenever notify() method is called.
Thread will die whenever destroy() method is called.
Program to get current thread information:
class Demo
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
System.out.println(t.getName());
System.out.println(t.getPriority());
t.setName(“demo”);
t.setPriority(Thread.MAX_PRIORITY);
System.out.println(t.getName());
System.out.println(t.getPriority());
}
}
Steps to develop multithreaded application by extending
java.lang.Thread class:
1) Create a class that extends java.lang.Thread class
2) Override run() method.
Note: run() method given as a null body method to write child thread task code.
3) Write child thread task code in a run() method.
4) Write main() method
5) Create an object of current class.
6) Call start() method.
Note: start() method implicitly calls run() method.
7) Write main thread task code in a main() method.
Example:
class Demo extends Thread
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
System.out.println("Child Thread: "+i);
Thread.sleep(1000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
public static void main(String args[])
{
try{
Demo d=new Demo();
d.start();
for(int i=1;i<=10;i++)
{
System.out.println("Main Thread: "+i);
Thread.sleep(1000);
if(i==5)
d.suspend();
if(i==10)
d.resume();
}
}catch(Exception e)
{
System.err.println(e);
}
}
}
Steps to create multithread application by implementing
java.lang.Runnable interface:
1) Create a class that implements java.lang.Runnable interface.
2) Override run() method.
3) Write child thread task code in a run() method.
4) Write main() method
5) Create an object of current class & assign to Runnable reference
6) Create an object Thread class by passing Runnable reference
7) Call start() method.
8) Write main thread task code in a main() method.
Example:
class Test implements Runnable
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
System.out.println("JavaEE");
Thread.sleep(1000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
}
class Demo implements Runnable
{
public void run()
{
try{
for(int i=1;i<=10;i++)
{
System.out.println("Core Java");
Thread.sleep(2000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
public static void main(String args[])
{
try{
Runnable r=new Demo();
Thread t=new Thread(r);
t.start();
Runnable r2=new Test();
Thread t2=new Thread(r2);
t2.start();
for(int i=1;i<=10;i++)
{
System.out.println("Advanced Java");
Thread.sleep(3000);
}
}catch(Exception e)
{
System.err.println(e);
}
}
}
Synchronization:
It is a mechanism that allows to access a shared resource only
one thread at a time.
There are two ways to synchronize the code:
1) synchronizing a method
2) synchronizing block of code
1) synchronizing a method:
Syntax:
synchronized ReturnType MethodName(arg1, arg2, ......)
{
================
================
================
}
2) synchronizing a block of code:
Syntax:
ReturnType MethodName(arg1, arg2, ......)
{
================
================
synchronized(Object)
{
================
================
}
================
================
}