此类可用单态获取
然后用postThread(Runnable runnable);不停的压入线程.
ThreadQueue会不停的一个一个的执行.
简单使用方法:
- ThreadQueue threadQueue=ThreadQueue.getThreadQueue();
- threadQueue.postThread(new Runnable(){
- public void run()
- {
- }
- });
下面是源码
- package src;
- import java.security.PublicKey;
- import java.util.LinkedList;
- public class ThreadQueue extends Thread
- {
- public static ThreadQueue sThreadQueue=null;
- private LinkedList<Runnable> mLink = null;
- private Thread mThread = null;
- private int mThreadState = 0;
- private boolean mFroceWaitFlag = false;
- private boolean mRuningFlag = false;
- public ThreadQueue()
- {
- mLink = new LinkedList<Runnable>();
- mThread = new Thread(this);
- mThreadState = PREPARE;
- }
- public void postThread(Runnable runnable)
- {
- dealAction(ACTION_POST, runnable);
- }
- private synchronized Runnable dealAction(int action, Runnable runnable)
- {
- Runnable runnable_1 = null;
- switch (action)
- {
- case ACTION_POST:
- if (runnable == null)
- {
- } else
- {
- post(runnable);
- }
- break;
- case ACTION_GET:
- if (mLink.isEmpty())
- {
- } else
- {
- runnable_1 = mLink.poll();
- }
- break;
- }
- return runnable_1;
- }
- private void post(Runnable runnable)
- {
- if (isWaiting())
- {
- mLink.offer(runnable);
- notify();
- } else
- {
- mLink.offer(runnable);
- }
- }
- public void run()
- {
- while (mRuningFlag)
- {
- if (!mFroceWaitFlag)
- {
- mThreadState = DEALING;
- if (!mLink.isEmpty())
- {
- mThreadState = DEALING;
- Runnable runnable = dealAction(ACTION_GET, null);
- runnable.run();
- } else
- {
- mThreadState = WAITING;
- synchronized (this)
- {
- try
- {
- wait();
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- } else
- {
- mThreadState = FORCEWAITING;
- synchronized (this)
- {
- try
- {
- wait();
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- mThreadState = DESTROY;
- }
- public Thread getThread()
- {
- return mThread;
- }
- public boolean isPrepare()
- {
- if (mThreadState == PREPARE)
- {
- return true;
- }
- return false;
- }
- public boolean isRuning()
- {
- if (mRuningFlag)
- {
- return true;
- }
- return false;
- }
- public boolean isDealing()
- {
- if (mThreadState == DEALING)
- {
- return true;
- }
- return false;
- }
- public boolean isWaiting()
- {
- if (mThreadState == WAITING)
- {
- return true;
- }
- return false;
- }
- public boolean isDestroyed()
- {
- if (mThreadState == DESTROY)
- {
- return true;
- }
- return false;
- }
- public void start()
- {
- if (isPrepare())
- {
- mRuningFlag = true;
- mThread.start();
- }
- }
- public void destroy()
- {
- if (isRuning())
- {
- mRuningFlag = false;
- if(mThreadState==WAITING||mThreadState==FORCEWAITING)
- {
- synchronized (this)
- {
- notify();
- }
- }
- }
- }
- public void forceWait()
- {
- if (!mFroceWaitFlag)
- {
- mFroceWaitFlag = true;
- }
- }
- public void forceRuning()
- {
- if (mFroceWaitFlag)
- {
- mFroceWaitFlag = false;
- if (mThreadState == FORCEWAITING)
- {
- synchronized (this)
- {
- notify();
- }
- }
- }
- }
- public static final int ACTION_POST = 0x20001;
- public static final int ACTION_GET = 0x20002;
- public static final int PREPARE = 0x30001;
- public static final int DEALING = 0x30002;
- public static final int WAITING = 0x30003;
- public static final int FORCEWAITING = 0x30004;
- public static final int DESTROY = 0x30004;
- public static ThreadQueue getThreadQueue()
- {
- if(sThreadQueue==null)
- {
- sThreadQueue=new ThreadQueue();
- sThreadQueue.start();
- }
- return sThreadQueue;
- }
- }