best way for implementing thread in java

Discussion in 'Java' started by khushboo652, Jan 11, 2012.

  1. khushboo652

    khushboo652 Banned

    Joined:
    Jan 9, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    pls give me the answer which is the best way to implement thread in java runnable interface or inheritance and why?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What all ways you have tried and looking for best amont them?
     
  3. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
  4. dagdu

    dagdu New Member

    Joined:
    Feb 8, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    By implementing runnable interface is best way to implement.By implementing we can reuse that thread and we can extends any other useful class.
     
  5. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
  6. RRT2010

    RRT2010 New Member

    Joined:
    Apr 7, 2011
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    India
    Also you can see if you require to Synchronize the threads. This may be needed if you have many threads running in parallel to avoid concurrency.
     
  7. ifeeleducation

    ifeeleducation New Member

    Joined:
    Mar 2, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    CEO
    Location:
    Mumbai
    Home Page:
    http://ifeel.edu.in/
    Use Class is the best way for implementing thread in java
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    On what basis you say it is the best?
     
  9. theglobalassociates

    theglobalassociates New Member

    Joined:
    Apr 5, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://theglobalassociates.com/b2b-lead-generation/
    1.Extending Thread Class
    2.Implementing Runnable Class
     
  10. Jeff Ronald

    Jeff Ronald Member

    Joined:
    Dec 9, 2016
    Messages:
    58
    Likes Received:
    0
    Trophy Points:
    6
    Gender:
    Male
    Home Page:
    http://ezmoov.com
    There are two ways:
    1.Extending thread class
    2.Using Runnable Interface.
     
  11. persysweb

    persysweb Member

    Joined:
    Aug 1, 2017
    Messages:
    98
    Likes Received:
    18
    Trophy Points:
    8
    Location:
    India
    Home Page:
    httpS://persys.in/
    The best way to implement thread in java is by implementing Runnable Interface.
    As java does not support multiple inheritance,to remove the ambiguity problem multiple inheritance is not supported by java.
    eg. class A extends B,C this cannot be achieved in java .

    But Multiple inheritance can be achieved in java by using Interface.
    eg.class A extends B implements Runnable.
    So by using Runnable Interface we are saving one class for Extend.
     
  12. priya456

    priya456 New Member

    Joined:
    Jul 7, 2017
    Messages:
    26
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Female
    There are two method for creating thread

    1) Extending Thread Class

    2) Implementing Runnable Interface

    Why implements runnable is preferred over extends thread?

    When you extend Thread class, you can’t extend any other class which you require . When you implement Runnable, you can save a space for your class to extend any other class in future or now.
     
  13. meenal deshpande

    meenal deshpande New Member

    Joined:
    Oct 11, 2018
    Messages:
    20
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Female
    Making a string in Java is done this way:

    String = new Thread();

    To begin the Java string you will call its begin() technique, similar to this:

    thread.start();

    This model doesn't indicate any code for the string to execute. The string will stop again immediately after it is begun.

    There are two different ways to determine what code the string ought to execute. The first is to make a subclass of Thread and supersede the run() strategy. The second strategy is to pass a protest that executes Runnable (java.lang.Runnable to the Thread constructor. The two strategies are secured underneath.

    String Subclass

    The primary method to determine what code a string is to run, is to make a subclass of Thread and abrogate the run() strategy. The run() strategy is what is executed by the string after you call begin(). Here is a case of making a Java Thread subclass:

    open class MyThread expands Thread {

    open void run(){

    System.out.println("MyThread running");

    }

    }

    To make and begin the above string you can do this way:

    myThread = new MyThread();

    myTread.start();

    The begin() call will return when the string is begun. It won't hold up until the run() technique is finished. The run() strategy will execute as though executed by an alternate CPU. At the point when the run() strategy executes it will print out the content "MyThread running".

    You can likewise make a mysterious subclass of Thread this way:

    String = new Thread(){

    open void run(){

    System.out.println("Thread Running");

    }

    }

    thread.start();

    This model will print out the content "String running" when the run() technique is executed by the new string.

    Runnable Interface Implementation

    The second method to indicate what code a string should run is by making a class that actualizes the java.lang.Runnable interface. A Java question that actualizes the Runnable interface can be executed by a Java Thread. How that is done is demonstrated somewhat later in this instructional exercise.

    The Runnable interface is a standard Java Interface that accompanies the Java stage. The Runnable interface just has a solitary technique run(). Here is essentially how the Runnable interface looks:

    open interface Runnable() {

    open void run();

    }

    Whatever the string should do when it executes must be incorporated into the usage of the run() strategy. There are three different ways to actualize the Runnable interface:

    Make a Java class that executes the Runnable interface.

    Make an unknown class that actualizes the Runnable interface.

    Make a Java Lambda that executes the Runnable interface.

    Every one of the three alternatives are clarified in the accompanying segments.

    Java Class Implements Runnable

    The main method to actualize the Java Runnable interface is by making your own Java class that executes the Runnable interface. Here is a case of a custom Java class that executes the Runnable interface:

    open class MyRunnable actualizes Runnable {

    open void run(){

    System.out.println("MyRunnable running");

    }

    }

    This Runnable execution does is to print out the content MyRunnable running. Subsequent to printing that content, the run() technique exits, and the string running the run() strategy will stop.

    Mysterious Implementation of Runnable

    You can likewise make a mysterious execution of Runnable. Here is a case of an unknown Java class that executes the Runnable interface:

    Runnable myRunnable =

    new Runnable(){

    open void run(){

    System.out.println("Runnable running");

    }

    }

    Aside from being an anononymous class, this model is very like the precedent that utilized a custom class to execute the Runnable interface.
     
    Last edited: Dec 28, 2018
    shabbir likes this.
  14. sayalipatil

    sayalipatil New Member

    Joined:
    Dec 18, 2018
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Home Page:
    https://crbtech.in/online-java-training-course
    Thread must be implemented by Implementing Runnable Interface.

    As Java does not support Multiple Inheritance (1 class cannot be inherited from 2 super class).For eg: class A extends B,Thread — This cannot be achieved in java but can be done in C++.

    But Multiple Inheritance can be achieved in java by using Interface. For eg: class A extends B Implement Runnable.

    So by using Runnable Interface you are saving one class for Extends.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice