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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:04:41+00:00 2026-06-02T15:04:41+00:00

i’m trying to write some game engine for my projects and I’m facing a

  • 0

i’m trying to write some “game engine” for my projects and I’m facing a problem with threads. When I create a Thread (LoadThread) in main flow, it keeps waiting until Run(); in LoadThread ends.

//loading thread
public class LoadThread implements Runnable{
    private boolean running = false;
    private Thread loader = null;

    public LoadThread(/*init data structures*/){
        loader = new Thread(this);
    }

    public void start(){
        running = true;
        run();
    }

    synchronized public void run() {
        System.out.println(" loading started ");
        while(running){            
            //do some loading, when done, running = false
        }
        System.out.println(" loading done ");
    }
}

//holds data, starts loading
public class SourceGod {
    private LoadThread img_loader;
    public void startLoading(){
        img_loader = new LoadThread(/* some data structures */);
        img_loader.start();
    }
}

//runs the game
public class Game extends GameThread implements ActionListener{
    private SourceGod sources;
    public Game(Window full_screen){
        sources = new SourceGod(/* some data structures */);
        System.out.println("before");
        sources.startLoading();
        System.out.println("after");
    }
}

//own thread to refresh
abstract public class GameThread extends JPanel implements Runnable{
    //anything from there is not called before "after"
}

output

before
loading started
//some loaded data report, takes about 2-3s
loading done
after

Any help will be appreciated.
( little more of code http://paste.pocoo.org/show/orCfn9a8yOeEQHiUrgjG/ )
Thanks,
Vojtěch

  • 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-02T15:04:43+00:00Added an answer on June 2, 2026 at 3:04 pm

    Your problem is that you are creating a Thread object in your constructor but when you call start() you are not starting the thread but running the run() method in the same thread. This looks to be confusion around extending Thread versus implementing Runnable.

    What I think you should do instead is something like:

    // this should be of type Thread not LoadThread
    private Thread img_loader;
    ...
    // don't create the loader thread inside of LoadThread
    img_loader = new Thread(new LoadThread(/* some data structures */));
    img_loader.start();
    

    LoadThread is the class of type Runnable that the thread will be executing. With your current code, when it calls:

    img_loader.start();
    

    It isn’t actually starting a new Thread, it is just calling the LoadThread.start() method which calls run() in the same thread:

    public void start(){
        running = true;
        run();
    }
    

    Edit:

    In looking more closely at your posted code in the link you provided, you are creating your Thread object inside of the LoadThread constructor. This is not a good pattern:

    public LoadThread(/*init data structures*/) {
        // not recommended IMO
        loader = new Thread(this);
    }
    

    So again, when you are calling LoadThread.start() you aren’t starting the thread but calling run() in the same thread. I would use the new Thread(new LoadThread()) pattern instead. If you are stuck on wrapping the Thread inside of LoadThread then you should change LoadThread.start() to be:

    // this should be removed, you want to call Thread.start() instead
    public void start(){
        running = true;
        // this will start the internal thread which will call run()
        loader.start();
    }
    

    Btw, if you want to set running to be false in another thread then it should be marked as volatile.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.