Skip to content

Instantly share code, notes, and snippets.

@akirad
Last active December 17, 2015 18:48
Show Gist options
  • Save akirad/5655394 to your computer and use it in GitHub Desktop.
Save akirad/5655394 to your computer and use it in GitHub Desktop.
A thread test sample for java. It shows the differece between runnable.run() and thread.start(). This code is originally from a J2EE community. I modified it for me.
public class ThreadTest {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Start!");
Runnable r1 = new Runnable() {
public void run() {
try {
for (int i=0; i<100; i++) {
System.out.println("Runnable1" + ": " + i);
Thread.sleep(100);
}
} catch (Exception ex){
System.out.println(ex.getStackTrace());
}
}
};
Runnable r2 = new Runnable() {
public void run() {
try {
for (int i=0; i<100; i++) {
System.out.println("Runnable2" + ": " + i);
Thread.sleep(100);
}
} catch (InterruptedException ex) {
System.out.println(ex.getStackTrace());
}
}
};
//-----------------------------------------------
// After r1 finished completely, r2 is executed.
//-----------------------------------------------
//r1.run();
//r2.run();
//-----------------------------------------------
// Both r1 and r2 are executed by turns.
//-----------------------------------------------
//Thread thr1 = new Thread(r1);
//Thread thr2 = new Thread(r2);
//thr1.start();
//thr2.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment