Java threading: how runnable is done for threading

I understand that if you want to sink, you can either expand the thread or implement runnable for multithread in java. But why should you implement an interface for java for a stream? What are the benefits of a runnable interface that makes java threading work? Is the Java interface from something?

+4
source share
3 answers

The only thing that is especially important in the Runnable interface is what Thread accepts in its constructor. It is just a simple interface.

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

It Thread , which actually "performs" multithreading (in that it interacts with its own system). The Runnable implementation is where you put the code you want to tell Thread to run.

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

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

So Runnable itself has nothing to do with multithreaded, it is just a standard interface for expanding when encapsulating a block of code in an object.

+10
source

In terms of functionality, there is no difference between implementing a Runnable interface or extending the Thread class. But there may be situations where the implementation of the Runnable interface may be preferable. Think about the fact that your class should inherit from some other class, and should also show the functionality of the stream. Since your class cannot inherit multiple classes (Java does not support it), your class can implement the Runnable interface in this case.

+2
source

But why should you implement an interface for java for a stream?

You do not have, as you said earlier, you can extend the Thread object and implement the public void run method. If you want a more organized and flexible (yes, flexible) approach, you definitely want to use Runnable for the obvious reason: code reuse.

When I talk about organized, I want to say that it’s easy to maintain

 Runnable doSomething = new Runnable() { @Override public void run() { goAndDoSomethingReallyHeavyWork(); } }; 

and then reuse the same runnable for another thread or the same thread at another moment (yes, you can actually use Thread) than to extend 2 or more threads into objects that you will use once.

What are the values ​​of the runnable interface that does work with java thread?

The important thing is that the Thread object will β€œknow” that your Runnable starts the method and executes it when it has to (and so stop, pause, and other Thread actions).

Is there a Java interface from something?

This question is worth my +1 to you. I would really like to know, but it seems that this is a feature of the language, not a product of itself, like any other object that extends the superclass of the Object class.

Hope this helps. Greetings

+1
source

Source: https://habr.com/ru/post/1440674/


All Articles