recursively pass a block to a scheduleSyncDelayedTask

Discussion in 'Plugin Development' started by Ne0nx3r0, Apr 7, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Ne0nx3r0

    I've been playing around with the scheduler since reading this thread; but I can't seem to wrap my fingers around how to go about this...

    How would you go about passing a block (or any data for that matter) to a scheduled task, then recursively pass a different block to a scheduled task and so on until you met a condition? Not necessarily a repeating task, but just tasks that spawn other tasks recursively.

    I've been able to get this to work, but so far my methods are fairly messy. No goal at this point, I'm just trying to get a mindset for how these things work.
     
  2. Offline

    Carnes

    I'm not sure how it's supposed to be done.. but i've been directly changing public properties on the runnable class. Mostly for filling ArrayLists that the scheduled task works on during each run().

    The only problem i've run into so far is removing elements from an array while the separate task is iterating through it, like with a for(Element element: elementArray){}. I get around this by using boolean flags to mark items for deletion/addition and run() does these before operating on the work queue. But so far an async task can spawn entities and read/change everything without causing problems.

    When it's done with a work item it changes a boolean flag (called complete) on the object to true and other threads will grab the item next time they run.
    Sounds like you're doing it perfectly : ) I'd love to hear how your research goes. I only just now discovered the ArrayList.contains property ahhhh! so much to rewrite.
     
  3. Offline

    Kekec852

    Passing data to Runnable is simple if you are scheduling task from the same class Runnable is implemented. You just need to make sure that you code is thread safe using synchronizations. Look at this: Synchronization and SynchronizedMaps and Arrays ...

    Using booleans to block some operations is not the most efficient method, but it gets job done.
     
  4. Offline

    Carnes

    Oh my, this is excellent. Thanks you. Synchronization and Guarded Blocks look perfect.

    Guarded Blocks i get easily but Synchronization either wasn't explained well or i need sleep (boo that word). You just sync a method (not a property :s ) and they are modified in order. But it seems to me you could still end up with a synched method modifying something crucial to the running of the thread and break things.

    Synch just keeps two threads from modifying the same contents of a 3rd thread at the exact same time. So i'd have to block somewhere while the other threads are writing. Or use boolean flags and skip over sections that are being written. I sense much rewriting :oops: and learning : )
     
Thread Status:
Not open for further replies.

Share This Page