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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:24:46+00:00 2026-05-27T19:24:46+00:00

I’m reviewing Java code that essentially is a recurring process that moves/reads/parses some files

  • 0

I’m reviewing Java code that essentially is a recurring process that moves/reads/parses some files on regular basis and outputs data into the database. The repeating part is done (roughly) as follows:

public class CollectorMain {
    public static boolean signalRecieved = false;
    public static void main(String[] args) {
         Runtime.getRuntime().addShutdownHook(new Thread() {
              public void run() {  
              shutdown(); 
         }});
         while(!signalRecieved) {
              Collector.execute();
              try {
                  Thread.sleep(60 * 1000);
              } catch (InterruptedException e) {
                  break;
              }
         }
         // some shutdown logic
    }
    public static void shutdown() {
        signalReceived = true;
    }
}

public class Collector() {
    public static void execute() {
        // Move files from the queue dir to temp location
        // Read, parse files and insert records into database. 
        // Then delete the processed files
    }
}

My recommendation was to refactor code to

  1. Create instance of Collector and refactor static execute() method to the instance method
  2. To use Runnable or TimerTask to handle invocations

My argument was that using Thread.wait from the main method and combining it with static access is not a good way of handling repeatable process especially doing file IO. To which the author replied (quoting)

The description of Runnable says “should be implemented by any class
whose instances are intended to be executed by a thread”. In fact, I
am intentionally avoiding threads in this program, for reasons of cost
vrs.performance requirements.

Here’s another quote from same discussion which will hopefully help to clarify the author’s position

Technically, Java isn’t executed at all, it is interpreted by the JVM
which then executes machine instructions, to simulate that the Java
code is executing. So it’s really the JVM that is executing on a
thread, or multiple threads.

But as a Java code writer, I don’t care. If I don’t create “threads”
in Java, then It’s the job of the JVM to appear as if there are no
threads — even if the JVM is using threads “under the covers”.

A Java Pause is not executed, it is simulated by a sequence of machine
instructions that may or may not call an OS ‘wait’. (It probably does,
because the JVM would not want to spin, burning CPU cycles, but that’s
a JVM implementation choice).

So I have 2 questions:

  1. Is putting Thread.wait into the main method legit, safe and advisable way of doing repeatable task in this instance? And if not, why not since there’s only one (main) thread of execution?
  2. What are the pitfals of using static metods in this context (if any)?

I’ll be happy to provide additional info if you have any other questions.

  • 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-27T19:24:47+00:00Added an answer on May 27, 2026 at 7:24 pm

    You’re really arguing about design decisions, not performance decisions.

    Your colleague’s statement about how Thread.sleep is implemented is basically incorrect as far as I can see. On a sensible JVM on a sensible operating system, Thread.sleep() is implemented using an O/S native method for parking a thread (or putting it in a “timed wait” state or whatever you want to call it on your OS). Or put another way, while a thread is sleeping, it consumes zero CPU. And in any case, TimerTask will use Thread.sleep (or similar– I don’t just recall if it uses the park() method introduced in Java 5, but it essentially makes no difference).

    The JVM won’t generally make secret under-the-hood decisions about threading. If you ask for another thread, you’ll get one; if you don’t, you won’t. A few “housekeeping” threads will be created for garbage collection etc, but as far as your code is concerned, you can assume that no secret clever thread creation is going on.

    So, coming back to your questions:

    • sleeping in the main thread is perfectly safe per se; obviously, while the main thread is sleeping, it won’t be executing anything, but if you don’t need it to, then that’s fine;
    • whether you use Thread.sleep() directly in your code or use one of the “timer” utility methods is again a design decision based on whether you want to use a general library that removes implementational details from your code, or whether you prefer to have those details present and under your control; performance-wise, it will make little odds if you implement things correctly;
    • whether you have a static method or have an instance of Collector doesn’t essentially matter– it’s just a design decision that you need to make based on what seems more intuitive: do you see Collector as a “general utility” class, or as a “thing” with state that you request to perform operations?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT

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.