Java Interfaces and Threads for Right Hand Thinkers

How can a java interface gain you access to methods in classes that implement the same interface?
How can a Thread use a start() method that is nowhere to be seen (its hiding in Thread).

To solve these convoluted issues I have written a Thready class that is similar to java.lang.Thread and a Runnabley interface that is similar to java.lang.Runnable but just 4 or 5 lines so that you can then see exactly how its all working. Its hard to explain in words without being an annoying geek.

So, here’s some code to look at and run at home.
Thread becomes Thready and Runnable becomes Runnabley. The users class is called FakeThread:

************************************************
package com.thready;

public class FakeThread implements Runnabley {

public static void main(String[] args){

// or Runnabley runnable = new FakeThread();
FakeThread runnable = new FakeThread();
Thready thready = new Thready(runnable);

// or Thready thready = new Thready(new FakeThread());
thready.start();
System.out.println(” running thread “+thready.getName());

}
public void run(){
System.out.println(” running dummy thread from FakeThread “);
}
}
********************************************************

Here is a fake Thready class (similar to the real Thread class)
******************************************
package com.thready;

public class Thready implements Runnabley {

private Runnabley target;

public Thready(Runnabley target){
this.target = target;
}

public void run() {
if (target != null) {
target.run();
}
System.out.println(” from run() in Thready but in this case NOT a real thread but a dummy one “);
}
public void start(){
System.out.println(” from start() in Thready class “);
run();
}
public final String getName() {
return “FredTheThread”;
}
}
*********************************

Here is Runnabley, similar to java.lang.Runnable:
**************************
package com.thready;

public interface Runnabley {

public abstract void run();

}
***********************************

If you want to do a real Thread and Runnable, then here is your tester class:
***********************************
package com.thready;

public class Test2 implements Runnable {

public static void main(String[] args){

Runnable runnable = new Test2();

Thread thread = new Thread(runnable);

thread.start();
System.out.println(” running thread “+thread.getName());

}

public void run(){
System.out.println(” running thread in run test2 class “);
}

}
***********************************************************

If you like words here are some: If your class implements an interface, then when you make an object it automatically becomes an object of the interface as well. Then all classes that implement the same interface can use each other’s methods if they want. See how we make a thread object that takes an interface in its constructor, pass it our class object and then we get access to methods in Thread. Run() in Thread runs because something in Thread tells it to run, – target.run() – not because it is overwritten.

Its best to play around with the code I have given you answering the questions:
1) How can run() in your class be called?
2) How is start() in Thread called?
3) If Thread had no constructor that took a Runnable, how would things change.

Like I said it IS convoluted, so all the best.

Answer to 3)
The class thread needs to call run() in our users class. Therefore it needs to know
the name of our user classes object so it can call it. That object gets into Thread via its constructor.
(I repeat its not overwriting the run() method, its actually being called form Thread.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s