Create Thread in Java With Example

How to create a thread in Java?

In this program, we will learn to create thread in java with a source and example.

Aim:

To write a Java program that creates three threads. The first thread displays “Good Morning” after every one second, the second thread displays “Hello” after every two seconds, and the third thread displays “Welcome” after every three seconds

Explanation:

In this, we will see how the threads will work in our project. The first thread displays Good Morning every one second, the second thread displays Hello every two seconds and the third thread displays Welcome every three seconds. The technique used to implement threads here is by extending the Thread class.

While executing a thread, if any interrupt occurs then there arises a java exception. In such cases, to handle these exceptions thread statements are kept in try-catch blocks.

Algorithm To Create Threads:

  1. step: import java.io.*;
  2. step: Create class A and extends Thread
  3. step: create string name.
  4. step: Create class function and pass argument:
    1. A(String tname)
  5. step: name = tname
  6. step: Print name: System.out.print(name)
  7. step: public void run()
  8. step: Again Print (name)
  9. step: Create another class Hellow Thread
  10. step: Create main function
    1. public static void main(String ar[])
  11. step: Create an object: A obj1= new A(“Good Morning”);
  12. step: Within try block create, Thread.sleep(1000); and close try block.
  13. step: Same you can follow. Create an object: A obj2= new A(“Hello”); and within a try block create thread.sleep(1000).
  14. step: End.

Source Code: To Create Thread in java

In the program, we will simply perform to print the message. When you the program please observe the output. It will take 1sec of the gap to print a message and if want to increase the time then you can easily increase the threads time.

import java.io.*;
class A extends Thread
{
String name;
A(String tname)
{
name=tname;
System.out.println(name);
}
public void run()
{
System.out.println(name);
}
}
class HelloThread
{
public static void main(String ar[])
{
A obj1= new A("Good Morning");
try
{
Thread.sleep(1000);
}
catch(Exception ie)
{
System.out.println("Interrupted");
}
A obj2= new A("Hello");
try
{
Thread.sleep(1000);
}
catch(Exception ie)
{
System.out.println("Interrupted");
}
A obj3= new A("Welcome");
try
{
Thread.sleep(1000);
}
catch(Exception ie)
{
System.out.println("Interrupted");
}
}
}

Sample output:

Create Thread in Java

FAQ:

How do you create a thread in Java?

A Thread is created when the Runnable interface is implemented. Thread object should be created and start() method called. The main thread in java will start when the program will execute.

What is thread in Java with example?

In Java thread is nothing but it allows a program to perform more efficiently by doing multiple things at the same time. Threads can be used to complete complex tasks in the background without interrupting the main program.

What is use of thread in Java?

The use of threads in java is to make java applications faster while performing multiple tasks at a time. The thread will help to run multiple programs in a parallel way. Within a program, each of the tasks will run independently.

Recommended Post:

Find the solution to the salesforce Question and many more

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment