Last active
November 25, 2015 09:39
-
-
Save rishi93/5771f7fbdc5c7fc9fd35 to your computer and use it in GitHub Desktop.
Threads in Java by extending Thread class
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
import java.io.*; | |
class MyThread extends Thread | |
{ | |
private String threadName; | |
MyThread(String threadName) | |
{ | |
this.threadName = threadName; | |
System.out.println("Creating Thread " + this.threadName); | |
} | |
@Override | |
public void run() | |
{ | |
System.out.println("Running Thread " + this.threadName); | |
try | |
{ | |
Thread.sleep(500); | |
} | |
catch(InterruptedException e) | |
{ | |
e.printStackTrace(); | |
} | |
System.out.println("Finished Thread " + this.threadName); | |
} | |
} | |
class Ideone | |
{ | |
public static void main (String[] args) | |
{ | |
Thread t1,t2; | |
t1 = new MyThread("t1"); | |
t2 = new MyThread("t2"); | |
t1.start(); | |
t2.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment