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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:11:54+00:00 2026-06-07T04:11:54+00:00

I am trying to implement a ScheduledExecutorService thread that loops every second, but as

  • 0

I am trying to implement a ScheduledExecutorService thread that loops every second, but as of right now it only loops once.

My question is how do I set it up so that it loops periodically instead of one iteration?

Also,how do I pass the connection pool into the thread so that I can query a database every iteration? Any help is much appreciated.

public static void main(String[] args) throws InterruptedException {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    AdminManager frame = new AdminManager();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        BoneCP connectionPool = null;
        Connection connection = null;

        try {
            // load the database driver (make sure this is in your classpath!)
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        try {
            // setup the connection pool
            BoneCPConfig config = new BoneCPConfig();
            config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/database"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
            config.setUsername("root"); 
            config.setPassword("");
            connectionPool = new BoneCP(config); // setup the connection pool

            connection = connectionPool.getConnection(); // fetch a connection

            if (connection != null){
                System.out.println("Connection successful!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
        exec.schedule(new Runnable(){
            @Override
            public void run(){
                System.out.println("Working ... ");

            }
        }, 1, TimeUnit.SECONDS);

        //connectionPool.shutdown(); // shutdown connection pool.
}
  • 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-07T04:11:55+00:00Added an answer on June 7, 2026 at 4:11 am

    http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html

    there’s a scheduleAtFixedRate method.
    To pass something in to a anonymous class it needs to be declared final.
    And it needs to be in the same scope.

    Also the code you have now is closing the connection, if you intend on passing it to another thread you need to keep it open.

    !Edit some sample code

    public class Whatever {
        public static void main(String[] args) throws Exception {
            // ... do your frame thing
    
            loadDataBaseDriver();
            BoneCP connectionPool = createConnectionPool();
    
            try {
                final Connection connection = connectionPool.getConnection();
                ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
    
                exec.scheduleAtFixedRate(new Runnable(){
                    @Override
                    public void run(){
                        System.out.println("Working ... ");
    
                        // use connection
                    }
                }, 0, 1, TimeUnit.SECONDS);
            } catch (SQLException e) {
              // do whatever
            }
        }
    
        public static BoneCP createConnectionPool() throws SQLException {
            BoneCPConfig config = new BoneCPConfig();
            config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/database"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
            config.setUsername("root"); 
            config.setPassword("");
            connectionPool = new BoneCP(config);
            return connectionPool;
        }
    
        public static void loadDataBaseDriver() {
            try {
                // load the database driver (make sure this is in your classpath!)
                Class.forName("com.mysql.jdbc.Driver");
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    
    }
    

    I don’t know the signatures of the methods you are calling so the errors might
    be wrong

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

Sidebar

Related Questions

I'm trying implement pull to refresh on a ListFragment but right now none of
Trying to implement some nested loops that are spitting out good old nested html
Trying to implement something similar to qtip, but using a table that compares the
Trying to implement a function that will return a list of ints the represent
Trying to implement NSCopying for the first time, and I have a question about
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement AVAudioplayer and get some metering data of the played music, but
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Trying to implement the new FP 10.1 Global error handler into my projects but
Trying to implement an autocomplete box ultimately. For now im following php academy's lead.

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.