Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4.  
  5. public class MyExecutor implements Executor {
  6. private BlockingQueue<Runnable> queue;
  7. private Thread[] threadPool;
  8.  
  9. public MyExecutor(int queueSize, int poolSize) {
  10. this.queue = new LinkedBlockingQueue<Runnable>(queueSize);
  11. this.threadPool = new Thread[poolSize];
  12. }
  13.  
  14. public MyExecutor() {
  15. this(1000, 5);
  16. }
  17.  
  18.  
  19. public void execute(Runnable r) {
  20. try {
  21. queue.put(r);
  22. } catch (InterruptedException ex) {
  23. ex.printStackTrace();
  24. }
  25. if (threadPool[0] == null) {
  26. int i = 0;
  27. while(i < threadPool.length) {
  28. threadPool[i] = new Thread(new Worker(queue));
  29. threadPool[i].start();
  30. i++;
  31. }
  32. }
  33. }
  34.  
  35. /*public void shutdown() {
  36. int i = 0;
  37. while(i < threadPool.length) {
  38. try {
  39. queue.put(new Killer());
  40. } catch (InterruptedException ex) {
  41. ex.printStackTrace();
  42. }
  43. i++;
  44. }
  45. }
  46. */
  47.  
  48. public int getMaxPendingTime() {
  49. int i = 0;
  50. int max = 0;
  51. while(i < queue.size()) {
  52. //if (queue.iterator().hasNext()) {
  53. //int d = queue.iterator().next().getDuration();
  54. max += 1000;
  55. i++;
  56. }
  57. return max;
  58. }
  59. }
  60.  
  61. class Worker implements Runnable {
  62. private BlockingQueue<Runnable> queue;
  63.  
  64. public Worker(BlockingQueue<Runnable> queue) {
  65. this.queue = queue;
  66. }
  67.  
  68. public void run() {
  69. while (true) {
  70. Runnable r = null;
  71. try {
  72. r = queue.take();
  73. /*if(r.class == Killer) {
  74. return;
  75. }
  76. */
  77. } catch (InterruptedException ex) {
  78. ex.printStackTrace();
  79. }
  80. r.run();
  81. }
  82. }
  83. }
  84.  
  85. /*class Killer implements Runnable {
  86. public void run() {}
  87. }
  88. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement