简体   繁体   English

多线程问题

[英]Multithreading problems

I'm trying get more into multithreading in Java, but my endeavors have been stopped at this code:我正在尝试更多地了解 Java 中的多线程,但我的努力已停止在此代码上:

package io.nlaz.test.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.List;

public class test implements TabExecutor {
        @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        try {
            Thread a24 = new Thread(() -> {
                try {
                    Thread.sleep(1000);
                    sender.sendMessage("2");
                    Thread.sleep(2000);
                    sender.sendMessage("4");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            Thread a35 = new Thread(() -> {
                try {
                    Thread.sleep(3000);
                    sender.sendMessage("3");
                    Thread.sleep(2000);
                    sender.sendMessage("5");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            sender.sendMessage("1");
            a24.start();
            a35.start();
        } catch (Exception e) { e.printStackTrace(); }
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
        return null;
    }
}

When the code is run, it supposed to send a message counting up to 5 every second.当代码运行时,它应该每秒发送一条最多计数 5 条的消息。 but when run it get's up to 2, then waits for the a24 thread to finish, then executes a35.但是在运行时它会达到 2,然后等待 a24 线程完成,然后执行 a35。 I've looked up possible solutions to this, however they all either lead to the main thread getting paused, or the exact same problem as I already have.我已经查找了可能的解决方案,但是它们都导致主线程暂停,或者与我已经遇到的完全相同的问题。

I do have a part of it might be because I'm using Thread.sleep() and not something like Thread.currentThread().wait() .我确实有一部分可能是因为我使用的是Thread.sleep()而不是Thread.currentThread().wait()之类的东西。 However when I use that method, I get an error saying java.lang.IllegalMonitorStateException: current thread is not owner .但是,当我使用该方法时,我收到一条错误java.lang.IllegalMonitorStateException: current thread is not owner Any suggestions help, thank you!任何建议都有帮助,谢谢!

Do NOT use Thread with Bukkit's plugins.不要Thread与 Bukkit 的插件一起使用。 Spigot has his own threading system. Spigot 有自己的线程系统。

BukkitTask task = Bukkit.getScheduler().runTaskTimer(myPlugin, new Runnable() {
   private int timer = 4;

   @Override
   public void run() {
      if(timer == 4 || timer ==2) {
          sender.sendMessage("Time: " + timer);
      }
      if(timer == 0) {
          task.cancel();
      }
      timer--;
   }
});

Documentation 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM