Created
February 23, 2016 10:36
-
-
Save SijmenHuizenga/3242255ff31038fc12c0 to your computer and use it in GitHub Desktop.
Theading Example
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
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(); | |
} | |
} |
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
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