Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
  • Home
  • Android
  • Buy Projects
  • My Account
    • Become a Member
    • Log In|Log Out
    • Free Projects
No Result
View All Result
Camposha
No Result
View All Result
ADVERTISEMENT
Home Java

Java Threading – Runnable Interface

2 years ago
in Java
Reading Time: 2min read
38 2
A A
40
SHARES
4k
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

SimilarPosts

Java Iterator

Java Iterator

2 years ago
4k
Java Iterable Interface

Java Iterable Interface

2 years ago
4k
Java Control Structures – Loops Introduction

Java Control Structures – Loops Introduction

2 years ago
4k
Java Operators – Logical Operators

Java Operators – Logical Operators

2 years ago
4k

A Runnable is an abstraction for an executable command.

It’s an interface that defined in the java.lang package:

1
package java.lang;

1
public interface Runnable{}

Usage

The Runnable interface is used extensively to execute code in Threads.

The Runnable interface has one abstract method called run():

1
public abstract void run ()

Normally you implement that method whenever a your class implements Runnable interface.

The method will then get called to start executing the active part of the class’ code.

Runnable Interface Grandchildren

Well we mean indirect subclasses. Runnable has alot of them so we list only afew commonly used:

No. Class Description
1. Thread A concurrent unit of execution.This class implements Runnable interface.The Thread class makes use of the Runnable’s abstract run() method by calling it’s(the Thread’s) concrete run() method. That run() method then calls the Runnable’s abstract run() method internally.
2. TimerTask An abstract class used to describe a task that should run at a specified time.Maybe once,Maybe recurringly.TimerTask implements Runnable interface.The tasks done by the TimerTask do get specified in the run() method.
3. FutureTask<> A class that represents a ceancellable asynchronous computation.This class implements the RunnableFuture interface, which implements the Runnable interface.

Let’s now see some examples:

Java Runnable Example

Let’s see an example of Java Runnable with Threads.

  1. First specify your application’s package:
    1
    package info.tutorialsloop;
    Packages group classes.
  2. Create the class:
    1
    public class Main{}
  3. Let’s make our class implement Runnable interface:
    1
    public class Main implements Runnable {}
  4. This will force us override the run() method.So we do so:
    1
    2
    3
    4
    @Override
    public void run() {
     
    }
  5. Let’s create two fields at the class level.One to represent the distance our Spaceship has travelled while another a boolean to be flagged to false or true depending on whether we’ve arrived or not:
    1
    2
    private static int spaceship_distance=0;
    private boolean arrived=false;
  6. Then go back to the run() method and add the following code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
        while (!arrived)
        {
            try{
                System.out.println("Distance in Light Years : "+spaceship_distance++);
                Thread.sleep(1000);
                if(spaceship_distance==10)
                {
                    arrived=true;
                    System.out.println("Reached Destination. Thanks.");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
  7. We then create our main method:
    1
    2
    3
    public static void main(String[] args) {
     
    }
  8. Then add the following code in the main method:
    1
    2
    3
        Main m=new Main();
        Thread myThread=new Thread(m);
        myThread.start();
    Basically we are instantiating a thread and passing in our Main class instance. We then start the thread via the start() method.

Here’s what we get:

1
2
3
4
5
6
7
8
9
10
11
Distance in Light Years : 0
Distance in Light Years : 1
Distance in Light Years : 2
Distance in Light Years : 3
Distance in Light Years : 4
Distance in Light Years : 5
Distance in Light Years : 6
Distance in Light Years : 7
Distance in Light Years : 8
Distance in Light Years : 9
Reached Destination. Thanks.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
Share16Tweet10Send
ADVERTISEMENT

Related Posts

Java Iterator
Java

Java Iterator

December 19, 2020
4k
Java Iterable Interface
Java

Java Iterable Interface

December 19, 2020
4k
Java Control Structures – Loops Introduction
Java

Java Control Structures – Loops Introduction

December 19, 2020
4k
Java Operators – Logical Operators
Java

Java Operators – Logical Operators

December 19, 2020
4k
Java Operators – Relational Operators
Java

Java Operators – Relational Operators

December 19, 2020
4k
Java URI
Java

Java URI

December 19, 2020
4k
ADVERTISEMENT

Get Free Projects

  • Home
  • Android
  • Buy Projects
  • My Account

© 2021 Camposha

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

© 2021 Camposha

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In