Last active
November 24, 2018 20:32
-
-
Save daxroc/1650774e0971465c9e84bd142a5e51c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyRunnable implements Runnable { | |
// Create class properties we can set | |
private String message; | |
// Create Constructor - can customise | |
// what's set when we create a new instance | |
public MyRunnable(String message) { | |
// Set class properties y | |
// when we create the a new instance of the class | |
this.message = message; | |
} | |
// We must implement this function | |
// Because the Runnable interface requires it to exist | |
// Java would error on compile if we didn't | |
public void run() { | |
// Do something in a thread | |
println this.message | |
} | |
} | |
public class RunnableExample { | |
public static void main(String[] args) { | |
// Initialise a variable of Type Runnable | |
// and Instantiate new MyRunnable class instance | |
Runnable r1 = new MyRunnable("I can run"); | |
// Initialise a variable of Type Thread | |
// and Instantiate a new Thread class instance | |
// Passing in our custom runnable | |
Thread t1 = new Thread(r1); | |
// Execute our Thread by calling the start function. | |
t1.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment