#does java timer start its own thread?

1 messages ยท Page 1 of 1 (latest)

robust agate
#

title

harsh violetBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Title

No, Java Timer does not start its own thread. Instead, it uses a single background thread that is shared among all Timer instances. This means that if you have multiple Timer instances running, they will all share the same thread for executing their tasks.

This design choice was made to reduce the overhead of creating and managing multiple threads for each Timer instance. However, it also means that if a task takes a long time to execute or if one Timer instance blocks the shared thread, it can affect the execution of tasks in other Timer instances.

If you need more control over threading behavior or need to run tasks in parallel, you may want to consider using a different concurrency mechanism such as Executors in the java.util.concurrent package.

Overall, while Java Timer provides a simple way to schedule tasks to run at specific times or intervals, it is important to be aware of its threading behavior and limitations when designing your applications.

robust agate
#

that is intresting bc I am being yelled at by my code for wrong thread

#

Thread.startVirtualThread(() -> physicsEngine = new PhysicsEngine()); I have this in my main class

#

and the constructor of physics engine has

#
        new Timer().schedule(new TimerTask() {
            long lastRan = System.nanoTime();
            @Override
            public void run() {
                long diff = System.nanoTime() - lastRan;
                float deltaTime = diff / 1_000_000_000f;

                lastRan = System.nanoTime();
                update(deltaTime);
            }
        }, 1L, 1L);```
#

it yells for using wrong thread everytime I update the engine

#

can it be because the variable is in main class? which is on main thread

remote temple
robust agate
#

no its a lib named bullet

remote temple
#

Yes Timer or things like ScheduledExecutor use their own thread

robust agate
#

fucking knew it

#

is there a way to force timer to use my current thread? or is there an alternative

remote temple
#

otherwise the timer wouldn't work

robust agate
#

how do i make it run the method then?

remote temple
#

a framework would usually share a method when you can give it code to execute in their thread

#

With Swing, you have SwingUtilities.invokeLater, with JavaFX, you have Platform.runLater, etc

robust agate
#

it just needs the native library loaded on that thread too

#
    public void update(float timeInterval, int maxSteps, boolean doEnded,
            boolean doProcessed, boolean doStarted) {
        assert Validate.nonNegative(timeInterval, "time interval");
        assert Validate.nonNegative(maxSteps, "max steps");

        if (NativeLibrary.jniEnvId() != jniEnvId()) {
            logger.log(Level.WARNING, "invoked from wrong thread");
        }

        long spaceId = nativeId();
        assert accuracy > 0f : accuracy;
        stepSimulation(spaceId, timeInterval, maxSteps, accuracy, doEnded,
                doProcessed, doStarted);
    }```
#

maybe I can just load library there too?

remote temple
#

wdym

#

also note that frameworks tend to give you a special timer which work with them

#

Swing has Timer, and JavaFX has Animation

#

for example

robust agate
#

I am gonna have to check..

#

I cant really find anything on their api

#

is there any other way?

remote temple
robust agate
#

to keep updating the physics space

remote temple
robust agate
#

nope

#

you need to call update yourself if u wanna update

#

this is going to be hard

remote temple
#

@robust agate can you show me the error please ?

robust agate
#

does not have a stack trace

#

its their custom error for thread saftey ig

#

I can try and catch if u want

remote temple
#

instead of creating a timer

#

do the timer yourself with Thread.sleep

robust agate
#

should I just make the class extend TimerTask?

remote temple
#

no

robust agate
remote temple
#

you do a while loop

#

and you sleep for a certain time

robust agate
#

which would be 50ms

remote temple
#

if you want 200fps yes

robust agate
#

I am just updating it every 1 tick

remote temple
#

but what is one tick ?

#

you want to update each 1000/tps ms

robust agate
#

1 tick = 1 second /20

remote temple
#

so 1000/20=50

#

ms

robust agate
#

small error.

remote temple
#

?

robust agate
#
        long lastRan = System.nanoTime();
        while (1 == 1) {
            Thread.sleep(50);
            long diff = System.nanoTime() - lastRan;
            float deltaTime = diff / 1_000_000_000f;

            lastRan = System.nanoTime();
            update(deltaTime);
        }```
#

yeah

#

do u just mean this?

#

it looks like its not giving any error

remote temple
#

also delta here is 50

#

always

#

and you are not updating lastRan

robust agate
#

I am?

remote temple
remote temple
robust agate
#

lastRan = System.nanotime

remote temple
#

ah my bad

#

but you system has still a problem

#

which is that you wait for more than 50ms

robust agate
#

hmm

#

java.lang.NullPointerException: Cannot invoke "io.github.unjoinable.physics.PhysicsEngine.addObject(io.github.unjoinable.physics.PhysicsObject)" because "physicsEngine" is null

#

I legit

#

private static volatile PhysicsEngine physicsEngine;

#
        Thread.startVirtualThread(() -> {
            try {
                physicsEngine = new PhysicsEngine();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });```
#

thats all done on the main thread

remote temple
robust agate
#

because I am trying to run the engine on a seperate thread

remote temple
#

but why ?

robust agate
#

So if it gets laggy it does not block main thread

remote temple
#

and what is the main thread doing ?

robust agate
#

running the minecraft server

remote temple
#

huh

#

wait

#

what are you trying to do ?

robust agate
#

Running Physics Engine alongside Minecraft

#

to provide accurate physics in minecraft

remote temple
#

why can't you run it during minecraft ticks ?

robust agate
#

good question

#

I thought it would be more efficient and better

#

What if the physics space gets too populated and starts lagging server

remote temple
#

but how are you going to link minecraft ticks and the lib ticks ?

#

they don't happen at the same time

robust agate
#

why do u think I update it every 1 tick

remote temple
#

they don't happen at the same time

robust agate
#

how?

remote temple
#

Because you never made them happen at the same time ?

robust agate
#

this is confusing

#

i mean they dont really need to happen at same time do they

remote temple
#

depends

#

how are you linking them ?

robust agate
#

every entity has an update method

#

which automatically teleports the mc entity by getting coords from physics space

remote temple
#

what if minecraft ticks and your engine ticks don't happen at the same rate ? for example because of lag

robust agate
#

I have not thought about that yet BUT

#

I think concurrency is bad now

#

I can just get instance thread from minecraft and run it on that

remote temple
#

anyway

#

why don't you create your physics just before going in the sleep loop and in the same thread as the sleep loop ?

robust agate
#

what do u mean

remote temple
#

why don't you create the physics engine in the same thread as the while sleep loop ?

robust agate
#

i dont know

remote temple
#

then do it

robust agate
#

ooh I get it..thank u