Every computer user want to do works in a fast way and they also want to do multiple works at a time. Suppose a user is working on MS Paint and now he can't open anything else. Means when he will finish the paint related work after that he can open other applications.Then it will be very bad for user.So to overcome these types of situations concurrency has introduced.So that user can run multiple applications at a time and the processing of the application increases.Even concurrency can be applied at the process level so that appication can concurrently execute more than one task at a time.
Concurrent programming difficulties:Concurrent programming is not so easy.Suppose you are reading one newspaper,one novel and one maths book and you are reading one book for some seconds say 15 and after that you are reading newspaper for 15 seconds and then you are reading novel for 15 seconds.What are the complexities in this procedure.There are a lot of things you have to do.First you have to switch between these readings and after that you have to remember the page numbers and then you also have to remember the line number so that you can continue.So above is a simple example by which you can understand that concurrent programming is not a simple task.
In concurrent programming there are two basic units of execution:
1)Process
2)Thread
A process has a self contained execution unit.Processes communicate to each other via IPC(Inter process communication).Each process has its own memory space.Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using a ProcessBuilder object.
A thread is also known as the lightweight process.Thread also provides a execution environment.Each process has atleast one thread.A thread can also create another thread.In java when you run the program a thread is created called main thread which can also create another thread.
We can define and start a thread in two ways:
1)Using runnable interface
2)inheriting Thread class
Click Here to know implementation of thread
Which of two ways is more beneficial.The first way which employe a runnable object is more general because it can subclass any class apart from Thread.The second method is easier to use in simple applications.So Runnable is used for high level applications and is more flexible approach.
Sleep():Thread.sleep causes the current thread to suspend execution for specified time.This method is provided so that other thread of the same process can use the processor time or any other application can also make use of that time.There are two overloaded methods:
sleep(time in milisecond)
sleep(time in milisecond,time in nanosecond)
However these sleep times are not guaranteed to be precise because they are facilitated by underlying OS.
Interrupts:Interrupts are the indications to the threads to stop doing the thing which they are currrently doing and its upto the programmer how has he programmed the thread to do when an interrupt has generated. But in most scenarios threads are terminated.
If thread is in any method and and it is interrupted then the best option is that thread should return from that method.We can explain it in example:
Now there can be a case,in above example we know that Thread.sleep() method will throw an exception an d then we can know there is interruption but suppose thread is using a method which does not use any other method which wil throw an exception.Then how will that thread will be interrupted.So for this we will use Thread.interrupted() which will return true if there is interrupt so that thread can be interupted.
For example:
Suppose there are three threads T1,T2,T3 and we want the sequence to be T1,T2,T3 then we can do one thing we can call T1.join() and then we can call T2.join.So T1 will be completed first then T2 and then T3.In this way we can preserve the sequence of executing the threads.
So above is basics about concurrency in java.
Concurrent programming difficulties:Concurrent programming is not so easy.Suppose you are reading one newspaper,one novel and one maths book and you are reading one book for some seconds say 15 and after that you are reading newspaper for 15 seconds and then you are reading novel for 15 seconds.What are the complexities in this procedure.There are a lot of things you have to do.First you have to switch between these readings and after that you have to remember the page numbers and then you also have to remember the line number so that you can continue.So above is a simple example by which you can understand that concurrent programming is not a simple task.
In concurrent programming there are two basic units of execution:
1)Process
2)Thread
A process has a self contained execution unit.Processes communicate to each other via IPC(Inter process communication).Each process has its own memory space.Most implementations of the Java virtual machine run as a single process. A Java application can create additional processes using a ProcessBuilder object.
A thread is also known as the lightweight process.Thread also provides a execution environment.Each process has atleast one thread.A thread can also create another thread.In java when you run the program a thread is created called main thread which can also create another thread.
We can define and start a thread in two ways:
1)Using runnable interface
2)inheriting Thread class
Click Here to know implementation of thread
Which of two ways is more beneficial.The first way which employe a runnable object is more general because it can subclass any class apart from Thread.The second method is easier to use in simple applications.So Runnable is used for high level applications and is more flexible approach.
Sleep():Thread.sleep causes the current thread to suspend execution for specified time.This method is provided so that other thread of the same process can use the processor time or any other application can also make use of that time.There are two overloaded methods:
sleep(time in milisecond)
sleep(time in milisecond,time in nanosecond)
However these sleep times are not guaranteed to be precise because they are facilitated by underlying OS.
Program Code:
package examples;
public class ThreadSleep {
public static void main(String[] args) throws InterruptedException {
for (int i=0;i<10;i++){
//sleep for two seconds
Thread.sleep(2000);
System.out.println(i);
}
}
}
Output:
0
1
2
3
4
5
6
7
8
9
Numbers from 0 to 9 has been printed at a interval of 2 seconds.In program main method throws an InterruptedException.This is an exception that sleep throws when another thread interrupts the current thread while sleep is active.package examples;
public class ThreadSleep {
public static void main(String[] args) throws InterruptedException {
for (int i=0;i<10;i++){
//sleep for two seconds
Thread.sleep(2000);
System.out.println(i);
}
}
}
Output:
0
1
2
3
4
5
6
7
8
9
Interrupts:Interrupts are the indications to the threads to stop doing the thing which they are currrently doing and its upto the programmer how has he programmed the thread to do when an interrupt has generated. But in most scenarios threads are terminated.
If thread is in any method and and it is interrupted then the best option is that thread should return from that method.We can explain it in example:
public class ThreadSleep {
public static void display() {
for (int i=0;i<10;i++){
//sleep for two seconds
try{
Thread.sleep(2000);
}catch(InterruptedException e){
return ;
}
System.out.println(i);
}
}
}
So in above method if exception comes then it should return from that method.public static void display() {
for (int i=0;i<10;i++){
//sleep for two seconds
try{
Thread.sleep(2000);
}catch(InterruptedException e){
return ;
}
System.out.println(i);
}
}
}
Now there can be a case,in above example we know that Thread.sleep() method will throw an exception an d then we can know there is interruption but suppose thread is using a method which does not use any other method which wil throw an exception.Then how will that thread will be interrupted.So for this we will use Thread.interrupted() which will return true if there is interrupt so that thread can be interupted.
For example:
public void disp(){
System.out.println("Hi this is abhishek awasthi");
if(Thread.interrupted())
return ;
}
join():Join method allows one thread to wait until other is complete.Suppose there are more than one thread and you want a proper sequence in which the thread should be executed then what can we do.Well we can use this join method.System.out.println("Hi this is abhishek awasthi");
if(Thread.interrupted())
return ;
}
Suppose there are three threads T1,T2,T3 and we want the sequence to be T1,T2,T3 then we can do one thing we can call T1.join() and then we can call T2.join.So T1 will be completed first then T2 and then T3.In this way we can preserve the sequence of executing the threads.
So above is basics about concurrency in java.
0 comments:
Post a Comment