如何转换 extends Thread Class 以实现 Runnable
How To Convert extends Thread Class to Implement Runnable
我在网上找到了一些用于创建拼图的示例代码,我注意到它们有一个 Thread class 扩展 Thread 并执行诸如监视拼图状态甚至设置表面视图属性等操作用于拼图。
我想通过实现 运行nable 将这段代码移到 运行() 中。我读过实现 运行nable 比扩展 Thread 更好,但我不知道如何实现。下面是从自定义表面视图调用的 class。我想在这个表面视图 class 中实现 运行nable(我认为这很好)。提前致谢。
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();
}
}
在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();
这应该可以解决问题![=16=]
我在网上找到了一些用于创建拼图的示例代码,我注意到它们有一个 Thread class 扩展 Thread 并执行诸如监视拼图状态甚至设置表面视图属性等操作用于拼图。
我想通过实现 运行nable 将这段代码移到 运行() 中。我读过实现 运行nable 比扩展 Thread 更好,但我不知道如何实现。下面是从自定义表面视图调用的 class。我想在这个表面视图 class 中实现 运行nable(我认为这很好)。提前致谢。
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();
}
}
在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();
这应该可以解决问题![=16=]