Multi-threading

Hi,

I’m trying to access my main thread from my secondary thread… But that is not going that well right now.
What I’m trying to do… Is sending a console command from the thread that is doing the work. But I do not have that much experience with threading… If there is someone here that can point me in the right direction or show me an example. That would be really great.

And I’m using Sponge scheduler to run the task async.

Error from console:

[00:28:00] [pool-3-thread-1/FATAL] [Sponge]: stopTiming called async for playerCommand [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: java.lang.Throwable [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at co.aikar.timings.TimingHandler.stopTiming(TimingHandler.java:130) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at net.minecraft.command.CommandHandler.handler$onExecuteCommandReturn$0(SourceFile:91) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at net.minecraft.command.CommandHandler.func_175786_a(SourceFile:83) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at org.spongepowered.common.command.MinecraftCommandWrapper.tryExecute(MinecraftCommandWrapper.java:146) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at org.spongepowered.common.command.MinecraftCommandWrapper.process(MinecraftCommandWrapper.java:132) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at consoleSender.sendCommand(consoleSender.java:19) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at backup.backupWork(backup.java:34) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at backup.lambda$runAsyncNow$1(backup.java:76) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at org.spongepowered.api.scheduler.Task$Builder.lambda$execute$12(Task.java:138) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at org.spongepowered.common.scheduler.SchedulerBase.lambda$startTask$570(SchedulerBase.java:177) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at java.util.concurrent.FutureTask.run(Unknown Source) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [00:28:00] [pool-3-thread-1/ERROR] [STDERR]: at java.lang.Thread.run(Unknown Source)

Code:

public static void runAsync() {
    int interval = Config.getTimerInterval();

    Scheduler schedula = Sponge.getScheduler();
    Task.Builder taskBuilder = schedula.createTaskBuilder();

    Task task = (Task) taskBuilder.execute(() -> backupWork()).async().delay(TimeUnit.MINUTES.toMillis(interval), TimeUnit.MILLISECONDS)
            .interval(TimeUnit.MINUTES.toMillis(interval), TimeUnit.MILLISECONDS)
            .name("AutoBackup - backup process has started!").submit(autoBackup.getAuto());
}

From my command sender. I believe that is from you… From my last post.

public static void sendCommand(String command){
    //Get console
    CommandSource console = autoBackup.console;
    java.util.Optional<? extends CommandMapping> optCommandMapoping = autoBackup.cManager.get(command, console);
    if (optCommandMapoping.isPresent()){
        CommandMapping mapping = optCommandMapoping.get();
        try
        {
            mapping.getCallable().process(console, "");
        }catch (CommandException e){
            e.printStackTrace();
        }
    }
 }

Thanks!!

If you could show us the code you are using it would be a big help.

I have added the code.

Okay, so what you want to do is run the work task, and then from in that work task, schedule a non-async task with no delay in which you call the command.

So:

  1. Start worker thread
  2. Determine command
  3. From within worker thread, construct task to execute the command
  4. Start that task as a non-async, no-delay, task
  5. ???
  6. Profit!

Edit:
I do this all the time when I’m making either a server query or processing some data, and then I return the main thread to execute/display the result. If done right, it works perfectly.

Oh wow… Well that worked :smiley:
Thanks! Didn’t think about actually starting a new TASK from a task… That is not async… :smiley: