What is different in this codes? Thread is working same as Task? What can I use in run function in Thread? PHP: class Task extends PluginTask{public function onRun(){$this->plugin->getServer()->broadcastMessage("Test");}} PHP: class Task extends Thread{public function run(){//What all can be here???}}
If you don't know what a thread or an Async task does, I don't very much recommend to do it. Tasks are used to execute parts of code either after a delay, or every * ticks. There are three types of normal tasks, which are: DelayedTask: Executes the task after a delay, which is specified in the second argument of it. RepeatingTask: Executes the task every * seconds, where * is specified in the second argument. DelayedRepeatingTask: Combination of the tasks above, both delayed and repeating. Those three tasks are ran on the main thread. They require time to execute, (most of the time not notable) and should be handled with care if you're dealing with time consuming functions. In this case it's best to use an AsyncTask. An async task runs asynchronously to the main thread, and requires an 'Async worker' to work. Using too many async tasks can take up these async workers, and will disallow you to execute more async tasks. Async tasks are not always easy to use, especially with the PocketMine API. Async tasks shouldn't hold references to the PocketMine API. Additionally, because Async tasks run asynchronously to the main thread, you should check if the server is still running, preferably a couple times. Starting a new Thread has never been very clear to me. If I'm correct, it's mostly used for executing code more than just once, like an AsyncTask does, and I would again recommend it only if you know what you're doing.
Conceptually incorrect. There is only one type of asynchronous task. Delayed repeating task is not a reasonable type, because a repeating task can be made delayed at the same time, or you can first delay then repeat then delay the same task. Delay and repeat are verbs. They are actions upon a task, not properties of the task. There are three API operations you can do with a task to schedule it, but there is only one type of task (not counting async tasks, since it's totally irrelevant).
There is no definition called "ideally". Every situation is a different case. You don't need an asynchronous task unless it's seriously heavy burden on the main thread and it is appropriate for separation. Moreover, a synchronous task is only for executing something later. It is generally irrelevant with decreasing the load, except for operations that you have to do on the main thread bit by bit. You are all messing up all the concepts.
Basically you only need to start a thread if you have a persistent server (e.g. an IRC bridge, an HTTP server, a mail server) or otherwise a listener that will run for the whole lifetime of the server.
I should've been more specific. Rather than extending Thread I'd "ideally" extend AsyncTask which is easier because it's managed by PocketMine, but nobody should use asynchronous tasks unless it's something that could freeze the server.