Created
January 28, 2013 03:48
-
-
Save stangirala/4652874 to your computer and use it in GitHub Desktop.
A random thread example.
This file contains hidden or 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
| public class AsyncHelloWorld { | |
| public static void main(String[] args) { | |
| Thread InterfaceExample = new Thread(new PrintString("Thread 1")); | |
| Thread ThreadExample = new PrintStringThread("Thread 2"); | |
| InterfaceExample.start(); | |
| ThreadExample.start(); | |
| try { | |
| ThreadExample.join(); | |
| } catch (InterruptedException e) {e.printStackTrace();} | |
| } | |
| } | |
| class PrintString implements Runnable { | |
| private String Name; | |
| public PrintString(String name) { | |
| Name = name; | |
| } | |
| public void run() { | |
| try { | |
| System.out.println(Name + " Sleeping."); | |
| Thread.sleep(4000); | |
| System.out.println(Name + " Done Sleeping."); | |
| } catch (InterruptedException e) { | |
| //e.printStackTrace(); | |
| // Interrupted | |
| System.out.println("Thread interuppted."); | |
| return; | |
| } | |
| System.out.println(Name + " Hello, World!"); | |
| } | |
| } | |
| class PrintStringThread extends Thread { | |
| private String Name; | |
| public PrintStringThread(String name) { | |
| Name = name; | |
| } | |
| public void run() { | |
| System.out.println(Name + " Sleeping."); | |
| try { | |
| Thread.sleep(4000); | |
| } catch (InterruptedException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| System.out.println(Name + " Done Sleeping."); | |
| System.out.println(Name + " Hello, World!"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment