java 带PriorityQueue的数据库管理器

khbbv19g  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(113)

x1c 0d1x的数据

  1. import java.io.PrintWriter;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. public class LabProgram {
  5. public static boolean verifyCallbacks(PrintWriter testFeedback, ArrayList<String> actual,
  6. ArrayList<String> expected) {
  7. // Compare ArryList sizes first
  8. boolean areEqual = true;
  9. if (actual.size() == expected.size()) {
  10. for (int i = 0; areEqual && i < actual.size(); i++) {
  11. if (!actual.get(i).equals(expected.get(i))) {
  12. areEqual = false;
  13. }
  14. }
  15. }
  16. else {
  17. areEqual = false;
  18. }
  19. // Print results
  20. testFeedback.write(areEqual ? "PASS" : "FAIL");
  21. testFeedback.write(": Verification of invoked callbacks\n");
  22. testFeedback.write(" Expected: " + expected + "\n");
  23. testFeedback.write(" Actual: " + actual + "\n");
  24. return areEqual;
  25. }
  26. public static boolean test1(PrintWriter testFeedback) {
  27. TestClock clock = new TestClock();
  28. TimeoutManager timeouts = new TimeoutManager(clock);
  29. ArrayList<String> actualCallbacks = new ArrayList<String>();
  30. ArrayList<String> expectedCallbacks = new ArrayList<String>();
  31. // Test with items:
  32. // Added at clock time = 0:
  33. // - item D with delay 500 (callback time is 500)
  34. // - item A with delay 100 (callback time is 100)
  35. // Added at clock time = 50
  36. // - item B with delay 150 (callback time is 200)
  37. clock.setTime(0);
  38. timeouts.addTimeout(() -> {
  39. actualCallbacks.add("D");
  40. testFeedback.write("Item D's callback\n");
  41. }, 500);
  42. timeouts.addTimeout(() -> {
  43. actualCallbacks.add("A");
  44. testFeedback.write("Item A's callback\n");
  45. }, 100);
  46. clock.setTime(50);
  47. timeouts.addTimeout(() -> {
  48. actualCallbacks.add("B");
  49. testFeedback.write("Item B's callback\n");
  50. }, 150);
  51. // Do an update with clock time = 100, which should invoke item A's
  52. // callback
  53. clock.setTime(100);
  54. testFeedback.write("Updating with clock time = 100. Item A should show below.\n");
  55. timeouts.update();
  56. // Verify that only item A's callback was called
  57. expectedCallbacks.add("A");
  58. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  59. return false;
  60. }
  61. // Do another update with a clock time of 150, which shouldn't invoke any
  62. // callbacks
  63. clock.setTime(150);
  64. testFeedback.write("Updating with clock time = 150. No callbacks should show below.\n");
  65. timeouts.update();
  66. // Verify that still only item A's callback has been called
  67. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  68. return false;
  69. }
  70. // Add more timeouts at clock time = 300:
  71. // - item E with delay 500 (callback time is 800)
  72. // - item C with delay 100 (callback time is 400)
  73. clock.setTime(300);
  74. testFeedback.write("Adding timeouts E and C\n");
  75. timeouts.addTimeout(() -> {
  76. actualCallbacks.add("E");
  77. testFeedback.write("Item E's callback\n");
  78. }, 500);
  79. timeouts.addTimeout(() -> {
  80. actualCallbacks.add("C");
  81. testFeedback.write("Item C's callback\n");
  82. }, 100);
  83. // Verify that adding new timeouts didn't invoke any new callbacks
  84. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  85. return false;
  86. }
  87. // Do an update with a clock time of 350, which should invoke item B's
  88. // callback
  89. clock.setTime(350);
  90. testFeedback.write("Updating with clock time = 350. Item B should show below.\n");
  91. timeouts.update();
  92. // Verify callbacks: A and B called, others not
  93. expectedCallbacks.add("B");
  94. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  95. return false;
  96. }
  97. // Do an update with a clock time of 550, which should invoke item C's
  98. // callback and item D's callback, in that order
  99. clock.setTime(550);
  100. testFeedback.write("Updating with clock time = 550. Item C and D should show below, ");
  101. testFeedback.write("in that order.\n");
  102. timeouts.update();
  103. // Verify callbacks: A, B, C, and D
  104. expectedCallbacks.add("C");
  105. expectedCallbacks.add("D");
  106. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  107. return false;
  108. }
  109. // Do another update with a clock time of 700, which shouldn't invoke any
  110. // callbacks
  111. clock.setTime(700);
  112. testFeedback.write("Updating with clock time = 700. No callbacks should show below.\n");
  113. timeouts.update();
  114. // Verify callbacks: again just A, B, C, and D
  115. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  116. return false;
  117. }
  118. // Do a final update with time = 900, which should invoke item E's callback
  119. clock.setTime(900);
  120. testFeedback.write("Updating with clock time = 900. Item E should show below.\n");
  121. timeouts.update();
  122. // Verify callbacks: A, B, C, D, and E
  123. expectedCallbacks.add("E");
  124. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. public static boolean test2(PrintWriter testFeedback) {
  130. TestClock clock = new TestClock();
  131. TimeoutManager timeouts = new TimeoutManager(clock);
  132. ArrayList<String> actualCallbacks = new ArrayList<String>();
  133. ArrayList<String> expectedCallbacks = new ArrayList<String>();
  134. // At t = 0:
  135. // - Add item A with delay 800 (callback time is 800)
  136. clock.setTime(0);
  137. testFeedback.write("At t=0, adding timeout A with delay = 800\n");
  138. timeouts.addTimeout(() -> {
  139. actualCallbacks.add("A");
  140. }, 800);
  141. // At t = 50:
  142. // - Add item D with delay 600 (callback time is 650)
  143. clock.setTime(50);
  144. testFeedback.write("At t=50, adding timeout D with delay = 600\n");
  145. timeouts.addTimeout(() -> {
  146. actualCallbacks.add("D");
  147. }, 600);
  148. // At t = 100:
  149. // - Add item C with delay 200 (callback time is 300)
  150. clock.setTime(100);
  151. testFeedback.write("At t=100, adding timeout C with delay = 200\n");
  152. timeouts.addTimeout(() -> {
  153. actualCallbacks.add("C");
  154. }, 200);
  155. // At t = 150:
  156. // - Add item E with delay 250 (callback time is 400)
  157. clock.setTime(150);
  158. testFeedback.write("At t=150, adding timeout E with delay = 250\n");
  159. timeouts.addTimeout(() -> {
  160. actualCallbacks.add("E");
  161. }, 250);
  162. // At t = 200:
  163. // - Add item B with delay 50 (callback time is 250)
  164. // - Update, then verify that no callback have yet been called
  165. clock.setTime(200);
  166. testFeedback.write("At t=200, adding timeout B with delay = 50");
  167. testFeedback.write(", then updating\n");
  168. timeouts.addTimeout(() -> {
  169. actualCallbacks.add("B");
  170. }, 50);
  171. timeouts.update();
  172. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  173. return false;
  174. }
  175. // At t = 400:
  176. // - Update, then verify that callbacks B, C, and E have been called
  177. clock.setTime(400);
  178. testFeedback.write("At t=400, updating\n");
  179. timeouts.update();
  180. expectedCallbacks.add("B");
  181. expectedCallbacks.add("C");
  182. expectedCallbacks.add("E");
  183. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  184. return false;
  185. }
  186. // At t = 450:
  187. // - Add item F with delay 100 (callback time is 550)
  188. clock.setTime(450);
  189. testFeedback.write("At t=450, adding timeout F with delay = 100\n");
  190. timeouts.addTimeout(() -> {
  191. actualCallbacks.add("F");
  192. }, 100);
  193. // At t = 600:
  194. // - Update, then verify callbacks: B and C from earlier, E and F now
  195. clock.setTime(600);
  196. testFeedback.write("At t=600, updating\n");
  197. timeouts.update();
  198. expectedCallbacks.add("F");
  199. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  200. return false;
  201. }
  202. // At t = 800:
  203. // - Update, then verify callbacks: B, C, E, F, D, A
  204. clock.setTime(800);
  205. testFeedback.write("At t=800, updating\n");
  206. timeouts.update();
  207. expectedCallbacks.add("D");
  208. expectedCallbacks.add("A");
  209. if (!verifyCallbacks(testFeedback, actualCallbacks, expectedCallbacks)) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. public static void main(String[] args) {
  215. PrintWriter testFeedback = new PrintWriter(System.out);
  216. boolean test1Result = test1(testFeedback);
  217. testFeedback.println();
  218. boolean test2Result = test2(testFeedback);
  219. testFeedback.println();
  220. testFeedback.flush();
  221. System.out.println("Local test 1: " + (test1Result ? "PASS" : "FAIL"));
  222. System.out.println("Local test 2: " + (test2Result ? "PASS" : "FAIL"));
  223. testFeedback.close();
  224. }
  225. }
  1. public abstract class MillisecondClock {
  2. abstract int getTime();
  3. }
  1. public class TestClock extends MillisecondClock {
  2. private int currentTime;
  3. public TestClock() {
  4. currentTime = 0;
  5. }
  6. @Override
  7. public int getTime() {
  8. return currentTime;
  9. }
  10. // Sets this clock's current time, provided the new time is greater than the
  11. // previously set time.
  12. public boolean setTime(int newTime) {
  13. // Only allow time to move forward
  14. if (newTime > currentTime) {
  15. currentTime = newTime;
  16. return true;
  17. }
  18. return false;
  19. }
  20. }
  1. @FunctionalInterface
  2. interface CallbackMethod {
  3. void invoke();
  4. }
  5. public class TimeoutItem implements Comparable<TimeoutItem> {
  6. private int callbackTime;
  7. private CallbackMethod callbackMethod;
  8. public TimeoutItem(CallbackMethod callbackMethod, int callbackTime) {
  9. this.callbackTime = callbackTime;
  10. this.callbackMethod = callbackMethod;
  11. }
  12. public TimeoutItem(TimeoutItem toCopy) {
  13. this.callbackTime = toCopy.callbackTime;
  14. this.callbackMethod = toCopy.callbackMethod;
  15. }
  16. public void callCallback() {
  17. callbackMethod.invoke();
  18. }
  19. final public int getCallbackTime() {
  20. return callbackTime;
  21. }
  22. @Override
  23. public int compareTo(TimeoutItem tItem) {
  24. // Java's PriorityQueue keeps item with smallest value at the front.
  25. if (this.callbackTime > tItem.callbackTime) {
  26. return 1;
  27. }
  28. else if (this.callbackTime < tItem.callbackTime) {
  29. return -1;
  30. }
  31. else {
  32. return 0;
  33. }
  34. }
  35. }
  1. import java.util.PriorityQueue;
  2. public class TimeoutManager {
  3. private PriorityQueue<TimeoutItem> queue;
  4. private MillisecondClock clock;
  5. public TimeoutManager(MillisecondClock clock) {
  6. this.queue = new PriorityQueue<>();
  7. this.clock = clock;
  8. }
  9. public void addTimeout(Runnable callback, int delayBeforeCallback) {
  10. int callbackTime = clock.getTime() + delayBeforeCallback;
  11. TimeoutItem item = new TimeoutItem(callback, callbackTime);
  12. queue.offer(item);
  13. }
  14. public void update() {
  15. int currentTime = clock.getTime();
  16. while (!queue.isEmpty() && queue.peek().getCallbackTime() <= currentTime) {
  17. TimeoutItem item = queue.poll();
  18. item.getCallbackMethod().run();
  19. }
  20. }
  21. }

这是我的代码,但我一直得到错误:
第14章:你是谁?错误:不兼容的类型:Runnable无法转换为CallbackMethod TimeoutItem item = new TimeoutItem(callback,callbackTime); ^ TimeoutManager.java:22:error:cannot find symbol item.getCallbackMethod().run(); ^ symbol:method getCallbackMethod()location:variable item of type TimeoutItem注意:某些消息已被简化;使用-Xdiags:verbose重新编译以获得完整输出2错误
我知道我必须在最后一部分做些什么,但我被卡住了。请帮助

mfuanj7w

mfuanj7w1#

超时提示符.PY应该看起来像这样:
从队列导入系统导入优先级队列
从TimeoutItem导入TimeoutItem从时钟导入 *
类TimeoutManager:definit(self,clock):#add_timeout()和update()使用的当前时间self.clock = clock #TimeoutItems的优先级队列。具有#最低回调时间的超时项首先被出队。self.pq = PriorityQueue()

  1. # Return a timeout manager's internal priority queue.
  2. # Used only for grading purposes.
  3. def get_priority_queue(self):
  4. return self.pq
  5. def get_priority_queue_times(self):
  6. return sorted([entry.get_callback_time() for entry in self.pq.queue])
  7. # Add a TimeoutItem.
  8. # The added timeout expires at:
  9. # (clock's current time when self function is called) + (item's delay time)
  10. def add_timeout(self,
  11. delay_before_callback,
  12. callback):
  13. current_time = self.clock.get_time()
  14. callback_time = current_time + delay_before_callback
  15. new_timeout_item = TimeoutItem(callback_time, callback)
  16. self.pq.put(new_timeout_item)
  17. pass
  18. # Dequeues each expired TimeoutItem from the priority queue in order of their
  19. # priority and calls each expired item's callback function.
  20. def update(self):
  21. current_time = self.clock.get_time()
  22. while not self.pq.empty():
  23. next_item = self.pq.get()
  24. if next_item.get_callback_time() <= current_time:
  25. next_item.call_callback()
  26. else:
  27. self.pq.put(next_item)
  28. break

字符串

展开查看全部

相关问题