Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: hstefan/ThreadingExample
base: 0597880c67
...
head fork: hstefan/ThreadingExample
compare: 24a0e23143
  • 3 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
16 src/com/hstefan/threadingexample/DotProductAsyncTask.java
View
@@ -1,29 +1,25 @@
package com.hstefan.threadingexample;
+import com.hstefan.threadingexample.task.Task;
+
import android.os.AsyncTask;
import android.util.Log;
-public class DotProductAsyncTask extends AsyncTask<Integer, Void, Float> {
+public class DotProductAsyncTask extends Task<Integer, Float> {
@Override
- protected Float doInBackground(Integer... index) {
+ public Float onRun(Integer... params) {
Float res = 0.f;
DotProductThreadingSingleton l_inst = DotProductThreadingSingleton.getInstance();
Float u[] = l_inst.getVectorU();
Float v[] = l_inst.getVectorV();
- assert(index.length == 2);
+ assert(params.length == 2);
assert(u != null && v != null);
- for(int i = index[0]; i < index[1]; ++i) {
+ for(int i = params[0]; i < params[1]; ++i) {
res += u[i]*v[i];
}
return res;
}
-
- @Override
- protected void onPostExecute(Float result) {
- Log.d("Thread", "Reached post execution state. Partial result: " + result);
- DotProductThreadingSingleton.getInstance().threadEnded(result);
- }
}
24 src/com/hstefan/threadingexample/DotProductThreadingSingleton.java
View
@@ -1,8 +1,10 @@
package com.hstefan.threadingexample;
import java.util.Calendar;
+import java.util.Observable;
+import java.util.Observer;
-public class DotProductThreadingSingleton {
+public class DotProductThreadingSingleton implements Observer {
static private DotProductThreadingSingleton _instance = null;
private long m_timer;
@@ -35,15 +37,6 @@ public synchronized void setNumThreads(int numThreads) {
m_runningThreads = m_numThreads;
}
- public synchronized void threadEnded(float partialRes) {
- m_runningThreads--;
- m_acc += partialRes;
- if(m_runningThreads == 0) {
- if(m_listener != null)
- m_listener.onDotProductCalculation(m_acc);
- }
- }
-
public void setOnDotProductCalculationListener(OnDotProductCalculationListener lst) {
m_listener = lst;
}
@@ -67,5 +60,16 @@ public synchronized void setVectorVVector(Float[] v) {
public interface OnDotProductCalculationListener {
void onDotProductCalculation(float res);
}
+
+ @Override
+ public void update(Observable observable, Object data) {
+ float partialResl = (Float) data;
+ m_runningThreads--;
+ m_acc += partialResl;
+ if(m_runningThreads == 0) {
+ if(m_listener != null)
+ m_listener.onDotProductCalculation(m_acc);
+ }
+ }
}
49 src/com/hstefan/threadingexample/ThreadingExampleActivity.java
View
@@ -4,15 +4,19 @@
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
+import java.util.Observable;
+import java.util.Observer;
import java.util.concurrent.Executor;
import com.hstefan.threadingexample.DotProductThreadingSingleton.OnDotProductCalculationListener;
import com.hstefan.threadingexample.db.ResultDbOpenHelper;
+import com.hstefan.threadingexample.task.Task;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
+import android.location.GpsStatus.Listener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.BaseColumns;
@@ -26,12 +30,11 @@
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
-public class ThreadingExampleActivity extends Activity implements VectorGenerationTask.OnTaskFinishedListener<Float[]>,
+public class ThreadingExampleActivity extends Activity implements Observer,
OnClickListener, OnDotProductCalculationListener {
/** Called when the activity is first created. */
private Float[] m_u, m_v;
- private List<AsyncTask<Integer, Void, Float>> m_Tasks;
private DotProductThreadingSingleton m_dotInstance;
private int m_numThreadsLRun;
private ResultDbOpenHelper m_resultsDbHelper;
@@ -65,8 +68,6 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
-
-
private void setupListView() {
m_resultsDbHelper = new ResultDbOpenHelper(this);
m_resultDb = m_resultsDbHelper.getWritableDatabase();
@@ -87,11 +88,8 @@ private void setupListView() {
private void setupDotTask() {
m_dotInstance = DotProductThreadingSingleton.getInstance();
m_dotInstance.setOnDotProductCalculationListener(this);
- m_Tasks = new ArrayList<AsyncTask<Integer,Void,Float>>();
}
-
-
private void setupViews() {
bRun = (Button)findViewById(R.id.bRun);
bRun.setClickable(false);
@@ -104,17 +102,12 @@ private void setupViews() {
setupListView();
}
-
-
private void generateVectors(int len) {
bRun.setClickable(false);
Toast.makeText(this, "Generating vector", Toast.LENGTH_LONG).show();
- new VectorGenerationTask().setOnFinishListener(this).execute(
- new VectorGenerationData(len, bRun, this));
+ new VectorGenerationTask().setParams(len).add_Observer(this).run();
}
-
-
@Override
protected void onDestroy() {
m_resultsDbHelper.getWritableDatabase().close();
@@ -122,18 +115,6 @@ protected void onDestroy() {
}
-
- @Override
- public void onTaskFinish(Float[][] result) {
- Toast.makeText(this, "Vector generated", Toast.LENGTH_LONG).show();
- bRun.setClickable(true);
- m_u = result[0];
- m_v = result[1];
- m_dotInstance.setVectorUVector(m_u);
- m_dotInstance.setVectorVVector(m_v);
- bRun.setClickable(true);
- }
-
@Override
public void onClick(View v) {
if(checkVectorLength()) {
@@ -171,15 +152,13 @@ private void calculateDotProduct(int numThreads) {
Log.d("MethodCall", "calculateDotProduct");
int sliceSz = m_u.length/numThreads;
int numSlices = m_u.length/sliceSz;
- Executor tExec = AsyncTask.THREAD_POOL_EXECUTOR;
m_dotInstance.setNumThreads(numThreads);
m_dotInstance.startTimer();
- m_Tasks.clear();
for(int slice = 0; slice < numSlices; ++slice) {
Log.d("Thread", "Spawning thread #" + slice);
assert((slice*sliceSz >= 0) && (slice*sliceSz + sliceSz <= m_u.length));
- m_Tasks.add(new DotProductAsyncTask().executeOnExecutor(tExec,
- slice*sliceSz, (slice*sliceSz) + sliceSz));
+ new DotProductAsyncTask().setParams(slice*sliceSz, (slice*sliceSz) +
+ sliceSz).add_Observer(m_dotInstance).run();
}
m_numThreadsLRun = numThreads;
}
@@ -200,4 +179,16 @@ public void onDotProductCalculation(float res) {
m_rAdapter.getCursor().requery();
}
+
+ @Override
+ public void update(Observable observable, Object data) {
+ Toast.makeText(this, "Vector generated", Toast.LENGTH_LONG).show();
+ bRun.setClickable(true);
+ Float[][] result = (Float[][]) data;
+ m_u = result[0];
+ m_v = result[1];
+ m_dotInstance.setVectorUVector(m_u);
+ m_dotInstance.setVectorVVector(m_v);
+ bRun.setClickable(true);
+ }
}
33 src/com/hstefan/threadingexample/VectorGenerationData.java
View
@@ -1,33 +0,0 @@
-package com.hstefan.threadingexample;
-
-import android.widget.Button;
-
-public class VectorGenerationData {
- private int m_aLen;
- private Button m_bRun;
- private VectorGenerationTask.OnTaskFinishedListener<Float[]> m_FinListener;
-
- public VectorGenerationData(int aLen, Button bRun,
- VectorGenerationTask.OnTaskFinishedListener<Float[]> finListener) {
- super();
- this.m_aLen = aLen;
- this.m_bRun = bRun;
- this.m_FinListener = finListener;
- }
-
- public int getVectorLength() {
- return m_aLen;
- }
-
- public Button getButton() {
- return m_bRun;
- }
-
- public void attachListener(VectorGenerationTask.OnTaskFinishedListener<Float[]> listener) {
- m_FinListener = listener;
- }
-
- public VectorGenerationTask.OnTaskFinishedListener<Float[]> getListener() {
- return m_FinListener;
- }
-}
24 src/com/hstefan/threadingexample/VectorGenerationTask.java
View
@@ -1,20 +1,16 @@
package com.hstefan.threadingexample;
+import com.hstefan.threadingexample.task.Task;
+
import android.os.AsyncTask;
import android.widget.Button;
-public class VectorGenerationTask extends AsyncTask<VectorGenerationData, Void, Float[][]> {
+public class VectorGenerationTask extends Task<Integer, Float[][]> {
private int m_numElements;
- private OnTaskFinishedListener<Float[]> m_finListener;
-
- public VectorGenerationTask setOnFinishListener(OnTaskFinishedListener<Float[]> listener) {
- this.m_finListener = listener;
- return this;
- }
@Override
- protected Float[][] doInBackground(VectorGenerationData... data) {
- m_numElements = data[0].getVectorLength();
+ public Float[][] onRun(Integer... params) {
+ m_numElements = params[0];
Float[][] ret = new Float[2][m_numElements];
for(int i = 0; i < m_numElements; ++i) {
@@ -24,14 +20,4 @@ public VectorGenerationTask setOnFinishListener(OnTaskFinishedListener<Float[]>
ret[1] = ret[0];
return ret;
}
-
- @Override
- protected void onPostExecute(Float[][] result) {
- if(m_finListener != null)
- m_finListener.onTaskFinish(result);
- }
-
- public interface OnTaskFinishedListener<T> {
- void onTaskFinish(Float[][] result);
- }
}
15 src/com/hstefan/threadingexample/task/Task.java
View
@@ -1,11 +1,12 @@
package com.hstefan.threadingexample.task;
import java.util.Observable;
+import java.util.Observer;
import android.os.AsyncTask;
public abstract class Task<ParamT, ResultT> extends Observable {
- private AsyncTask<ParamT, Void, ResultT> m_task;
+ private AsyncTask<ParamT, Void, ResultT> m_intTask;
private ParamT m_params[];
public abstract ResultT onRun(ParamT... params);
@@ -16,6 +17,7 @@
public InternalTask(Task<ParamT, ResultT> task, ParamT...params) {
m_params = params;
+ m_task = task;
}
@Override
@@ -25,7 +27,9 @@ protected ResultT doInBackground(ParamT... params) {
@Override
protected void onPostExecute(ResultT result) {
+ m_task.setChanged();
m_task.notifyObservers(result);
+ m_task.clearChanged();
}
}
@@ -35,7 +39,12 @@ protected void onPostExecute(ResultT result) {
}
public void run() {
- m_task = new InternalTask(this, m_params);
- m_task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, m_params);
+ m_intTask = new InternalTask(this, m_params);
+ m_intTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, m_params);
+ }
+
+ public Task<ParamT, ResultT> add_Observer(Observer o) {
+ addObserver(o);
+ return this;
}
}

No commit comments for this range

Something went wrong with that request. Please try again.