[HELP] --> Java Thread Related Help

Status
This thread has been locked.

tchristofferson

Developer
Premium
Feedback score
2
Posts
2
Reactions
12
Resources
0
I am making a plugin and the particular code is using an ExecutorService so it does not freeze the main thread. The only problem is it is still happening. Does anyone know why this is happening?

Code:

Code:
double price;

ExecutorService service = Executors.newSingleThreadExecutor();

try {

    price = service.submit(() -> manager.getValue(args[1], 1)).get();

} catch (InterruptedException | ExecutionException e) {

    e.printStackTrace();
    sendError(sender);
    return false;

}

I thought calling the get method on a Future is supposed to run in a different thread.
 
Last edited:
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

FroggyKnight

Feedback score
4
Posts
55
Reactions
30
Resources
0
I've never really used an ExecutorService. So my only answer for you would be just creating a new class and making it a thread. Then just make it handle the prices you're processing. Getting the external data you could use hashmaps or something. Then having the thread returning your prices. Not sure if this is what you want, but just an idea.
 

AgentTroll

Deactivated
Feedback score
0
Posts
1
Reactions
0
Resources
0
Calling Future#get() blocks the caller. The fact that you are executing your task on a different thread doesn't make it faster, it just offloads it from the main thread.

You want to schedule a task on the main thread after you have completed your task and use the result from there.

PHP:
service.submit(() -> {
    manager.getValue(...);
    Bukkit.getScheduler().runTask(plugin, () -> {
        // Use your result here
    });
});
 
Last edited:
Status
This thread has been locked.
Top