How does java runnable work




















Runnable is an interface that is to be implemented by a class whose instances are intended to be executed by a thread. There are two ways to start a new Thread — Subclass Thread and implement Runnable. There is no need of subclassing Thread when a task can be done by overriding only run method of Runnable. Steps to create a new Thread using Runnable : 1. Create a Runnable implementer and implement run method.

Instantiate Thread class and pass the implementer to the Thread , Thread has a constructor which accepts Runnable instance. Invoke start of Thread instance, start internally calls run of the implementer. If you pass an object that fails to both a declare that it implements Runnable , and b carries a method run that takes no arguments and returns no value, then the compiler flags that situation as an error.

So an executor service requires that you submit your task as an object of a class that implements the Runnable or Callable interface to guarantee that when a task arrives to be executed on a background thread, that task has a method named exactly run or call for Callable. Here is some example code. Notice how the executor service does not care what kind object you pass to its submit method. You could pass an object of the class Dog , SalesReport , or Payroll — does not matter.

All the executor service cares about is that the object passed to submit has a method called run. If you are comfortable with lambda syntax in modern Java, we can shorten to a single line that code defining your Runnable implementation. Same effect, just different syntax. All classes in Java extend either the Object class or some other class that extends from Object. An interface in Java does not extend from any class.

Remember that an interface is just a contract, a promise that some class may choose to make with regard to having methods with particular names that takes certain types of arguments and return a certain type of value. An interface in Java can extend one or more other interfaces. Doing so just adds more methods to the promise being made by a class claiming to implement that interface. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Java Threading: How does implementing runnable work for threading Ask Question. Asked 9 years ago.

Active 10 months ago. Viewed 7k times. Add a comment. A Java object that implements the Runnable interface can be executed by a Java Thread. How that is done is shown a bit later in this tutorial. The Runnable interface is a standard Java Interface that comes with the Java platform.

The Runnable interface only has a single method run. Here is basically how the Runnable interface looks:. Whatever the thread is supposed to do when it executes must be included in the implementation of the run method. There are three ways to implement the Runnable interface:. The first way to implement the Java Runnable interface is by creating your own Java class that implements the Runnable interface. Here is an example of a custom Java class that implements the Runnable interface:.

All this Runnable implementation does is to print out the text MyRunnable running. After printing that text, the run method exits, and the thread running the run method will stop.

You can also create an anonymous implementation of Runnable. Here is an example of an anonymous Java class that implements the Runnable interface:. Apart from being an anononymous class, this example is quite similar to the example that used a custom class to implement the Runnable interface. The third way to implement the Runnable interface is by creating a Java Lambda implementation of the Runnable interface. This is possible because the Runnable interface only has a single unimplemented method, and is therefore practically although possibly unintentionally a functional Java interface.

Here is an example of a Java lambda expression that implements the Runnable interface:. To have the run method executed by a thread, pass an instance of a class, anonymous class or lambda expression that implements the Runnable interface to a Thread in its constructor.

Here is how that is done:. When the thread is started it will call the run method of the MyRunnable instance instead of executing it's own run method. The above example would print out the text "MyRunnable running". There are no rules about which of the two methods that is the best. Both methods works. Personally though, I prefer implementing Runnable , and handing an instance of the implementation to a Thread instance.

When having the Runnable 's executed by a thread pool it is easy to queue up the Runnable instances until a thread from the pool is idle. This is a little harder to do with Thread subclasses. Sometimes you may have to implement Runnable as well as subclass Thread. For instance, if creating a subclass of Thread that can execute more than one Runnable.

This is typically the case when implementing a thread pool. When creating and starting a thread a common mistake is to call the run method of the Thread instead of start , like this:. At first you may not notice anything because the Runnable 's run method is executed like you expected. To use the Runnable interface to create and start a thread, you have to do the following:.

Create a class that implements Runnable. Provide a run method in the Runnable class. Create an instance of the Thread class and pass your Runnable object to its constructor as a parameter.



0コメント

  • 1000 / 1000