Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Threading: How does implementing runnable work for threading

I understand that if you want to thread you can either extend thread or implement runnable to multithread in java. But why do you have to implement an interface for java to thread? Whats the importances of the runnable interface that makes java threading work? Does Java's interface extend from something?

like image 426
stackoverflow Avatar asked Oct 19 '12 01:10

stackoverflow


People also ask

Why is it better to implement runnable interface while creating threads?

When we extend Thread class, we can't extend any other class even we require and When we implement Runnable, we can save a space for our class to extend any other class in future or now. When we extend Thread class, each of our thread creates unique object and associate with it.

What is the relationship between thread and runnable?

Runnable is an interface which represents a task that could be executed by either a Thread or Executor or some similar means. On the other hand, Thread is a class which creates a new thread. Implementing the Runnable interface doesn't create a new thread. Java Docs clearly explains the difference between them.

Can we extend thread and implement runnable at the same time?

You should almost never do that. The type Thread already implements Runnable . The only reason to do this is if you want to be explicit in your source code. There is only one way to create a thread: creating a Thread instance and invoking its start() method.


2 Answers

The only thing special about the Runnable interface is that it is what Thread takes in its constructor. It's just a plain-old interface.

As with most interfaces, the point is that you're programming to a contract: you agree to put the code you want to run in the Runnable#run() implementation, and Thread agrees to run that code in another thread (when you create and start a Thread with it).

It's Thread that actually "does" the multithreading (in that it interacts with the native system). An implementation of Runnable is just where you put the code that you want to tell a Thread to run.

In fact, you can implement a Runnable and run it, without having it run in a separate thread:

Runnable someCode = new Runnable() {
    public void run() {
       System.out.println("I'm a runnable");
    }
};
someCode.run();

So Runnable itself doesn't have anything to do with multi-threading, it's just a standard interface to extend when encapsulating a block of code in an object.

like image 172
Mark Peters Avatar answered Oct 04 '22 02:10

Mark Peters


In terms of functionality, there is no difference between implementing Runnable interface or extending Thread class. But there might be situations that implementing Runnable interface could be preferred. Think of the case that your class has to inherit from some other class and also it should show thread functionality. Since your class cannot inherit from multiple classes(Java doesn't support it), your class could implement Runnable interface in that case.

like image 42
Juvanis Avatar answered Oct 04 '22 02:10

Juvanis