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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:28:20+00:00 2026-06-02T06:28:20+00:00

I’m a newbie in Java and I haven’t work with threads in past. Now

  • 0

I’m a newbie in Java and I haven’t work with threads in past. Now I have a really tricky issue for my level of knowledge.

I have developed two functionalities the “Send mail” and the “Check mail”. Each user register his preferences and the system creates two catalogues with many instructions for periodic work like

A) send message every 1500 msec

B) send message every 1800 msec

C) send message every 3000 msec

And
A) check message every 2000 msec

B) check message every 6000 msec

C) check message every 8000 msec

I tried many ways with threads but I fail to do it work.
My question is which is the best way to synchronize them? Below is code from my last effort using threads.

public class MailCreatorThread extends Thread {
    @Override
    public void run() {
        CreateMail(_date); //creates a mail with _date as subject
    }
}

public class GPSThread extends Thread {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (!_isTimeToStop) {
            try {
                while (_servicesToUpdate.size() == 0) {
                    Thread.sleep(500);
                }
                _currentService = (MyService) _servicesToUpdate.get(0)
                        .clone();
                _servicesToUpdate.remove(0);

                        MailCreatorThread mailCreatorThread = new MailCreatorThread();
                        mailCreatorThread.start();


            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

public class CheckServicesThread extends Thread {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (!_isTimeToStop) {
            try {
                Thread.sleep(1000);
                    for (int j = 0; j < _servicesList.length; j++) {
                        MyService currentService = ((MyService) _servicesList[j]);
                        if (myService.getTimeToNextUpdate() - 1000 <= 0) {
                            _servicesToUpdate
                                    .add((MyService) currentService
                                            .clone());
                            currentService
                                    .setTimeToNextUpdate(currentService
                                            .getUpdatePeriod());
                        } else {
                            currentService
                                    .setTimeToNextUpdate(currentService
                                            .getTimeToNextUpdate() - 1000);
                        }
                }

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        stopSelf();
    }
}
  • 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-02T06:28:21+00:00Added an answer on June 2, 2026 at 6:28 am

    You can use a ScheduledExecutorService to run thos periodic tasks. The syntax is fairly straightforward:

    Runnable check = new Runnable() {
        public void run() {
            checkMessage();
        }
    }
    Runnable send = new Runnable() {
        public void run() {
            sendMessage();
        }
    }
    
    //since what you are doing is mostly I/O you probably want to have 
    //more than one thread available so that if one operation blocks,
    //the other ones can be launched in parallel
    
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
    scheduler.scheduleAtFixedRate(check, 0, 1500, MILLISECONDS);
    scheduler.scheduleAtFixedRate(send, 0, 6000, MILLISECONDS);
    

    Note: Timer mentioned in Ozzy’s answer should not be used any more as it has been improved in Java 1.5 by ScheduledThreadPoolExecutor, as explained in Timer’s javadoc :

    Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn’t require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.