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

  • Home
  • SEARCH
  • 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 8731909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:20:36+00:00 2026-06-13T09:20:36+00:00

I need to create two threads , one invoking a scheduler executor service and

  • 0

I need to create two threads , one invoking a scheduler executor service and the other which runs in an infinite loop to fetch and process files.

I use the following code:

ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(new Runnable() {
        public void run()
        {
            obj.checkFileExist();
            obj.enqueue();

         }
    }, 0, 1, TimeUnit.MINUTES);

And in an infinite loop I process files one by one:

public class processModel extends Thread{

    public static void getQueueSize(int size)
    {
        System.out.println("getting queue size");

    }
    public void dequeue()
    {

        // dequeue the queue
        System.out.println("dequeue");

    }

    public void processFile()
    {
        // process the file
        System.out.println("process file");
    }

    public static void main(String[] args) {
        final boolean flag = true;
        final int size = 9;
        final processModel obj = new processModel();
        Thread t1 = new Thread(){
            public void run()
            {
                while(flag)
                {
                obj.dequeue();
                obj.processFile();
                getQueueSize(size);

                }

            }

        };

        t1.start();

    }

}

How can I achieve both concurrently?

  • Thread 1 – check if file exists in a folder. if yes enqueue it else sleep for a min.. – scheduler can perform this
  • Thread 2 – if queue is non empty , process one by one. else sleep for a minute

How can I run these 2 threads concurrently. It would help me a lot if you show up some code.

  • 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-13T09:20:37+00:00Added an answer on June 13, 2026 at 9:20 am

    Using two threads doesn’t make any sense. The only time both thread will run at once is when the enqueuer is picking up files faster than the dequeuer can process them. You solution would be simpler and more robust if you has a single repeating task which will ensure you never have one thread critically always ahead of the other ( and the code is much simpler)

    ScheduledExecutorService executor = new SingleScheduledThreadPool();
    executor.scheduleAtFixedRate(new Runnable() {
        public void run() {
            for(File file: obj.getCHangedFiles())
                obj.processFile(file);
    
         }
    }, 0, 1, TimeUnit.MINUTES);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create one list based on two other lists. But seems like
I need to create two timers. One is on the UI thread, the other
I need to create a class which calculates the distance between two points. I
I need to create a row in XAML which has a label,two radio buttons..
I have several threads which need to write to two different text files. So
So I'm doing this project for my OO class. We need to create two
If I want to do bi-directional mapping, do I need to create two mapping?
I have the following three arrays and need to create a new two-dimensional array
I need to create a class with two properties: LogOutput ExceptionOutput These properties (Actions<>)
I have two objects I need to create a relationship between in Core Data.

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.