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

The Archive Base Latest Questions

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

I want to save an object (my game’s model/data) to disk, but as the

  • 0

I want to save an object (my game’s model/data) to disk, but as the game can get quite large – feasibly large enough to take several game ticks to store – I’m thinking it makes sense to perform the saving in a separate thread in order to keep the game running relatively smoothly.

What is the best method to achieve this? I wasn’t sure if it made sense to make the GameState (the model) a runnable or an extension of Thread, because most of the time it isn’t intended to be Runnable – logically, then, it shouldn’t be Runnable?

Other possibilities I’ve looked at are to have a Runnable GameSaver class, to which I pass the GameState or a copy of the GameState. Presumably, however, this would cause problems with synchronisation if I pass the GameState or will slow the game down while the class is cloned.

What’s the best approach, or the pro’s and con’s of approaches? Any other alternatives appreciated, too – I doubt my search has been exhaustive.

  • 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-02T11:19:27+00:00Added an answer on June 2, 2026 at 11:19 am

    From a pure thread management perspective, the cleanest way I think is to use an executor. That does not solve your cloning (or not) issue.

    Create a method that saves the game:

    public void saveTheGame() {
        //you maybe need to take a snapshot, which might require synchronization
        GameState state = ....;
    }
    

    Create a runnable, as a class instance member for example, that embeds that call and an executor service:

    private final Runnable save = new Runnable() {
        public void run() {
            saveTheGame();
        }
    }
    private final ExecutorService executor = Executors.newFixedThreadPool(1);
    

    And save the game as and when needed:

    executor.submit(save);
    

    Don’t forget to shutdown the executor when closing your app:

    executor.shutdown();
    

    You can also use a ScheduledExecutorService instead that runs every x minutes for example.

    The class might look like this for example:

    public static class GameSaver {
        private final Runnable save = new Runnable() {
    
            @Override
            public void run() {
                saveGame();
            }
        };
        private static final ExecutorService executor = Executors.newFixedThreadPool(1);
        private final GameState state;
    
        public GameSaver(GameState state) {
            this.state = state;
        }
    
        public void save() {
            executor.submit(save);
        }
    
        public static void close() {
            executor.shutdown();
        }
    
        private void saveGame() {
            //save your game here
        }
    
    }
    

    and in your main code:

    GameState state = getGameState();
    GameSaver saver = new GameSaver(state);
    saver.save();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save and load my xml data using XmlReader. But I don't
i want to save some user settings on Session object. And get it from
I want to save an object or form to the database. Only I can't
I want to be able to save any C# object in a single column
I want to save one of my entity objects into the session, but as
I want to save a matrix to a text file, so I can read
I want to save a remote img-file to my server, but I don't know
i want to save nssarray object into string. code is for (NSMutableDictionary *dictionary in
I want to save gtk.DrawingArea() object contents to jpeg file using PIL. Particularly I
I want to save object state and reuse it after some time. I got

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.