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 8645429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:32:59+00:00 2026-06-12T12:32:59+00:00

The following snippet is a thread named Foo that sleeps for 1 minute and

  • 0

The following snippet is a thread named “Foo” that sleeps for 1 minute and then copies the data typed in 1 minute to a log file.

         while(isStarted) {
           try {
              Thread.sleep(60000); // sleep for 1 minute
              ArrayList<String> keyStrokeList = nativeMethods.getKeyStrokeList();
              int result = copy.copyToLogFile(keyStrokeList);
              System.out.println(result);
           } catch(Exception exc) {
               exc.printStackTrace();
             }
         }

I will describe one situation :

Foo thread has finished copying all the data typed in last one minute and it has been 30 seconds since it is asleep. This thread unaware of the situation that several keys are being tapped when it is asleep,will never be able to copy the key strokes into the log file when one presses System.exit(0).

Is there any way I can interrupt this thread i.e awake it and ask it to copy the data to the log file.

Please discuss how should I approach this problem.

The situation in the question :

loop started

thread is sleeping and will sleep for 1 minute

after a minute,it gets the keys tapped in the last 1 minute and copies all that
to a file

Thread sleeps again..and will sleep for 1 minute before it copies the keystrokes

It has been about 30 seconds and thread will sleep for 30 seconds more before it starts
copying the key strokes

suddenly the user presses exit button in the application

The user wants that key strokes be recorded till the second he presses exit

I cannot do System.exit(0) before checking the thread is asleep or not

How do I do this. Should I awake it or make a different call to the list and get the 
key strokes because they are being recorded ? And how shall I awake it ?
  • 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-06-12T12:33:00+00:00Added an answer on June 12, 2026 at 12:33 pm

    You’re part way there…

    while(isStarted) {
        try {
            Thread.sleep(60000); // sleep for 1 minute
        } catch(InterruptedException exc) {
            exc.printStackTrace();
        }
        ArrayList<String> keyStrokeList = nativeMethods.getKeyStrokeList();
        int result = copy.copyToLogFile(keyStrokeList);
        System.out.println(result);
    }
    

    What you need to is provide a way to terminate the loop…

    public void dispose() {
        isStarted = false;
        interrupt();
        try {
            join();
        } catch(InterruptedException exc) {
            exc.printStackTrace();
        }
    }
    

    You should also know that the JVM will not exit until all non-daemon threads have completed (under normal shutdown). This means you can call System.exit(0) and the JVM will not terminate until the logger thread has terminated.

    You could use this, but attaching a shut down hook which would have the capacity to call the dispose method on the logger thread…just a thought

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

Sidebar

Related Questions

The following HTML snippet submits the file to UploadHandler which is a servlet. Then
The following snippet from RuntimeUtil.java from jlibs guarantees GC that garbage collection is done.
The following snippet, from another thread, works to print a message and fail after
In following code snippet, AddRow() is called from a non-UI thread: public partial class
The following method: private void startServer() { // snippet that starts the server on
When I run the following snippet to connect to datasource to get the data
I have the following code snippet of a small Thread class I am trying
Following snippet attempts to write the name of directories and files present in some
The following snippet is not working for me. Basically I want to check if
I first implemented the following snippet to show a hi alert and do a

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.