Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active November 25, 2015 09:39
Show Gist options
  • Save rishi93/5771f7fbdc5c7fc9fd35 to your computer and use it in GitHub Desktop.
Save rishi93/5771f7fbdc5c7fc9fd35 to your computer and use it in GitHub Desktop.
Threads in Java by extending Thread class
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