当前位置: 首页 > 知识库问答 >
问题:

如何将extends Thread类转换为实现Runnable

云利
2023-03-14
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();
    }

}

共有1个答案

羊越
2023-03-14

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();
 类似资料:
  • 我已经编写了一个静态编程语言FirebasRecyclerAdapter,它作为我的主要活动的一部分工作得很好。但是,我希望将此代码放在单独的Main Adapter.kt文件/类中。我该怎么做呢?

  • 问题内容: 我有一个想要表示为C#类的复杂JSON对象。我在名为“ Form”的父类上有一个良好的开端,但是如何代表不同类型的集合(请参见下面的“ elements”对象)? 这是JSON对象: 我开始的课程看起来像这样: 如何处理“ elements”属性以获得所需的JSON输出? 我正在WCF 4.0和web.config中的以下属性一起使用:automaticFormatSelectionE

  • 问题内容: 我只想知道是否有一个程序可以像 JAXB for Java那样将XSD文件转换为Python类? 问题答案: generateDS:我认为这是我需要的好工具 编辑:实际上,generateDS做得很好!它使用所有方法(setter和getter,导出到XML,从XML导入)生成Python类。效果很好!

  • 问题内容: 我正在使用像XML dateTime类型的“ 2011-02-27T10:03:33.099-06:00”这样的字符串来获取时间。我也有TimeZone类型的时区。我应该如何在该时区将dateTime转换为GregorianCalendar java类型。 问题答案: Java内置了用于解析xml日期时间的代码:使用DatatypeConverter.parseDateTime()。这

  • 问题内容: 如何将reflect.Value转换为其类型? 谢谢! 问题答案: 好,我找到了 具有将其转换为

  • 问题内容: 我想知道如何将某些函数返回的父对象转换为子类。 我不能更改A类。如果可以的话,我可以对A类实现functionIneed,但是由于代码的结构,这是不可能的。 问题答案: Python不支持“广播”。您将需要编写代码,以便它可以适当地进行初始化。