Java Multithreading - One Thread to Take Input, Another to Print on Console Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Multithreading is a concept in which our program can do two or more tasks at the same time. Thread is the execution unit of any process. Every process has at least one thread which is called main thread. In this article, we will create a Java program that will do printing on the console until the user gives input. Here, we need two threads. One thread do printing and another waits for user input. Java Program and Explanation Here, one thread is printing on the console and while the other thread will wait for user input. Main class is executed on the parent thread and also it creates a child thread that does printing on the console. Tasks of Parent thread: Create a new thread. (child thread)Waits for user input. Tasks of child thread: Do printing. Java // fileName - GeeksForGeeks.java import java.lang.*; // needs to be inherited from // Thread class for multithreading public class GeeksForGeeks extends Thread { private String mssg; // constructor GeeksForGeeks(String mssg) { this.mssg = mssg; } // invoked when we // call start() method public void run() { // prints only when start variable // is true (variable present in Main class) while (Main.start == true) { System.out.println(this.mssg); try { // put thread on waiting // state for 1500ms Thread.sleep(1500); } catch (Exception err) { } } } } Explanation of the above code: We extend our class to Thread class to make our program multithreaded.its run() method is invoked internally whenever we invoke the start() method.run() methods do printing until the start variable value is true. Java // fileName - Main.java import java.lang.*; import java.util.Scanner; public class Main { // public static variable and it's // value will be false when user enter input public static boolean start = true; public static void main(String args[]) { GeeksForGeeks newThread = new GeeksForGeeks("I Love GFG!"); // child thread execution // starts here newThread.start(); Scanner sc = new Scanner(System.in); // parent thread waiting for user input here // parent thread is at waiting state at this line sc.nextLine(); // when user gave any input then the value // of this variable changed to be false // it will tells the child thread that // user gave input and now stop printing Main.start = false; } } Explanation of the above code: It creates an object of GeeksForGeeks Class which extends the Thread class.After this, it invokes the start() method which executes all the code of the GeeksForGeeks run() method on a different thread. After that, It waits for user input. Output: printing and waiting for user input at the same timeprinting and waiting for user input at the same timeBoth classes in the same Java file: Java /*package whatever // do not write package name here */ // This program may not work on at gfg IDE // run it on your local system import java.io.*; import java.lang.*; import java.util.Scanner; // needs to be inherited from Thread // class for multithreading class GeeksForGeeks extends Thread { private String mssg; GeeksForGeeks(String mssg) { this.mssg = mssg; } // invoked when we // call start() method public void run() { // prints only when start variable // is true (variable present in Main class) while (Main.start == true) { System.out.println(this.mssg); try { // put thread on waiting // state for 1000ms Thread.sleep(1000); } catch (Exception err) { } } } } public class Main { // public static variable and it's value // will be false when user enter input public static boolean start = true; public static void main(String[] args) { GeeksForGeeks newThread = new GeeksForGeeks("Manish Loves GFG!"); // child thread execution // starts here newThread.start(); Scanner sc = new Scanner(System.in); // parent thread waiting for user input here // parent thread is at waiting state at this line sc.nextLine(); // when user gave any input then the value // of this variable changed to be false // it will tells the child thread that // user gave input and now stop printing Main.start = false; } } Output: printing and waiting for user input at the same timeprinting and waiting for user input at the same time Comment More infoAdvertise with us Next Article How to Get the Id of a Current Running Thread in Java? E elite_programmer Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2022 Java-Multithreading +1 More Practice Tags : Java Similar Reads How to Find Prime and Palindrome Numbers using Multi-Threading in Java? Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java. Each thread runs parallel to each other. Multiple threads don't 4 min read Java Multithreading Program with Example Multithreading is a concept in which our program can do multiple tasks in a single unit of time. Thread is the execution unit of any process. Every process must have one thread and that thread name is the main thread. In this article. We will create a Java program that will do writing on file until 4 min read How to Monitor a Thread's Status in Java? The Java language support thread synchronization through the use of monitors. A monitor is associated with a specific data item and functions as a lock on that data. When a thread holds the monitor for some data item, other threads are locked out and cannot inspect or modify the data. In order to mo 3 min read Print even and odd numbers in increasing order using two threads in Java Given an integer N, the task is to write Java Program to print the first N natural numbers in increasing order using two threads.Prerequisite: MultithreadingExamples:Input: N = 10Output: 1 2 3 4 5 6 7 8 9 10Input: N = 18Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18Approach: The idea is to cre 3 min read How to Get the Id of a Current Running Thread in Java? The getId() method of Thread class returns the identifier of the invoked thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused. Java allows c 4 min read Difference Between Running and Runnable States of a Thread in Java Thread is the backbone of multithreading in java. Multithreading is a feature that allows concurrent execution of two or more parts of the program for the maximum utilization of CPU. Each part of such a program is called a thread. So threads are light-weighted processes within a process. A thread ca 5 min read Like