I am creating a simulator, but for simplicity sake, it's a 'game'. Thus it has a render() and an update() function. I've been trying to practice lambda expressions during summer but I cannot seem to be able to wrap my head around doing a lambda expression of another lambda expression. I am probably saying this wrong, but what I am trying to do is start two threads, one that loops rendering, and another that loops updating. I can get this far:
void render() {
//draw entities, etc.
}
void update() {
//update player/enemies, etc.
}
public GameFrame() {
/* init stuff */
Thread updateThread = new Thread(this::update);
Thread renderThread = new Thread(this::render);
}
This is not what I want because this only runs update and draw once, thus this is just two threads, one that renders once, and one that updates once (no looping). I want to create a function that does something along the lines of:
public void loop(Supplier< /*?*/ > arg) {
long startTime;
while(running) {
startTime = System.currentTimeMillis();
supplier.get(arg) // <- not sure about this either
try {
long sleepTime = 1000/FPS - (System.currentTimeMillis() - startTime);
if(sleepTime > 0)
Thread.sleep(sleepTime);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
Then I would call the function like this?
loop(this::update);
I believe this would cause the supplied function to loop, thus I tried:
Thread updateThread = new Thread(this::loop(this::update));
Or even something like this:
new Thread(() -> loop(this::update)).start();
I know I can just make my render function while loop, and my update function while loop by just copying the 'void loop(Supplier arg)' code into each part, but I wanted to see if I could do it this way anyways.
I don't need an exact answer, I would just like some guidance to what I'm not thinking about/what I'm thinking about wrong. I haven't been able to make much progress by reading up on lambda expressions on oracle.