我有一个应用程序,其中我有3-4个线程,我希望第二个线程必须在第一个线程结束时开始,第三个在第二个结束时等等。我正在给代码..
//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();
实际上,我试图在第一个函数调用时显示25%的进度条,在第二个操作时显示50%,所以... on .. plz help
答案 0 :(得分:1)
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);
}