Last active
April 23, 2018 21:56
-
-
Save VitaliyBelyaev/b03af0e83e8c09a2cf62a3457da2a50a to your computer and use it in GitHub Desktop.
My HandlerThread implementation for Threading exercise in Android-Academy SPb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.vitaliybv.threadingexercise; | |
import android.os.Handler; | |
import android.os.HandlerThread; | |
import android.os.Message; | |
import java.util.ArrayList; | |
public class CounterTask extends HandlerThread { | |
private static final String WORKER_THREAD_NAME = CounterTask.class.getSimpleName(); | |
private Handler mWorkerHandler; | |
private Handler mResponseHandler; | |
private Callback mCallback; | |
private static final int COUNTER = 1; | |
public interface Callback { | |
void onCountIncrease(int number); | |
void onCountCompleted(String s); | |
} | |
public CounterTask(Handler responseHandler, Callback callback) { | |
super(WORKER_THREAD_NAME); | |
mResponseHandler = responseHandler; | |
mCallback = callback; | |
} | |
public void queueTask(ArrayList<Integer> numbers) { | |
mWorkerHandler.obtainMessage(COUNTER,numbers).sendToTarget(); | |
} | |
public void prepareHandler() { | |
mWorkerHandler = new Handler(getLooper(), new Handler.Callback() { | |
@Override | |
public boolean handleMessage(Message msg) { | |
ArrayList<Integer> numbers = (ArrayList<Integer>) msg.obj; | |
handleRequest(numbers); | |
try { | |
msg.recycle(); | |
} catch (IllegalStateException e) { | |
mWorkerHandler.removeMessages(msg.what); | |
} | |
return true; | |
} | |
}); | |
} | |
public void handleRequest(ArrayList<Integer> numbers) { | |
for (final Integer number : numbers) { | |
mResponseHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mCallback.onCountIncrease(number); | |
} | |
}); | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
mResponseHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
mCallback.onCountCompleted("DONE!"); | |
} | |
}); | |
} | |
public void cancelTask(){ | |
mWorkerHandler.removeMessages(COUNTER); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.vitaliybv.threadingexercise; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.Handler; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class ThreadsActivity extends AppCompatActivity implements CounterTask.Callback { | |
private Button mCreateButton; | |
private Button mStartButton; | |
private Button mCancelButton; | |
private TextView mCounterTextView; | |
private CounterTask mCounterTask; | |
private Boolean isRunning = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_threads); | |
mCreateButton = findViewById(R.id.thread_create); | |
mStartButton = findViewById(R.id.thread_start); | |
mCancelButton = findViewById(R.id.thread_cancel); | |
mCounterTextView = findViewById(R.id.tv_thread_counter); | |
mCreateButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mCounterTask == null) { | |
mCounterTask = new CounterTask(new Handler(), ThreadsActivity.this); | |
mCounterTask.start(); | |
mCounterTask.prepareHandler(); | |
mCounterTextView.setText("HandlerThread is created"); | |
} else { | |
Toast.makeText(getApplicationContext(), | |
"HandlerThread is already created ", | |
Toast.LENGTH_SHORT) | |
.show(); | |
} | |
} | |
}); | |
mStartButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mCounterTask != null && isRunning) { | |
Toast.makeText(getApplicationContext(), | |
"CounterTask is already running", | |
Toast.LENGTH_SHORT) | |
.show(); | |
} else if (mCounterTask != null) { | |
mCounterTask.queueTask(MainActivity.numbers); | |
isRunning = true; | |
} else { | |
Toast.makeText(getApplicationContext(), | |
"You need to create CounterTask first", | |
Toast.LENGTH_SHORT) | |
.show(); | |
} | |
} | |
}); | |
mCancelButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mCounterTask != null) { | |
mCounterTask.cancelTask(); | |
if (mCounterTask.getState() == Thread.State.RUNNABLE) { | |
Toast.makeText(getApplicationContext(), | |
"Message removed, but task is still running", | |
Toast.LENGTH_SHORT) | |
.show(); | |
} else { | |
mCounterTextView.setText("Task is canceled"); | |
} | |
} else { | |
Toast.makeText(getApplicationContext(), | |
"There is nothing to cancel", | |
Toast.LENGTH_SHORT) | |
.show(); | |
} | |
} | |
}); | |
} | |
@Override | |
protected void onDestroy() { | |
mCounterTask.quit(); | |
super.onDestroy(); | |
} | |
public static void start(Activity activity) { | |
Intent intent = new Intent(activity, ThreadsActivity.class); | |
activity.startActivity(intent); | |
} | |
@Override | |
public void onCountIncrease(int number) { | |
mCounterTextView.setText(String.valueOf(number)); | |
} | |
@Override | |
public void onCountCompleted(String s) { | |
mCounterTextView.setText(s); | |
isRunning = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment