If you don't want, or can't, use AsyncTask, you can also create threads yourself:
// Declare some specificationsprivate class TaskSpec { TaskSpec() {} public String myString;}TaskSpec spec = new TaskSpec();spec.myString = "something";// Starting a threadMyTask task = new MyTask(spec);Thread myThread = new Thread(task);myThread.start();// This is the code that will run in the threadprivate class myTask implements Runnable { mySpec spec; boolean stopped; myTask(mySpec _spec) { spec = _spec; stopped = false; } public void stopTask() { stopped = true; } @Override public void run() { String myString = spec.myString; // ... int numTries = 0; while (numTries < MainActivity.MAX_TRIES && !stopped) { numTries++; // ... } }}You can synchronize using:
// Declare this in the activityprivate Object mLock; Then:
synchronized(mLock) { // Code that will be syncronized}