#does java timer start its own thread?
1 messages ยท Page 1 of 1 (latest)
<@&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>.
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.
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
Which framework ? Swing ? JavaFX ? Other ?
no its a lib named bullet
Yes Timer or things like ScheduledExecutor use their own thread
fucking knew it
is there a way to force timer to use my current thread? or is there an alternative
no, it simply can't use your thread
otherwise the timer wouldn't work
how do i make it run the method then?
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
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?
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
I am gonna have to check..
package index
I cant really find anything on their api
is there any other way?
what are you trying to do requiring this ?
to keep updating the physics space
does this lib has a update method which is called automatically ?
@robust agate can you show me the error please ?
does not have a stack trace
its their custom error for thread saftey ig
I can try and catch if u want
move the physics in the correct thread then
instead of creating a timer
do the timer yourself with Thread.sleep
should I just make the class extend TimerTask?
no
how would that work
which would be 50ms
if you want 200fps yes
I am just updating it every 1 tick
1 tick = 1 second /20
small error.
?
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
why 1==1, you know that true exists right ?
also delta here is 50
always
and you are not updating lastRan
I am?
well actually, it's more
you are not
lastRan = System.nanotime
ah my bad
but you system has still a problem
which is that you wait for more than 50ms
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
why did you assigned it in a thread ?
because I am trying to run the engine on a seperate thread
but why ?
So if it gets laggy it does not block main thread
and what is the main thread doing ?
running the minecraft server
Running Physics Engine alongside Minecraft
to provide accurate physics in minecraft
why can't you run it during minecraft ticks ?
good question
I thought it would be more efficient and better
What if the physics space gets too populated and starts lagging server
but how are you going to link minecraft ticks and the lib ticks ?
they don't happen at the same time
why do u think I update it every 1 tick
they don't happen at the same time
how?
Because you never made them happen at the same time ?
every entity has an update method
which automatically teleports the mc entity by getting coords from physics space
what if minecraft ticks and your engine ticks don't happen at the same rate ? for example because of lag
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
anyway
why don't you create your physics just before going in the sleep loop and in the same thread as the sleep loop ?
what do u mean
why don't you create the physics engine in the same thread as the while sleep loop ?
i dont know
then do it
ooh I get it..thank u