Skip to content

Instantly share code, notes, and snippets.

@SijmenHuizenga
Created February 23, 2016 10:36
Show Gist options
  • Save SijmenHuizenga/3242255ff31038fc12c0 to your computer and use it in GitHub Desktop.
Save SijmenHuizenga/3242255ff31038fc12c0 to your computer and use it in GitHub Desktop.
Theading Example
package threading.test;
public class App implements Runnable{
public App() {
Thread newThread = new Thread(this);
//als het programma stopt, stop ook deze thread
newThread.setDaemon(true);
//start de nieuwe thread
newThread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
//hier begint de nieuwe thread
while(true){
System.out.println("Hallo van thread");
//slaap voor 500 ms:
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main( String[] args ) {
new App();
}
}
package threading.test;
public class App2 extends Thread{
public App2() {
}
@Override
public void run() {
//hier begint de nieuwe thread
for(int i = 0; i < 6; i++){
System.out.println("Hallo van app2");
//slaap voor 500 ms:
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//maak een nieuwe App2 aan
App2 app2 = new App2();
app2.setDaemon(true);
app2.start();
try {
app2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment