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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:05:45+00:00 2026-05-31T05:05:45+00:00

public final void sendAdvertisement(final Advertisement advertisment, int delay, final int repetitions){ final ScheduledFuture exec

  • 0
public final void sendAdvertisement(final Advertisement advertisment, int delay, final int repetitions){
    final ScheduledFuture exec = executor.scheduleAtFixedRate( //<< initialized on this line
        new Runnable(){
            int totalSends = 0;
            public void run(){
                //do stuff here

                if(++totalSends >= repetitions) exec.cancel(true); //<< here is says exec might not be initialized
            }
        },
    0, delay, TimeUnit.MILLISECONDS);
}

If this isn’t possible, could you suggest a better way to do this? I couldn’t find a method for it in ScheduledThreadPoolExecutor. basically what I’m trying to do is make this code be run 3 times then cancel the “timer.” I could probably use Swing Timer, but I don’t want to since its used for other stuff.

It says in the comments, but this is the error:

%PATH%Discovery.java:148: variable exec might not have been initialized
                if(++totalSends >= repetitions) exec.cancel(true);
  • 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-31T05:05:47+00:00Added an answer on May 31, 2026 at 5:05 am

    Your code may work from the standpoint of removing the compiler warning but the whole point of the warning is pointing out that you may be accessing a variable that has not been assigned yet. Even if exec or exec[0] is non-null there is also no guarantee that the ScheduledFuture object has even been properly initialized — yes even though the inner thread may be running. This is very dangerous and might work for a while but then fail dramatically in production, when you move to an architecture with more cores, or under different load circumstances. It also may work but then you change your do stuff here code a month from now and it starts to fail.

    I see a couple of ways that you can accomplish this in a better manner. They are more complicated but also more safe and consistent with Java. The first that comes to mind is by using the AtomicReference:

    // this class handles atomic updates and memory synchronization
    final AtomicReference<ScheduledFuture> futureReference =
        new AtomicReference<ScheduledFuture>();
    ScheduledFuture exec = executor.scheduleAtFixedRate(
        new Runnable() {
            int totalSends = 0;
            public void run() {
                //do stuff here
                if (++totalSends >= repetitions) {
                    // we need to wait for the future to be initialized
                    while (true) {
                        ScheduledFuture future = futureReference.get();
                        if (future != null) {
                            future.cancel(true);
                            break;
                        }
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                            Thread.currentThread.().interrupt();
                        }
                    }
                }
            }
        },
        0, delay, TimeUnit.MILLISECONDS);
    // this sets the future reference so the thread can use it
    futureReference.set(exec);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code is invalid: interface Foo { public void foo(final String string); } public
considering this example: public static void main(final String[] args) { final List<String> myList =
Javadoc says: void android.app.Activity.showDialog(int id) public final void showDialog (int id) Since: API Level
For example: public void doSomething() { final double MIN_INTEREST = 0.0; // ... }
Consider an object declared in a method: public void foo() { final Object obj
I have the following code: public void post(String message) { final String mess =
Whi with this code public final String[] array_titoli_serie_prima = new String[]{ getResources().getString(R.string.titolo_p1), getResources().getString(R.string.titolo_p2), getResources().getString(R.string.titolo_p3),
I read that doing: public final void foo() {} is equals to: private static
In the documentation for the Parcel it states a method exists public final void
I have a method in engine i'm using (andengine) : public final void setText(String

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.