0% found this document useful (0 votes)
4 views3 pages

Java Thread Examples and Implementations

The document provides four Java programs demonstrating different aspects of threading. The first program illustrates the use of `currentThread()` to print the name of the currently executing thread. The second program shows how to set and retrieve thread names, while the third program implements the `Runnable` interface, and the fourth defines a thread by extending the `Thread` class, each with their respective outputs.

Uploaded by

my server
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Java Thread Examples and Implementations

The document provides four Java programs demonstrating different aspects of threading. The first program illustrates the use of `currentThread()` to print the name of the currently executing thread. The second program shows how to set and retrieve thread names, while the third program implements the `Runnable` interface, and the fourth defines a thread by extending the `Thread` class, each with their respective outputs.

Uploaded by

my server
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[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...

You might also like