Java thread Programming - Create a Basic Thread to Print "Hello, World!"
Java Thread: Exercise-1 with Solution
Write a Java program to create a basic Java thread that prints "Hello, World!" when executed.
Sample Solution:
Java Code:
public class Hello_world_thread extends Thread {
@Override
public void run() {
System.out.println("Hello, World!");
}
public static void main(String[] args) {
Hello_world_thread thread = new Hello_world_thread();
thread.start();
}
}
Sample Output:
Hello, World!
Explanation:
In the above exercise,
In the main method, an instance of the Hello_world_thread class is created, and the start() method is called on that instance. This starts the thread's execution, which invokes the over ridden run() method.
When the program is executed, it creates another thread and runs it, causing "Hello, World!" to be printed to the console.
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Java Thread Exercises Home.
Next: Find and Print Even-Odd Numbers with Threads.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/thread/java-thread-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics