Java Multithreading - One Thread to Take Input, Another to Print on Console Last Updated : 28 Apr, 2025 Comments Improve Suggest changes 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 Java Multithreading - One Thread to Take Input, Another to Print on Console 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 How to Copy a File in Multiple Threads Using Java? Copying files is a task, in programming that deals with file manipulation. Although Java offers tools, for file handling copying files can slow down our application's performance. To optimize this process, we can take advantage of multithreading, which enables threads to work on various sections of 3 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 How to Check if a Thread Holds Lock on a Particular Object in Java? Java language is one of the most popular languages for many years. One of the most advantageous features of java programming is Multithreading. Multithreading allows executing a single program into many small parts of the program with the maximum utilization of the CPU. Threads are a lightweight pro 3 min read Like