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

Runnable in Java

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

First question is,What is runnable?
So runnable is an interface which is used to provide concurrency to the application by providing the threads to the application.It is the general way of creating threads.

Why should we use runnable interface over Thread class?
If we use Thread class then we have to create our own threads.We will have to manage it on our own.Means a lot of burden will be on the application.
Lets know the execution of runnable interface.So runnable object can be executed by Executor object.So what is this Executor?
An Executor object is used to execute runnable objects.It does this by creating and managing a group of threads in a thread pool.When a Executor begins executing a Runnable,the executor calls the Runnable's run method which is executed in the new thread.
So the next question is how does this Executor manages the threads?
Executor interface has a single method named as execute() which accepts the Runnable as argument.When execute method is called it selects a thread and assigns this thread to the passed Runnable object.If there is no thread available in thread pool then Executor creates the new thread and assigns it to the Runnable object.So this is the main advantage of using Runnable over Thread class.Since Executor can reuse the threads to eliminate the overhead of creating the thread whenever possible.This will optimize the performance of application.
Executor Service can be started by following statement:
ExecutorService service = Executors.newCachedThreadPool();


Program Code:
package examples;

public class ThreadEx  implements Runnable{

String name;
int time;
public ThreadEx(String name,int time){
this.name=name;
this.time=time;
}
@Override
public void run() {

System.out.println("Hi this is thread "+name);
try{
Thread.sleep(time);
}catch(InterruptedException e){
return ;
}
System.out.println("I am last executable line in thread "+name);

}

}

package examples;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadDemo {

public static void main(String[] args) {

ThreadEx t1=new ThreadEx("t1",2000);
ThreadEx t2=new ThreadEx("t2",1000);
ThreadEx t3=new ThreadEx("t3",1500);

System.out.println("Starting Executor");

ExecutorService service=Executors.newCachedThreadPool();
service.execute(t1);
service.execute(t2);
service.execute(t3);

service.shutdown();
System.out.println("Executor Shutdown");
}

}

Output1:
Starting Executor
Hi this is thread t1
Executor Shutdown
Hi this is thread t2
Hi this is thread t3
I am last executable line in thread t2
I am last executable line in thread t3
I am last executable line in thread t1

Output2:
Starting Executor
Executor Shutdown
Hi this is thread t2
Hi this is thread t1
Hi this is thread t3
I am last executable line in thread t2
I am last executable line in thread t3
I am last executable line in thread t1
In output2 main thread terminates before any of the ThreadEx objects outputs their statements and in output1 t1 object has output its statement before main thread terminated.This illustrates the face that we can't predict the order in which the tasks will be executed.It doesn't dependes upon the order in which they have created and started.
  • 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