pls give me the answer which is the best way to implement thread in java runnable interface or inheritance and why?
By implementing runnable interface is best way to implement.By implementing we can reuse that thread and we can extends any other useful class.
Hello, There are two ways of implementing threading in Java 1) By extending java.lang.Thread class, or 2) By implementing java.lang.Runnable interface. Read more: http://javarevisited.blogspot.in/2011/02/how-to-implement-thread-in-java.html Check this site for examples.
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.
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.
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.
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.
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.