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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:58:37+00:00 2026-05-31T04:58:37+00:00

PhysicsThread.java starts a new thread. The run method executes the initialize method. This method

  • 0

PhysicsThread.java starts a new thread. The run method executes the initialize method. This method creates the physics world. After that, it starts a while loop that updates the world with an interval of 1000/60 = 16 milliseconds. However, I get a nullpointerexception in the loop, because it does not always know world, even though I check for that. Can explain what is happening?

PhysicsThread.java

public class PhysicsThread implements Runnable {

long fps = 60;
DiscreteDynamicsWorld dw;
long lasttime = System.currentTimeMillis();;

static int maxPhysicsObjects = 50000;

PhysicsThread() {
    Thread t = new Thread(this);
    t.setPriority(Thread.MIN_PRIORITY);
    t.start();
}

@Override
public void run() {
    System.out.println("Start thread");
    init();
    System.out.println("Start threadloop");

    while(true) {
        loop();
    }
}

public void init() {
    BroadphaseInterface broadphase = new AxisSweep3_32(new Vector3f(0,0,0), new Vector3f(200000,200000,200000), maxPhysicsObjects);//new DbvtBroadphase();
    DefaultCollisionConfiguration collisionConfiguration = new DefaultCollisionConfiguration();
    CollisionDispatcher dispatcher = new CollisionDispatcher(
            collisionConfiguration);

    SequentialImpulseConstraintSolver solver = new SequentialImpulseConstraintSolver();

    dw = new DiscreteDynamicsWorld(dispatcher, broadphase,
            solver, collisionConfiguration);
    System.out.println("Made world");
    dw.setGravity(new Vector3f(0, 0, -10));
}

public void loop() {
    if (dw!=null) {
        float delta = System.currentTimeMillis()-lasttime;
        lasttime = System.currentTimeMillis();
        dw.stepSimulation(delta/1000f, 1, 1/60f);
    }
    try {
        Thread.sleep(1000/fps);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public DiscreteDynamicsWorld getWorld() {
    return dw;
}

}

  • 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-31T04:58:38+00:00Added an answer on May 31, 2026 at 4:58 am

    I suspect this may be down to your construction technique, which is an antipattern. You shouldn’t let the this reference “escape” during a constructor, because then other threads may see the object in a partially-constructed state. At this point, all bets are off about the state of the class, and even invariants that are always true (such as final fields being set to a value) may not hold.

    I wonder if what is happening here might be due to the following sequence of events:

    • The constructor is called and starts the new thread based on this.
    • The new thread runs, and fully executes init (setting dw), and half of loop, before being pre-empted.
    • The main thread continues with the constructor, and sets dw to null as part of field initialisation.
    • The spawned thread continues after working out delta and lastTime, then sees a null value for dw.

    I’m a little hesistant to say that’s exactly the case because I would have expected the fields to be initialised before the constructor body runs. However it seems likely that accessing the fields while the object is still being constructed is a very bad idea.

    In order to fix this I’d suggest not starting a thread in the constructor, but rather having the calling code start the thread afterwards, e.g.:

    final PhysicsThread pt = new PhysicsThread();
    final Thread t = new Thread(pt);
    t.setPriority(Thread.MIN_PRIORITY);
    t.start();
    

    (Also, the idea of having init methods that aren’t the constructor is usually a bad idea – if the dw field never changes, why not perform all the work of init during the constructor itself and mark dw as final? This will be cleaner and make your code easier to reason about, since the VM guarantees the value will be fully set during construction and that it will never change afterwards. The only downside is a performance hit if you construct lots of these instances and never actually run them – so just don’t do that. 🙂

    Plus the class name is misleading, since this isn’t a Thread but a Runnable. Calling it something like PhysicsTask or PhysicsSimulationRun would arguably be clearer.)

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

Sidebar

Related Questions

I have a classic physics-thread vs. graphics-thread problem: Say I'm running one thread for
This is (roughly) what I have: class A { public bool IsInUpdate = false;
I'm interested in getting threading into the small engine I'm working on in my
So I'm trying to create a simple multi-threaded game engine for the game I

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.