Is it possible to resume an interrupted Thread in Android?
You shouldn't resume Thread by its API, resume() method is depracated (reason).
You can simulate resuming Thread by killing it and starting a new one:
/**
Since Thread can't be paused we have to simulate pausing.
We will create and start a new thread instead.
*/
public class ThreadManager
{
private static GameThread gameThread = new GameThread();
public static void setRunning(boolean isRunning)
{
if (isRunning)
{
gameThread = new GameThread();
gameThread.setRunning(true);
gameThread.start();
}
else
{
gameThread.setRunning(false);
}
}
public static boolean isRunning()
{
return gameThread.isRunning();
}
public static void join() throws InterruptedException
{
gameThread.join();
}
}
Thread does not support these actions as related methods are deprecated.
suspend()
resume()
Related
I have tried some ways to do it and didn't secced.
I have MainActivity which start 3 thread. I want to stop the threads when the user press "back" bottum or when from some reason the app stop (phone call for example).
and after the activity again seen (when the user come back to the app) the thread will continue from where they stop.
All the threads defined in MainActivity and there they start.
Thanks!
public class MainActivity extends Activity
{
//threads
private PingPongGame gameThread;
private PaddleMover paddleMoverThread;
private PresentThread giftThread;
public GameSounds gameSounds;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
gameLevel = new GameLevel0(screenWidth , screenHeight, this.giftArr);
gameLevelView = new GameLevelView(this,gameLevel);
// Creating the game view
this.gameView = new PingPongView(this);
// Setting the gameView as the main view for the PingPong activity.
setContentView(gameView);
if(gameThread == null){
//create the main thread
gameThread = new PingPongGame( gamePaddle, gameView, gameLevel , message , ballArr , gameSounds);
//create the thread responsible for moving the paddle
paddleMoverThread = new PaddleMover(gamePaddle, gameView);
//create the thread responsible for present
giftThread = new PresentThread(gamePaddle , gameView , gameLevel, message , giftArr , ballArr,gameSounds );
gameThread.start();
paddleMoverThread.start();
giftThread.start();
}
}
//This method is automatically called when the user touches the screen
#Override
public boolean onTouchEvent(MotionEvent event)
{
float destination;
// Toast.makeText(this, "try!", Toast.LENGTH_SHORT).show();
//get the x coordinate of users' press
destination = event.getX();
//notify the paddle mover thread regarding the new destination
gamePaddle.setPaddleDestination(destination);
return true;
}
}
Example of one of my threads:
public class PaddleMover extends Thread
{
private Tray gamePaddle; //holds a reference to the paddle
private PingPongView gameView; //holds a reference to the main view
//for stop
private Object mPauseLock;
private boolean mPaused;
//initialize class variables
public PaddleMover(Tray thePaddle, PingPongView mainView)
{
gamePaddle = thePaddle;
gameView = mainView;
//for stop and resume threads
mPauseLock = new Object();
mPaused = false;
}
//main method of the current thread
#Override
public void run()
{
//infinitely loop, and move the paddle if necessary
while ((Const.isLose == false) && (Const.isCompleteThisLevel==false) && (Const.isDestroy == false))
{
//check whether the paddle should be moved
if (gamePaddle.getMiddle() != gamePaddle.getPaddleDestination())
{
//move the paddle
gamePaddle.move();
//send a request to refresh the display
gameView.postInvalidate();
}
try
{
PaddleMover.sleep(3);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//for stop and resume
synchronized (mPauseLock) {
while (mPaused) {
try {
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
}
/**
* Call this on pause.
*/
public void onPause() {
synchronized (mPauseLock) {
mPaused = true;
}
}
/**
* Call this on resume.
*/
public void onResume() {
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}
}
You can achieve a lot better control with your threads using Runnable objects. Instead of defining your logic using a loop within the run() method, do the following:
Define your framewise logic for each thread within Runnable objects. Override the run() method. Do not use a while loop within your Runnable.
Create a variable which tracks whether or not your game is paused. Use this variable to create a single while() loop within a main thread.
Define a Thread class and get it's Handler. You can do this like so:
class WorkerThread extends Thread
{
private volatile Handler mHandler;
//volatile so you can try to acquire it until it is instantiated
#Override
public void run()
{
//This is pretty much boilerplate for worker thread implementations
Looper.prepare();
//Handlers must be instantiated by their respective threads
mHandler = new Handler();
Looper.loop();
}
#Override
public Handler getHandler()
{
return mHandler;
}
}
Instantiate multiple WorkerThreads and acquire references to their Handler objects.
In each frame, pass the Runnable() which defines the logic you want the Thread to execute to the Handler using the postRunnable() method.
Use ConditionVariable objects to make sure you don't call the postRunnable() method while the WorkerThread is still executing.
Runnable thread1runnable = new Runnable()
{
#Override
public void run()
{
//Do your logic here
...
thread1finished.open(); //This lets the block() function return
}
}
ConditionVariable thread1finished = new ConditionVariable();
thread1finished.open(); //Make sure the loop doesn't block the first time through
Thread thread1 = new WorkerThread();
//Start the thread to acquire the handler and prepare it for looping
thread1.start();
//Call stop() when shutting down your game, not pausing
Handler thread1handler;
while (thread1handler != null)
thread1handler = thread1.getHandler();
//A crude way of making sure you acquire the instantiated handler, you can top this
while (gameRunning)
{
if (!isPaused) //Stops work when your isPaused variable is set
{
thread1finished.block(); //Wait for the last runnable to finish
//Lock the ConditionVariable so the loop will block
thread1finished.close();
//Signal the worker thread to start crunching
thread1handler.postRunnable(thread1runnable);
}
}
In your onPause() override, set the variable that stops your while() loop from posting the Runnable objects to your WorkerThreads. Enjoy automatic starting and stopping!
There may be issues adapting this method to three different threads which you don't want to synchronize frame-per-frame. Let me know how it goes.
I want to make stopwatch. And i create stopwatch class like this. And when i call onPause in another Activity its freeze application.
public class StopWatch implements Runnable {
private Object mPauseLock;
private boolean mPaused;
private boolean mFinished;
private ArrayList<TextView> textFields;
private Handler mHandler = new Handler();
public StopWatch( ArrayList<TextView> textFields) {
mPauseLock = new Object();
mPaused = false;
mFinished = false;
this.textFields =textFields;
}
public void run() {
textFields.get(1).setText("progressing...");
if (!mPaused) {
mHandler.postDelayed(this, 1000);
}
synchronized (mPauseLock) {
while (mPaused) {
try {
mPauseLock.wait();
} catch (InterruptedException e) {
}
}
}
}
public void onPause() {
synchronized (mPauseLock) {
mPaused = true;
}
}
public void onResume() {
synchronized (mPauseLock) {
mPaused = false;
mPauseLock.notifyAll();
}
}
}
and i create instance of class in another View like. Can somebody exmplain me where is problem?
stopky = new StopWatch(textFields);
stopky.run();
// do another stuff and register buttons with onClickListener and call
stopky.onPause(); // freeze application
stopky.onResume();
You can't Object.wait() in a run method called from a Handler, which is probably running on the main/UI Thread.
The whole Android app is coordinated via short methods which register with the main/UI Thread. You're probably registering your stopwatch there, too. It's not possible to perform a while loop there and at the same time process events from the user interface..
A quick solution would be to re-schedule your run method and check the status the next time it gets called. Basically like so:
public void run() {
textFields.get(1).setText("progressing...");
if (!mPaused) {
// do what has to be done when stopwatch is running
mHandler.postDelayed(this, 1000);
} else {
// just re-schedule with a shorter delay
mHandler.postDelayed(this, 10);
}
}
An even better way would be to go for a fully event-driven design and avoid calling the stopwatch at all while it is stopped. In this case, you would simply re-start it from the Button's event handler.
I have a question related to the following link: What's the difference between Thread start() and Runnable run()
In this question, I see a person creating runnable objects and then initializing them in two different ways. So, does this mean that you could pass these runnables around to other things at run time?
I want to pass code to a preexisting thread to be executed within that thread's loop. I was looking around and from what I can tell, you would want to create a dedicated runnable class like the following:
public class codetobesent implements Runnable
{
public void run()
{
..morecodehere.
}
...insertcodestuffhere
}
But how would I pass this to a thread that is already running? Say I'm trying to make a game and I have something special I want the renderer to do in its thread. How would I pass this runnable to that thread and have it run this data correctly?
My current implementation of my rendering thread is the following, I pulled it off of a tutorial site, and it has worked pretty well so far. But I want to know how to pass things to it so I can run more than what's just in the preset loop.
class RenderThread extends Thread
{
private SurfaceHolder _curholder;
private UserView curview;
private boolean runrender = false;
public RenderThread (SurfaceHolder holder, UserView thisview)
{ //Constructor function - This gets called when you create a new instance of this object.
curview = thisview;
_curholder = holder;
}
public SurfaceHolder getThreadHolder()
{
return _curholder;
}
public void setRunning(boolean onoff)
{
runrender = onoff;
}
#Override
public void run()
{
Canvas c;
while (runrender)
{
c = null; //first clear the object buffer.
try
{
c = _curholder.lockCanvas(null); //lock the canvas so we can write to it
synchronized (_curholder)
{//we sync the thread with the specified surfaceview via its surfaceholder.
curview.onDraw(c);
}
}
finally
{
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null)
{
_curholder.unlockCanvasAndPost(c);
}
}
}
}
}
A Handler Thread implementation.
private void testWorker(){
WorkerThread worker = new WorkerThread();
worker.start();
for (int i = 0; i < 10; i++) {
worker.doRunnable(new Runnable() {
public void run() {
Log.d("demo", "just demo");
try {
Thread.sleep(1000);//simulate long-duration operation.
} catch (InterruptedException e) {
e.printStackTrace();
}
};
});
}
}
private class WorkerThread extends HandlerThread implements Callback {
private Handler mHandler;
public WorkerThread() {
super("Worker");
}
public void doRunnable(Runnable runnable) {
if (mHandler == null) {
mHandler = new Handler(getLooper(), this);
}
Message msg = mHandler.obtainMessage(0, runnable);
mHandler.sendMessage(msg);
}
#Override
public boolean handleMessage(Message msg) {
Runnable runnable = (Runnable) msg.obj;
runnable.run();
return true;
}
}
I am writing a game in which after a specified amount of time a thread must be stopped.The user has failed to complete a particular level.I am using a thread.How do i stop this thread after a specified amount of time and display another view.How do i do this.The following code delays the launching of the thread by timelimit.
Thread t = ... t.join(timelimit);
if (t.isAlive)
t.interrupt();
How do i run the thread and close it after a period of time.
Your working thread
public class Worker extends Thread {
private boolean isRunning = true;
public void run() {
while (isRunning) {
/* do your stuff here*/
}
}
public void stopWorker() {
isRunning = false;
}
}
Your stopping thread
public class Stopper extends Thread {
private Worker worker;
public void Stopper(Worker w) {
worker = w;
}
public void run() {
// wait until your timeout expires
worker.stopWorker();
}
}
you should declare your thread with something like this
public class GameLoopThread extends Thread{
private boolean running = false;
public void setRunning(boolean run){
running = run;
}
#Override
public void run(){
while(running){
}
}
}
This is the safer way, In order to stop you should set the running variable to false. Otherwise If you stop the thead you will get an android exception .
I prefer interrupting the Thread from outside and checking interrupted state in short intervals:
try {
while (!Thread.currentThread.interrupted()) {
doSth();
}
} catch (InterruptedException e) {
// finished
}
you can use below code is run after given specified time.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// after this is rung
}
}, 5000);
My service spawns a new thread, and stops it according to the typically recommended java method of interrupt()'ing. When I stop the service, I stop the thread in onDestroy(). The service is stopped, and the interrupt code is reached. However, soon enough the thread restarts from the beginning of the Runnable.
public class DoScan extends Service {
public volatile Thread runner;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
startThread();
}
#Override
public void onDestroy() {
super.onDestroy();
android.util.Log.v("####################", "DoScan.onDestroy");
stopThread();
}
public synchronized void startThread(){
if(runner == null){
android.util.Log.v("####################", "DoScan.startthread");
runner = new Thread(new ScanningThread());
runner.start();
}
}
/* use a handler in a loop cycling through most of oncreate.
* the scanningthread does the work, then notifies the svc's uithread
*/
public synchronized void stopThread(){
if(runner != null){
android.util.Log.v("####################", "DoScan.stopthread");
Thread moribund = runner;
runner = null;
moribund.interrupt();
android.util.Log.v("####################", "interrupted?" + moribund.isInterrupted());
}
}
}
I think the safest way is to have a flag so the thread checks for it inside its main loop.
class ScanningThread extends Thread {
// Must be volatile:
private volatile boolean stop = false;
public void run() {
while (!stop) {
System.out.println("alive");
}
if (stop)
System.out.println("Detected stop");
}
public synchronized void requestStop() {
stop = true;
}
}
public synchronized void startThread(){
if(runner == null){
android.util.Log.v("####################", "DoScan.startthread");
runner = new ScanningThread();
runner.start();
}
}
public synchronized void stopThread(){
if(runner != null){
android.util.Log.v("####################", "DoScan.stopthread");
runner.requestStop();
runner = null;
}
}
The problem is that your thread needs to cooperate by periodically checking for interruption and exiting if the thread has been interrupted. Unless you place something along the lines of the following in your thread...
// Processing...
if ( Thread.interrupted() ){
return;
}
// More processing...
try{
Thread.sleep(sleeptime);
}catch(InterruptedException interrupt){
return;
}
// Rinse and repeat...
...your thread will just ignore the fact that it has been interrupted. The method proposed by Lucas S. is essentially identical, except that using interruption will generate an exception if the thread is blocked, whereas under Lucas S.'s method, you might have to wait indefinitely for the thread to quit.
Interrupting a thread throws an exception in the thread, it does not necessarily stop it. You should catch that exception and then do the clean up in thread before exiting (provided, you need to exit!).