Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7128231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:07:01+00:00 2026-05-28T11:07:01+00:00

I can suspend the thread with user input in console,but can’t resume it again

  • 0

I can suspend the thread with user input in console,but can’t resume it again with user input.

I want to resume the thread when user gives input “n”. Again it will start with prev state
please help folks.
Thanks.

import java.io.Console;
import java.util.Scanner;

 class NewThread  implements Runnable {

       String name; // name of thread
       Thread t;
       boolean suspendFlag;
       NewThread(String threadname) {
          name = threadname;
          t = new Thread(this, name);
          System.out.println("New thread: " + t);
          suspendFlag = false;
          t.start(); // Start the thread
       }
       // This is the entry point for thread.
       public void run() {

         //  String username = scanner.nextLine();

          try {
          for(int i = 5000; i > 0; i--) {
             System.out.println(name + ": " + i);
             Thread.sleep(200);

                //String username = scanner.nextLine();
//              if(username.equals("y"))
//              {
//                  mysuspend(); 
//              }

             synchronized(this) {
                while(suspendFlag) {

                   wait();

                }
              }
            }
          } catch (InterruptedException e) {
             System.out.println(name + " interrupted.");
          }
          System.out.println(name + " exiting.");
       }
       void mysuspend() {
          suspendFlag = true;
       }
       synchronized void myresume() {
          suspendFlag = false;
           notify();
       }
    }

    class SuspendResume {
       public static void main(String args[]) {


            NewThread ob2 = new NewThread("One");
            // System.out.println(username);
          try { Scanner scanner = new Scanner(System.in);
          String username = scanner.nextLine();
          if(username.equals("y"))
                {
                    Thread.interrupted();

            // Thread.sleep(10000);
             ob2.mysuspend();
             System.out.println("Suspending thread One");
             Thread.sleep(10000);

            // ob2.myresume();
             System.out.println("Resuming thread One");
                }
          if(username.equals("n"))
            {

              ob2.myresume();
            }
          } catch (InterruptedException e) {
             System.out.println("Main thread Interrupted");
          }
          // wait for threads to finish
          try {
             System.out.println("Waiting for threads to finish.");
             ob2.t.join();

          } catch (InterruptedException e) {
             System.out.println("Main thread Interrupted");
          }
          System.out.println("Main thread exiting.");
       }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:07:01+00:00Added an answer on May 28, 2026 at 11:07 am

    Your control flow is wrong.

    You are reading input only once. If it is equal to y you are suspending the thread, but then you never go into the if statement that tests for n to resume the thread. I would do it like this:

    public class Test {
        public static void main(String args[]) {
    
            NewThread ob2 = new NewThread("One");
            Scanner scanner = new Scanner(System.in);
            while(true) {
                String username = scanner.nextLine();
                if (username.equals("y")) {
                    Thread.interrupted();
    
                    // Thread.sleep(10000);
                    ob2.mysuspend();
                    System.out.println("Suspending thread One");                    
    
                }
                if (username.equals("n")) {
                    ob2.myresume();
                    System.out.println("Resuming thread One");
                }
                if (username.equals("q")) {
                    ob2.myresume();
                    break;          
                }
            }
            // wait for threads to finish
            try {
                System.out.println("Waiting for threads to finish.");
                ob2.t.join();
    
            } catch (InterruptedException e) {
                System.out.println("Main thread Interrupted");
            }
            System.out.println("Main thread exiting.");
        }
    }
    

    If the user enters “y” the thread will be suspended. If they enter “n” it will be resumed and if they enter “q” you will just break the loop and wait for the thread to finish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to suspend and resume a thread user input through the command prompt? I
I can suspend a thread of another process by using SuspendThread(). Is there any
I have a thread doing things in background and I want to suspend it
Can I get a threads suspend count under Windows CE, using C or Visual
In Win32, you can create a thread in suspended mode, by using the dwCreationFlags
In the Message Loop of a windows application,the GetMessage() function will suspend the application
In my application I want to suspend a button event for some time so
Threads in .NET are paused during GC. How can a thread be paused safely
What is the difference between suspend and suspendAll in SCORM 2004 3rd ED? Can
How can I suspend/close my iPad app programmatically? I wanted to show the terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.