[Link] a Java Program to Illustrate Thread Example for currentThread().
Input-
public class CurrentThreadExp extends Thread
{
public void run()
{
[Link]([Link]().getName());
}
public static void main(String args[])
{
CurrentThreadExp t1=new CurrentThreadExp();
CurrentThreadExp t2=new CurrentThreadExp();
[Link]();
[Link]();
}
}
Out put-
Thread-0
Thread-1
[Link] a Java Program to Illustrate Thread Example for setName(string
name).
Input
public class SetNameExample extends Thread
{
public void run()
{
[Link]("running...");
}
public static void main(String args[])
{
SetNameExample t1=new SetNameExample();
SetNameExample t2=new SetNameExample();
[Link]();
[Link]();
[Link]("sourav");
[Link]("pallab");
[Link]("After changing name of t1: "+[Link]());
[Link]("After changing name of t2: "+[Link]());
}
}
Output-
After changing name of t1: sourav
running...
running...
After changing name of t2: pallab
[Link] a Java Program to Create a Thread that Implement the
Runnable Interface.
Input
public class Run {
public static void main(String[] args)
{
[Link]("Main thread is- "
+
[Link]().getName());
Thread t1 = new Thread(new RunnableDemo().new
RunnableImpl());
[Link]();
}
private class RunnableImpl implements Runnable {
public void run()
{
[Link]([Link]().getName()
+ ", executing run() method!");
}
}
}
Out put
Main thread is- main
Thread-0, executing run() method!
[Link] a Java Program Defining Thread By Extending Thread.
Input
class Multi extends Thread{
public void run(){
[Link]("thread is running...");
public static void main(String args[]){
Multi t1=new Multi();
[Link]();
}
Output
thread is running...