【问题标题】:How To Convert extends Thread Class to Implement Runnable如何将 extends Thread 类转换为实现 Runnable
【发布时间】:2015-02-13 01:49:35
【问题描述】:

我在网上找到了一些用于创建拼图的示例代码,我注意到他们有一个 Thread 类,它扩展了 Thread 并执行诸如监视拼图状态甚至设置用于拼图的表面视图的属性之类的事情.

我想通过实现 Runnable 将此代码移动到 Run() 中。我读过实现 Runnable 比扩展 Thread 更好,但我不知道如何。下面是从自定义表面视图调用的类。我想在这个表面视图类中实现 Runnable(我认为这很好)。提前致谢。

public class ThreadHelper extends Thread {

    private SurfaceHolder mHolder;
    private PuzzleSurface mPuzzleSurface;
    private ThreadHelper mThread;

    private int gameState;
    private int puzzleSurfaceWidth = 1;
    private int puzzleSurfaceHeight = 1;
    public static final int STATE_PAUSE = 1;
    public static final int STATE_RUNNING = 2;
    private boolean isRunning = false;

    public ThreadHelper(SurfaceHolder holder, PuzzleSurface puzzleSurface) {
        mHolder = holder;
        mPuzzleSurface = puzzleSurface;
    }

    /**
     * Return running thread
     * @return
     */
    public ThreadHelper getThread() {
        return mThread;
    }

    /**
     * Stop running thread
     */
    public void destroy() {
        synchronized (mPuzzleSurface) {
            if (isRunning) {
                isRunning = false;
            }

            if (mThread != null) {
                mThread.interrupt();
                mThread = null;
            }
        }
    }

    /**
     * Method pauses game state
     */
    public void pause() {
        synchronized (mPuzzleSurface) {
            if (gameState == STATE_RUNNING) {
                setState(STATE_PAUSE);
            }
        }
    }

    /**
     * Method sets game state to running
     */
    public void unpause() {
        setState(STATE_RUNNING);
    }

    /**
     * Method is used to set the game state to either paused (1)
     * or running (2) states
     * @param stateToSet
     */
    public void setState(int stateToSet) {
        synchronized (mPuzzleSurface) {
            // TODO Message Handling
        }
    }

    /**
     *  Called to retrieve per-instance state from an activity 
     *  before being killed
     * @param outState
     * @return outState instance
     */
    public Bundle saveState(Bundle outState) {
        synchronized (mPuzzleSurface) {
            if (outState != null) {

            }
        }
        return outState;
    }

    /**
     * Called to set the size of the surfaceView
     * @param width
     * @param height
     */
    public void setSurfaceSize(int width, int height) {
        // synchronized to make sure these all change atomically
        synchronized (mPuzzleSurface) {
            puzzleSurfaceWidth = width;
            puzzleSurfaceHeight = height;
        }
    }

    /* (non-Javadoc)
     * @see java.lang.Thread#getState()
     */
    @Override
    public State getState() {
        // TODO Auto-generated method stub
        return super.getState();
    }

    /**
     * Method used to set running state of thread
     * @param run
     */
    public void setRunning(boolean run) {
        isRunning = run;
    }

    /* (non-Javadoc)
     * @see java.lang.Thread#run()
     * Calls the run() method of the Runnable object the 
     * receiver holds
     */
    @SuppressLint("WrongCall")
    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (isRunning) {
            Canvas c = null;
            try {
                c = mHolder.lockCanvas(null);
                synchronized (mHolder) {
                    mPuzzleSurface.onDraw(c);
                }
            } finally {
                if (c != null) {
                    mHolder.unlockCanvasAndPost(c);
                }
            }

        }
        super.run();
    }

}

【问题讨论】:

    标签: android multithreading runnable


    【解决方案1】:

    ThreadHelper.java更改:

    public class ThreadHelper extends Thread {
    

    到:

    public class ThreadHelper implements Runnable {
    

    然后在您创建和启动ThreadHelper 的地方,更改所有看起来或多或少像这样的代码:

    ThreadHelper threadHelper = new ThreadHelper(holder, puzzleSurface);
    threadHelper.start();
    

    到这里:

    ThreadHelper threadHelper = new ThreadHelper(holder, puzzleSurface);
    Thread thread = new Thread(threadHelper);
    thread.start();
    

    这应该可以解决问题!

    【讨论】:

    • 让我试试这个。看起来很简单。 2 分钟。
    猜你喜欢
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2012-02-15
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多