Hub Of Geekz

  • Home
  • About Me
  • Contact Us
  • Home
  • Languages
    • C++
    • Java
    • Perl
    • Prolog
    • Bootstrap
  • Database
    • SQL
    • PL/SQL
  • Study
    • Java
      • Java Collection
      • Java Concurrency
      • Java Interview Questions
      • Java Networking
    • Number System
  • Kavita
  • Entertainment
    • Hinglish Cafe
    • Videos
    • Jokes
  • Windows Tricks
  • How To

Tuesday, 3 March 2015

Concurrency in Java

 Earthcare Foundation NGO     09:21     Concurrency, Java, Thread     No comments   

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.
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.

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.
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.
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.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Ad


Jobsmag.inIndian Education BlogThingsGuide

Subscribe

Do you want fresh and hot updates about us? Then subscribe to our Feeds.

Total Pageviews

Popular Posts

  • Write a program in PL/SQL to print the factorial of a number.
    In this post I will explain how to get the factorial of any given number. For that first you need to know what is the procedure to find ...
  • To find the GCD of two numbers in PROLOG.
    gcd(X,Y):-X=Y,write('GCD of two numbers is '),write(X); X=0,write('GCD of two numbers is '),write(Y); Y=0,write('G...
  • Write a PL/SQL code to get the Fibonacci Sequence
    First, I will explain what is Fibonacci Sequence and how to get this series. So, Fibonacci Sequence is a series of numbers 0,1,1,2,3,5,8,1...

Label

All Articles Best Resources Blogging Boost Traffic Bootstrap C Plus Plus Collection Comedy Comedy Posts Comedy Videos Concurrency creative commons website Education Employee Entertainment Fibonacci Sequence free images GirlFriend Hinglish Cafe How To Image Websites Inspirational Java Java Interview Questions Java Networking Kavita Sangrah Life Lock Sreen Love Number System Patterns Perl Picture PL/SQL Plastic Engineering Programming Prolog public domain SEO Servlet Short Story Shortcut Keys Social Media Social Services SQL SuVichar Thread Traffic True Events Ultimate Guide Windows Tricks Windows8.1 WordPress

Blog Archive

  • ►  2020 (43)
    • ►  September (41)
    • ►  August (2)
  • ►  2019 (1)
    • ►  July (1)
  • ►  2018 (9)
    • ►  September (7)
    • ►  July (1)
    • ►  May (1)
  • ►  2017 (8)
    • ►  June (3)
    • ►  May (3)
    • ►  March (1)
    • ►  January (1)
  • ►  2016 (2)
    • ►  September (1)
    • ►  January (1)
  • ▼  2015 (91)
    • ►  December (1)
    • ►  November (1)
    • ►  October (6)
    • ►  May (10)
    • ▼  March (20)
      • ArrayList Methods in Java
      • ArrayList Methods in Java
      • ArrayList in Java
      • Arrays Class in Java
      • Java Interview Question-Variable length arguments ...
      • Array in Java
      • Data Hierarchy in Operating Systems
      • Synchronization in Java
      • Typecasting in Java
      • Concurrency in Java
      • Runnable in Java
      • HashMap in Java
      • Reverse of a given number using Java
      • Star Pattern printing in Java
      • Difference between fail-fast and fail-safe iterators
      • Factorial of a number in Java
      • Null in Java
      • Final in Java
      • Immutable Objects in Java
      • Thread concept in java
    • ►  February (50)
    • ►  January (3)
  • ►  2014 (339)
    • ►  December (1)
    • ►  October (55)
    • ►  September (58)
    • ►  August (94)
    • ►  July (64)
    • ►  June (67)
  • ►  2013 (34)
    • ►  August (5)
    • ►  April (29)
  • ►  2012 (20)
    • ►  November (1)
    • ►  October (15)
    • ►  September (4)

Author

  • Earthcare Foundation NGO
  • Kavita house
  • Unknown
  • Unknown

Info

Copyright © Hub Of Geekz | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates