June 2018
Beginner
722 pages
18h 47m
English
Implementing Runnable has the advantage (and in some cases, the only possible option) of allowing the implementation to extend another class. It is particularly helpful when you would like to add thread-like behavior to an existing class:
public class BRunnable extends SomeClass implements Runnable { int i; BRunnable(int i, String s) { super(s); this.i = i; } public int calculateSomething(double x) { //calculate result return result; } public void run() { //any code you need goes here }}
You can even invoke the method run() directly, without passing the object into the Thread constructor:
BRunnable obj = new BRunnable(2, "whatever");int i = obj.calculateSomething(42d);obj.run(); Thread thr = new ...
Read now
Unlock full access