Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.craftbukkit.scheduler;
2
3import org.bukkit.plugin.Plugin;
4import org.bukkit.scheduler.BukkitWorker;
5
6public class CraftWorker implements Runnable, BukkitWorker {
7
8 private static int hashIdCounter = 1;
9 private static Object hashIdCounterSync = new Object();
10
11 private final int hashId;
12
13 private final Plugin owner;
14 private final int taskId;
15
16 private final Thread t;
17 private final CraftThreadManager parent;
18
19 private final Runnable task;
20
21 CraftWorker(CraftThreadManager parent, Runnable task, Plugin owner, int taskId) {
22 this.parent = parent;
23 this.taskId = taskId;
24 this.task = task;
25 this.owner = owner;
26 this.hashId = CraftWorker.getNextHashId();
27 t = new Thread(this);
28 t.start();
29 }
30
31 public void run() {
32
33 try {
34 task.run();
35 } catch (Exception e) {
36 e.printStackTrace();
37 }
38
39 synchronized (parent.workers) {
40 parent.workers.remove(this);
41 }
42
43 }
44
45 public int getTaskId() {
46 return taskId;
47 }
48
49 public Plugin getOwner() {
50 return owner;
51 }
52
53 public Thread getThread() {
54 return t;
55 }
56
57 public void interrupt() {
58 t.interrupt();
59 }
60
61 public boolean isAlive() {
62 return t.isAlive();
63 }
64
65 private static int getNextHashId() {
66 synchronized (hashIdCounterSync) {
67 return hashIdCounter++;
68 }
69 }
70
71 @Override
72 public int hashCode() {
73 return hashId;
74 }
75
76 @Override
77 public boolean equals(Object other) {
78 if (other == null) {
79 return false;
80 }
81
82 if (!(other instanceof CraftWorker)) {
83 return false;
84 }
85
86 CraftWorker otherCraftWorker = (CraftWorker) other;
87 return otherCraftWorker.hashCode() == hashId;
88 }
89
90}