A thread is known as a lightweight process. A thread is a basic processing unit to which an operating system can allocate processor time.It is better to use threads, but it doesn't mean that we can create as many as threads and our process will become more efficient. But as we know that threads also consume resources so we should create threads according to the need of application.
Every java program has at least one thread and that thread executes the java program.It is created when the main method of java program is invoked.A java program can have also more than one threads. For example a swing application has at least two threads.
Thread creation:Thread can be created in two ways:
1. Extend the java.lang.Thread class.
2. Implement the java.lang.Runnable interface.
In second method we have to override the run method. Now when the thread is started by calling the start() method run() method will automatically called.Once the run() method returns or throws an exception, the thread dies and will be garbage-collected.
A thread can be in following states during its lifetime:
new,runnable,blocked,waiting,terminated,timed_waiting
The values that represent these states are encapsulated in the java.lang.Thread.State enum. The members of this enum are
NEW, RUNNABLE, BLOCKED, WAITING,TERMINATED and TIMED_WAITING.
This is another method which is by implementing runnable method. In this example we have created a class which implements runnable interface,So we have to override run method of thread.In main method we have created an object of this class and passed this object in thread constructor so that a thread will be created for this class.In run method we are just printing numbers from 1 to 99 and we have also used a sleep method of thread which will sleep the thread for specified time.After we have started the thread by calling start() method, run() method of the thread will be automatically called
and executed.
Thread priority:When there are multiple threads in our application, we have to decide the scheduling of those threads. For this we can set the priorities to different threads. We can use the following method:
public final void setPriority(int priority)
Synchronization:Since we know that threads run independently but we may get a situation in which threads have to access the shared data. In this situation what will happen if two threads will access the same data and one thread is reading that data and other thread is modifying that data.
Now in this situation we can't guarantee for the correct data.So for these situations we have synchronization. The shared data can be synchronized so that if one thread is using that data no other thread can access that data until first thread has not completed the task. This synchronization is performed by using monitor lock. When a thread has a lock on some synchronized object and if another thread tries to access that thread then that thread will be blocked till the first thread finish the task using that synchronized object.
Method Synchronization:We can synchronize any method by writing synchronized keyword in method header.
Block synchronization:Block synchronization can be used to synchronize a block. Suppose an object has to be shared among threads and it is not synchronized and also we don't have access to its code then what will happen? What can we do?We can synchronize it by using block synchronization.
Suppose object1 needs to be synchronized then we can synchronize it by using this
So above is all about threads.Kindly comment if you want to know more.
Every java program has at least one thread and that thread executes the java program.It is created when the main method of java program is invoked.A java program can have also more than one threads. For example a swing application has at least two threads.
Thread creation:Thread can be created in two ways:
1. Extend the java.lang.Thread class.
2. Implement the java.lang.Runnable interface.
In second method we have to override the run method. Now when the thread is started by calling the start() method run() method will automatically called.Once the run() method returns or throws an exception, the thread dies and will be garbage-collected.
A thread can be in following states during its lifetime:
new,runnable,blocked,waiting,terminated,timed_waiting
The values that represent these states are encapsulated in the java.lang.Thread.State enum. The members of this enum are
NEW, RUNNABLE, BLOCKED, WAITING,TERMINATED and TIMED_WAITING.
package threads;
public class ExtendThread extends Thread {
public static void main(String[] args) {
ExtendThread thread = new ExtendThread();
thread.start();
System.out.println("Hi this is a thread example");
}
}
Above is an example of creating thread using method 1public class ExtendThread extends Thread {
public static void main(String[] args) {
ExtendThread thread = new ExtendThread();
thread.start();
System.out.println("Hi this is a thread example");
}
}
package threads;
public class ImplementsRunnable implements Runnable {
public void run() {
for(int i=0;i<100;i++){
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ImplementsRunnable obj = new ImplementsRunnable();
Thread thread = new Thread(obj);
thread.start();
}
}
public class ImplementsRunnable implements Runnable {
public void run() {
for(int i=0;i<100;i++){
System.out.println(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ImplementsRunnable obj = new ImplementsRunnable();
Thread thread = new Thread(obj);
thread.start();
}
}
This is another method which is by implementing runnable method. In this example we have created a class which implements runnable interface,So we have to override run method of thread.In main method we have created an object of this class and passed this object in thread constructor so that a thread will be created for this class.In run method we are just printing numbers from 1 to 99 and we have also used a sleep method of thread which will sleep the thread for specified time.After we have started the thread by calling start() method, run() method of the thread will be automatically called
and executed.
Thread priority:When there are multiple threads in our application, we have to decide the scheduling of those threads. For this we can set the priorities to different threads. We can use the following method:
public final void setPriority(int priority)
Synchronization:Since we know that threads run independently but we may get a situation in which threads have to access the shared data. In this situation what will happen if two threads will access the same data and one thread is reading that data and other thread is modifying that data.
Now in this situation we can't guarantee for the correct data.So for these situations we have synchronization. The shared data can be synchronized so that if one thread is using that data no other thread can access that data until first thread has not completed the task. This synchronization is performed by using monitor lock. When a thread has a lock on some synchronized object and if another thread tries to access that thread then that thread will be blocked till the first thread finish the task using that synchronized object.
Method Synchronization:We can synchronize any method by writing synchronized keyword in method header.
public synchronized int addition(int a, int b) {
return a+b;
}
return a+b;
}
Block synchronization:Block synchronization can be used to synchronize a block. Suppose an object has to be shared among threads and it is not synchronized and also we don't have access to its code then what will happen? What can we do?We can synchronize it by using block synchronization.
Suppose object1 needs to be synchronized then we can synchronize it by using this
synchronized(object1){
//call the methods of that object
}
//call the methods of that object
}
So above is all about threads.Kindly comment if you want to know more.
0 comments:
Post a Comment