0

我有一个应用程序,其中我有 3-4 个线程,我希望第 2 个线程必须在第 1 个线程结束时启动,第 3 个在第 2 个线程结束时启动,依此类推.. 我正在给出代码..

//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.. 请帮助

4

1 回答 1

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);

        }
于 2012-06-01T11:25:52.370 回答