Java_自己写的线程队列类

此类可用单态获取
然后用postThread(Runnable runnable);不停的压入线程.
ThreadQueue会不停的一个一个的执行.



简单使用方法:

 
 
  1. ThreadQueue threadQueue=ThreadQueue.getThreadQueue();
  2. threadQueue.postThread(new Runnable(){
  3. public void run()
  4. {
  5. }
  6. });



下面是源码

 
 
  1. package src;
  2.  
  3. import java.security.PublicKey;
  4. import java.util.LinkedList;
  5.  
  6. public class ThreadQueue extends Thread
  7. {
  8. public static ThreadQueue sThreadQueue=null;
  9. private LinkedList<Runnable> mLink = null;
  10.  
  11. private Thread mThread = null;
  12.  
  13. private int mThreadState = 0;
  14.  
  15. private boolean mFroceWaitFlag = false;
  16.  
  17. private boolean mRuningFlag = false;
  18.  
  19.  
  20. public ThreadQueue()
  21. {
  22. mLink = new LinkedList<Runnable>();
  23. mThread = new Thread(this);
  24. mThreadState = PREPARE;
  25. }
  26.  
  27.  
  28. public void postThread(Runnable runnable)
  29. {
  30. dealAction(ACTION_POST, runnable);
  31. }
  32.  
  33.  
  34. private synchronized Runnable dealAction(int action, Runnable runnable)
  35. {
  36. Runnable runnable_1 = null;
  37.  
  38. switch (action)
  39. {
  40. case ACTION_POST:
  41.  
  42. if (runnable == null)
  43. {
  44. } else
  45. {
  46. post(runnable);
  47. }
  48.  
  49. break;
  50.  
  51. case ACTION_GET:
  52. if (mLink.isEmpty())
  53. {
  54. } else
  55. {
  56. runnable_1 = mLink.poll();
  57. }
  58.  
  59. break;
  60. }
  61.  
  62. return runnable_1;
  63. }
  64.  
  65.  
  66. private void post(Runnable runnable)
  67. {
  68. if (isWaiting())
  69. {
  70. mLink.offer(runnable);
  71. notify();
  72.  
  73. } else
  74. {
  75. mLink.offer(runnable);
  76.  
  77. }
  78.  
  79. }
  80.  
  81.  
  82. public void run()
  83. {
  84. while (mRuningFlag)
  85. {
  86.  
  87. if (!mFroceWaitFlag)
  88. {
  89.  
  90. mThreadState = DEALING;
  91. if (!mLink.isEmpty())
  92. {
  93. mThreadState = DEALING;
  94. Runnable runnable = dealAction(ACTION_GET, null);
  95. runnable.run();
  96.  
  97. } else
  98. {
  99. mThreadState = WAITING;
  100. synchronized (this)
  101. {
  102. try
  103. {
  104. wait();
  105. } catch (InterruptedException e)
  106. {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
  111.  
  112. } else
  113. {
  114.  
  115. mThreadState = FORCEWAITING;
  116. synchronized (this)
  117. {
  118. try
  119. {
  120. wait();
  121. } catch (InterruptedException e)
  122. {
  123. e.printStackTrace();
  124. }
  125. }
  126. }
  127. }
  128.  
  129. mThreadState = DESTROY;
  130.  
  131. }
  132.  
  133.  
  134. public Thread getThread()
  135. {
  136. return mThread;
  137. }
  138.  
  139.  
  140. public boolean isPrepare()
  141. {
  142. if (mThreadState == PREPARE)
  143. {
  144. return true;
  145. }
  146.  
  147. return false;
  148.  
  149. }
  150.  
  151.  
  152. public boolean isRuning()
  153. {
  154. if (mRuningFlag)
  155. {
  156. return true;
  157. }
  158.  
  159. return false;
  160. }
  161.  
  162.  
  163. public boolean isDealing()
  164. {
  165. if (mThreadState == DEALING)
  166. {
  167. return true;
  168. }
  169.  
  170. return false;
  171. }
  172.  
  173.  
  174. public boolean isWaiting()
  175. {
  176. if (mThreadState == WAITING)
  177. {
  178. return true;
  179. }
  180.  
  181. return false;
  182. }
  183.  
  184.  
  185. public boolean isDestroyed()
  186. {
  187. if (mThreadState == DESTROY)
  188. {
  189. return true;
  190. }
  191.  
  192. return false;
  193. }
  194.  
  195.  
  196. public void start()
  197. {
  198. if (isPrepare())
  199. {
  200. mRuningFlag = true;
  201. mThread.start();
  202. }
  203. }
  204.  
  205.  
  206. public void destroy()
  207. {
  208. if (isRuning())
  209. {
  210. mRuningFlag = false;
  211. if(mThreadState==WAITING||mThreadState==FORCEWAITING)
  212. {
  213. synchronized (this)
  214. {
  215. notify();
  216. }
  217. }
  218. }
  219. }
  220.  
  221.  
  222. public void forceWait()
  223. {
  224. if (!mFroceWaitFlag)
  225. {
  226. mFroceWaitFlag = true;
  227. }
  228. }
  229.  
  230.  
  231. public void forceRuning()
  232. {
  233. if (mFroceWaitFlag)
  234. {
  235. mFroceWaitFlag = false;
  236.  
  237. if (mThreadState == FORCEWAITING)
  238. {
  239. synchronized (this)
  240. {
  241. notify();
  242. }
  243.  
  244. }
  245. }
  246. }
  247.  
  248. public static final int ACTION_POST = 0x20001;
  249. public static final int ACTION_GET = 0x20002;
  250.  
  251. public static final int PREPARE = 0x30001;
  252. public static final int DEALING = 0x30002;
  253. public static final int WAITING = 0x30003;
  254. public static final int FORCEWAITING = 0x30004;
  255. public static final int DESTROY = 0x30004;
  256.  
  257. public static ThreadQueue getThreadQueue()
  258. {
  259. if(sThreadQueue==null)
  260. {
  261. sThreadQueue=new ThreadQueue();
  262. sThreadQueue.start();
  263. }
  264. return sThreadQueue;
  265. }
  266. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的 Java线程队列实例,模拟队列实现排队叫号的过程: ```java import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class QueueExample { private Queue<Integer> queue = new LinkedList<>(); private Lock lock = new ReentrantLock(); private int currentTicketNumber = 0; public void addCustomer() { lock.lock(); try { queue.offer(currentTicketNumber++); System.out.println("新顾客取得号码:" + (currentTicketNumber - 1) + ",当前队列长度:" + queue.size()); } finally { lock.unlock(); } } public void serveCustomer() { lock.lock(); try { Integer ticketNumber = queue.poll(); if (ticketNumber == null) { System.out.println("队列为空,无法叫号"); } else { System.out.println("叫到号码:" + ticketNumber + ",当前队列长度:" + queue.size()); } } finally { lock.unlock(); } } public static void main(String[] args) { final QueueExample queueExample = new QueueExample(); // 开启 5 个线程模拟 5 个顾客取号 for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { queueExample.addCustomer(); } }).start(); } // 开启 3 个线程模拟 3 个服务员叫号 for (int i = 0; i < 3; i++) { new Thread(new Runnable() { @Override public void run() { queueExample.serveCustomer(); } }).start(); } } } ``` 这个例子中,我们定义了一个 `QueueExample` ,其中有一个 `queue` 队列,用于存储所有顾客的票号。我们使用 `ReentrantLock` 来实现线程安全。在 `addCustomer` 方法中,我们通过 `offer` 方法往队列中添加一个顾客的票号,并输出当前队列长度以及新顾客的票号。在 `serveCustomer` 方法中,我们通过 `poll` 方法从队列中获取一个顾客的票号,并输出当前队列长度以及叫到的顾客的票号。 在 `main` 方法中,我们开启了 5 个线程模拟 5 个顾客取号,以及 3 个线程模拟 3 个服务员叫号。你可以运行这个例子来看一下具体的输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值