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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:03:08+00:00 2026-06-03T20:03:08+00:00

I want to write an tail like app. Now this app, scans a file

  • 0

I want to write an tail like app. Now this app, scans a file for changes in the background and fires events in case something changed.

I want to run my application until the user requests to exit it by pressing ctrl + c (working by default). I do not want to create a lot of CPU load just by doing an endless while(true) loop like I’m doing ATM:

try {
        // thread doing the monitoring
        thread.start();

        // forcing the programm not to exit
        while (System.in.available() == 0) {
            Thread.sleep(5000);
        }

    } catch (final IOException e) {
        e.printStackTrace();
    }

Does anyone know a more elegant/the right approach for this?

  • 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-03T20:03:10+00:00Added an answer on June 3, 2026 at 8:03 pm

    I’m not sure why you are using a thread in the first place, the ‘tail’ unix script is simply a while(true) loop with no exit condition. It watches the file and prints changes if any is detected. I think by default it pauses 1 second, so basically:

    while(true) {
       // Code goes here. Watch file, System.out.print if it changes
    
       Thread.sleep(1000);
    }
    

    No need for a thread. But to answer the question about the best way to keep your app alive: Simply don’t return from the thread’s run() method. You don’t have to join the thread, the application will stay in the foreground as long as it has one non-daemon running thread.

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

Sidebar

Related Questions

I want to write some large amount of data to a file. the data
I use trap to write errors to file, and want write line number where
I want to write a c program that keeps reading a file and whenever
I want write this sql select statment in mysql v 3.23 select * from
I need to write something into a text file's beginning. I have a text
I wanted to see the impact of tail recursion optimization and want to write
I am using spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile for file uploading. i want write down unit
I want write a little code analyzer which parses nested structures and translates into
Hello I want write my own desktop sharing application in Java. The application should
I want to write a row in a db every time a user logs

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.