Create a gist now

Instantly share code, notes, and snippets.

@jingz8804 /App.java
Last active Aug 29, 2015

What would you like to do?
The ways to create Java threads
package lecture1;
public class App{
public static void main(String[] args){
Runner1 r1 = new Runner1();
Runner2 r2 = new Runner2();
r1.start();
r2.start();
// the third way to create a thread and run it
new Thread(new Runnable(){
public void run(){
System.out.println("Thread started: printing from an anonymous thread");
}
}).start();
}
}
package lecture1;
public Runner1 extends Thread{
public void run(){
// here we write the code that we wish the thread to execute
System.out.println("Thread started: extended from the Thread class");
}
}
package lecture1;
public Runner2 implements Runnable{
@Override
public void run(){
// here we write the code that we wish the thread to execute
System.out.println("Thread started: implemented the Runnable Interface");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment