1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

What is different?

Discussion in 'Development' started by Kyd, Apr 3, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    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???
    }
    }
     
  2. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Ideally I'd use an Asynchronous task.
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    What is doing AsyncTask? :v
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    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.
     
    SOFe and xBeastMode like this.
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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).
     
    jasonwynn10 likes this.
  6. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
    jasonwynn10 likes this.
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    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.
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    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.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.