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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:19:14+00:00 2026-06-13T16:19:14+00:00

I need to perform 2 tasks independently. First Task Once per minute it should

  • 0

I need to perform 2 tasks independently.

First Task

Once per minute it should check whether there is any file in a specific folder. If there is, it should add the names of the files to a queue.

This can be done as follows:

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class schedulerExample extends Thread{

    public void checkFile()
    {
        System.out.println("checking whether file exist in folder");

    }

    public void getFiles()
    {
        System.out.println("getting the file names");
    }

    public void enqueueFiles()
    {
        System.out.println("add files to queue");
    }
    public static void main(String[] args) {

    final schedulerExample obj = new schedulerExample();
    ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(1);
    executor.scheduleAtFixedRate(new Runnable() {

        @Override
        public void run() {

            obj.checkFile();
            obj.getFiles();
            obj.enqueueFiles();

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


    }

}

Second Task

If the queue is empty , sleep for one minute or else process the file from the queue 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);
                    if(size == 0)
                    {
                        try
                        {
                        Thread.sleep(60000);
                        }
                        catch(InterruptedException e)
                        {

                        }
                    }
                }

            }

        };

        t1.start();

    }

}

Now I need to do both in a single class concurrently. Is that possible?

One thread should be fetching the files once per minute. Another thread should execute files one-by-one.. if no files are there it waits for a minute and checks again. In the second method I have used an infinite loop — instead of that, is there a way that I can perform things one-by-one?

  • 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-13T16:19:16+00:00Added an answer on June 13, 2026 at 4:19 pm

    Synchronize on the queue object when you get file from it and when you add file to it.

    In the thread that reads, call wait() if the queue is empty.
    In the thread that checks for new files, call notify() after you added the new file to the queue.

    This is how it’s usually done.


    You should also prevent adding file that is being processed to the queue.

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

Sidebar

Related Questions

I need to perform some tasks with google map, 1. first I need the
I need to perform the same task on all items in Outlook 2007's Tasks
Very simple question. I need to perform a task when the user closes his
I have a task I need to perform, do_stuff(opts) , that will take ~1s
I have a jQuery function already to perform the task I need but is
I need to perform some operations on all folders within a file share which
For an upcoming project with node.js I need to perform various housekeeping tasks at
Let say your website need to call Twitter API to perform some tasks, you
I need to perform some tasks on object1 when some state change happens on
Sometimes we need to perform small administrative tasks in SharePoint. A simple PowerShell script

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.