TimedList - Automatically remove after configurable amount of seconds.

Discussion in 'Resources' started by bigteddy98, Aug 6, 2014.

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

    bigteddy98

    This resource is no longer availabe.
     
    LCastr0 likes this.
  2. Offline

    Comphenix

    I don't see why you need to create an entire background thread for every TimedList - it introduces concurrency for no good reason, wastes a lot of memory (usually 2 MB for the thread's stack) and is not very accurate as the entry count down may fall out of sync with the system clock. Also - isn't it more accurate to call this a TimedSet, as there is no get(index) method or defined order?

    Instead, try emulating a DelayQueue, and keep a priority queue of entries that are about to expire. That way, you don't have to scan the entire set for expired items, and you can perform a fast check every time the set is accessed.

    I've actually written a HashMap (ProtocolLib) with this feature myself, but it can also be used as a Set:
    Code:java
    1. private static ExpireHashMap<String, Boolean> clickers = new ExpireHashMap<String, Boolean>();
    2.  
    3. public static void main(String[] args) throws Exception {
    4. clickSign();
    5. clickSign();
    6. Thread.sleep(3001);
    7.  
    8. clickSign();
    9. }
    10.  
    11. private static void clickSign() {
    12. if (clickers.containsKey("player")) {
    13. System.out.println("Spam clicking is not allowed.");
    14. } else {
    15. clickers.put("player", true, 3, TimeUnit.SECONDS);
    16. System.out.println("Clicked sign.");
    17. }
    18. }

    Of course, you could just use Cooldowns in this case:
    Code:java
    1. if (Cooldowns.tryCooldown(player, "Signs", 3000)) {
    2. // Do what you need to do here. You don't have to set the cooldown when you're done.
    3. } else {
    4. // Cooldown hasn't expired yet
    5. player.sendMessage("Spam clicking is not allowed.");
    6. }

    That might be the simplest option - and it's just as fast as ExpireHashMap.
     
    stirante likes this.
  3. Offline

    bigteddy98

    Ah thanks, I will use ProtocolLib instead.
     
  4. Offline

    Comphenix

    Sure, but you can also copy ExpireHashMap into your project if you don't mind GPL.

    Still, I think TimedList could have been salvaged - if you'd replaced the ScheduledExecutorService with just using the standard Bukkit scheduler, the overhead would have been minimal in most cases. Or gone with the delay queue approach.
     
  5. Offline

    Phasesaber

    Deleting the thread might not be such a great idea, any code can be useful to someone.
     
Thread Status:
Not open for further replies.

Share This Page