Reputation: 169
I have an application in which i have 3-4 threads , i want that 2nd thread must start when 1st thread ends , 3rd when 2nd ends and so on.. I am giving the code..
//1st thread
new Thread(new Runnable() {
int progressStatus = 0;
public void run() {
progressStatus = dofirstWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
handler.post(new Runnable() {
public void run() {
bar.setProgress(progressStatus);
}
});
}
private int dofirstWork() {
//do some work
return 25;
}
}).start();
//2nd thread
new Thread(new Runnable() {
int progressStatus = 0;
public void run() {
progressStatus = dosecondWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
handler.post(new Runnable() {
public void run() {
bar.setProgress(progressStatus);
}
});
}
private int dosecondWork() {
//do some work
return 50;
}
}).start();
//3rd thread
new Thread(new Runnable() {
int progressStatus = 0;
public void run() {
progressStatus = dothirdWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
handler.post(new Runnable() {
public void run() {
bar.setProgress(progressStatus);
}
});
}
private int dothirdWork() {
//do some work
return 75;
}
}).start();
Actually , i m trying to display progress bar at 25% for 1st function call, at 50 % for second operation and so .. on.. plz help
Upvotes: 0
Views: 263
Reputation: 4638
package com.integrated.mpr;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.stat.correlation.Covariance;
import org.apache.commons.math.stat.correlation.PearsonsCorrelation;
import org.apache.commons.math.util.FastMath;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Logic extends Activity{
int n = Choose.n;
double final_matrix[][] = new double[n][5];
double swap =0;
double weightage_matrix[] = {0,0,0,0,0,0,0,0,0,0,0};
double sorted_weightage[] = {0,0,0,0,0,0,0,0,0,0};
String display[] = new String[n];
double input_matrix[][] = new double[22050][n];
double[] peak_matrix = new double[n];
double[] sd_matrix = new double[n];
double[] rms_matrix = new double[n];
double[] cf_matrix = new double[n];
double[] mean_matrix = new double[n];
int[] sensitive_positions = new int[n];
double[] new_sensitive = new double[n];
int[] sortsensi = new int[n];
private static final int Progress = 0;
ProgressBar bar;
TextView label;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
bar = (ProgressBar) findViewById(R.id.progBar);
final Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
int what=msg.what;
switch(what)
{
case 1:
{
//do some long operation
}
case 2:
{
//do some long operation
}
break;
case 3:
{
// do some long operation
}
break;
case 4 :
{
//do some long opeartion
}
break;
}
}
};
new Thread(new Runnable() {
int progressStatus = 0;
public void run() {
handler.sendEmptyMessage(1);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
bar.setProgress(25);
}
});
handler.sendEmptyMessage(2);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
bar.setProgress(50);
}
});
handler.sendEmptyMessage(3);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
bar.setProgress(75);
}
});
handler.sendEmptyMessage(4);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
public void run() {
bar.setProgress(100);
}
});
// Update the progress bar
}
}).start();
Intent openList=new Intent("com.integrated.mpr.SENSITIVELIST");
startActivity(openList);
}
Upvotes: 1