Java: relation of Runnable and Thread interfaces

I understand that the run() method needs to be declared because it is declared in the Runnable interface. But my question arises when this class works, how is the Thread object allowed if there is no import call to a specific package? how does runnable know anything about Thread or its methods? Does Runnable Interface Extend Thread Class? Obviously, I am not very good at interfaces. thanks in advance.

  class PrimeFinder implements Runnable{ public long target; public long prime; public boolean finished = false; public Thread runner; PrimeFinder(long inTarget){ target = inTarget; if(runner == null){ runner = new Thread(this); runner.start() } } public void run(){ } } 
+4
source share
6 answers

In this situation, I like to think of interfaces as contracts. Saying that your class implements Runnable, you explicitly declare that your class adheres to the Runnable contract. This means that other code can instantiate your class and assign the type Runnable:

 Runnable r = new PrimeFinder(); 

In addition, by adhering to this contract, you guarantee that other code that calls your class may expect to find Runnable methods (in this case, run ()).

+4
source

Nope. The theme is in the lava.lang package, so the implastic is imported.

And: Thread knows Runnable.

This is why Thread gets Runnable ( this implements Runnable) and calls its run () method inside its own execution thread.

The stream uses a link to the Runnable executable:

 public Thread(Runnable runnable) { this.myRunnable = runnable; } private Runnable myRunnable; 

The method for starting the Thread class might look like this:

 public void start() { // do weird stuff to create my own execution and... myRunnable.run(); } 
+3
source

Yes, Thread implements Runnable .

As references to the APIs, the runnable is intended to provide a common protocol for objects that want to execute code while they are active.

You are confused because there are two ways to make this kind of concurrency in Java:

  • you can extend the Thread class overriding the default run method, then call the thread in the same way as new MyThread().start()
  • you can write a class that implements the Runnable interface and starts it in the same way: new Thread(new MyRunnable()).start()

These approaches are IDENTICAL . The infact run method of the Thread class usually calls the run method of the Runnable object, attached to the thread, if any, otherwise it returns.

What is the Runnable interface for? This is useful because it declares a protocol that allows classes with specific characteristics to be considered.

This is the same in the Comparable or Serializable interface, but here you really have a redefinition method ( public void run() ), while Serializable is just a trait that you give to your class.

The final example is the fact that TimerTask implements Runnable . TimerTask used together with the Timer class to perform pending or periodic tasks, so it makes sense that timertask also starts, so Timer can run tasks using this particular method.

EDIT: since you seem confused by the usefulness of the interface, you need to think that: Java is a statically typed language . What does it mean? This means that he must know everything about the type at compile time to ensure that a runtime type error is never thrown.

Ok, now suppose the Java API supports the hipotetically class for drawing shapes. This way you can write your own classes for shapes, and then pass them to this class (call ShapeDrawer on it).

ShapeDrawer needs to know how to draw the shapes you pass, and only to be sure of this, to decide that each Shape object must have a method called public void drawMe() , so that a ShapeDrawer can call this method on every Shape that you attach to it without knowing anything more.

So you declare the interface

 public interface Shape { public void drawMe(); } 

that classes can be considered a Shape . And if the class is Shape , you can pass it to the ShapeDrawer class without any problems:

 class ShapeDrawer { public void addShape(Shape shape) { ... } public void drawShapes() { for (Shape s : shapes) s.drawMe(); } } 

So, the compiler is happy because when you add shapes, you add classes that implements Shape , your class knows exactly how to draw such shapes, and the developers are pleased that you have separated the general protocol of the object from their specific implementations.

This is a kind of contract, if you need a Triangle class that can be drawn by the ShapeManager , you need to declare this method so that you can call, for example,

shapeDrawerInstance.addShape(new Triangle())

+2
source

This has nothing to do with interfaces. Rather, Thread is in the java.lang package, and since java.lang is the default package, it does not need to be imported. That is, java.lang. * Imported by default, so you do not need to explicitly import it yourself.

+1
source

Your code, as it is written, does not compile. You declared PrimeFinder implements Runnable , but it's really not @Override public void run() .

As for why the interfaces are useful, this is because they define types that you can work with regardless of implementation. For example, if you work with Closeable , you know that you can close() it. The actual class can be any of many that implement it ( see the full list ), but working with the interface allows you to Closeable and it's just that they are all all Closeable .

With interfaces, you do not inherit an implementation; you inherit TYPE. If there is other code that works with Closeable types (for example, everything that it does calls close() on them with exception checking), then you can pass something to it that implements Closeable . Actually, this is "please close this thing for me, thanks alright." The utility already exists, and it is very flexible and very reusable, since it works with the interface.

Using interfaces instead of specific implementation classes, you encapsulate your logic much more cleanly. For example, an algorithm that works with the Set interface does not matter if it is a TreeSet or a HashSet . This lack of dependency is good because it allows the algorithm to be very flexible - it can also work with the SuperMagicalSet implements Set in the future, for example.

Working with interfaces also provides proper encapsulation, since without knowing what the actual implementation class will be, you should only work with what the interface provides.

I recommend reading Josh Bloch Effective Java 2nd Edition:

  • Item 18: Prefers Interfaces for Abstract Classes
  • Item 19: Using Interfaces to Define Types
0
source

The Thread class implements Runnable. Runnable knows nothing about Thread, but Thread knows everything about Runnable.

Other classes that use Thread know that Thread implements the interface specified by Runnable.

0
source

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


All Articles