Java Interface Question

I am confused (new to java):

When implementing the Runnable interface, you must override the run () method in order to be able to execute threads. Implementing this interface makes your object type Runnable (?). How does a thread function get an “injection” just by implementing the Runnable interface? Basically, when you instantiate a class that implements Runnable, what happens to these associations in the streaming functions? I'm probably mistaken based on the concept of OO. Thanks.

Is the JVM "aware" to search for runnable when executing thread.start ()?

+4
source share
7 answers

When you create a Runnable implementation, nothing associates your class with the capabilities of the JVM thread. An instance of a Runnable interface is like an instance of any other interface, just like another instance.

If you want to use the JVM threading system, you need to use a new instance of the Thread class that will run the run() method of your Runnable implementation in a separate thread.

All the logic for creating a new thread is performed by the Thread class.

+9
source

Runnable is a piece of work that "can" run in a separate thread or not (you can just call Runnable.run () yourself).

However, to call Runnable in a separate thread, do something like

 Thread thread = new Thread(new MyRunnable()); thread.start(); // MyRunnable will now be invoked in a new thread 
+4
source

Nothing special happens in the background.

Implementing the Runnable interface ensures that your class has a public void run() method.

The magic really happens when you pass your custom class to Thread .

 Thread th = new Thread(new YourCustomRunnable()); th.start(); 

In the above code, a new Thread will be created, and the code inside the run() method will be launched in another thread.

Internally, the thread will call your own run() method and make this code run in a separate thread. Technically, you can do the following:

 Runnable r = new MyCustomRunnable(); r.run(); 

In the above code, r will not work in a separate thread.

+2
source

The Runnable implementation does not start a new thread in order to start a new thread, you need to create a new Thread object and start it, and one of the most used Thread constructors takes the parameter Runnable as ( Thread (Runnable) ):

 Thread t = new Thread(new MyRunnable()); t.start(); 
+2
source

Implementing Runnable does not magically make your class work in a thread. Instead, you can do something like:

 Runnable myRunnable = new MyRunnable(); // MyRunnable implements Runnable Thread t = new Thread(myRunnable); t.start(); 

Now your code in the run () method myRunnable will be executed in a separate thread. I highly recommend that you look at ExecutorService and Executors in the java.util.concurrent package. These are the objects that will execute what you are running (runnable) in a thread. The advantage of using Executor / ExecutorService is that you can predefine the number of threads you want to allocate and use different strategies to increase / compress or keep the constant.

Another interesting thing for beginners: Threads can be daemon (background) or non-daemon threads (e.g. UI). Your JVM will not die if there are threads without a daemon. To declare Thread for the daemon, just call setDaemon (). With the executor, you need to provide a ThreadFactory in which you create the thread and mark it as a daemon.

Hope this helps.

+2
source

An interface is a kind of contract. By implementing Runnable, you promise that you will provide all the methods defined in the interface. Therefore, any other class, such as "Thread", that knows about Runnable.run (), can call this method on an object of your class. Not even knowing about his class.

To start a new thread with your code, you need to write something like this:

  Thread thread = new Thread(new MyRunnable()); thread.start(); 

The start () method will use some operating system magic to generate the stream, and then call the run () method in the context of this operating system stream for the object that you provided as a parameter to the constructor.

+1
source

In terms of the latest definitions (spring / DI) there is no "injection". Runnable is no different from any other interface. This is a “contract” that says that your class provides any necessary methods called in the interface.

+1
source

All Articles