如何为常用方法编写相同的线程?

时间:2016-09-30 18:26:43

标签: java multithreading

我有3个方法同时运行2个任务。其中一个任务在线程之间很常见,但我不知道如何为三个任务编写一个独特的方法来调用它。

private void compare(String path) {
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                // do something
                finished = true;

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            try{
                while(!finished){
                    statusLabel.setText("P-R-O-C-E-S-S-A-N-D-O");
                    Thread.sleep(500);
                    statusLabel.setText("");
                    Thread.sleep(500);
                }
                finished = false;
                statusLabel.setText("");
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    });

    t1.start();
    t2.start();

}

private void sobeNovaBaseBlackList(String path){
    Thread t1 = new Thread(new Runnable() {
        @Override
        public void run() {
            //do another thing
            finished = true;
        }
    });


    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            try{
                while(!finished){
                    statusLabel.setText("P-R-O-C-E-S-S-A-N-D-O");
                    Thread.sleep(500);
                    statusLabel.setText("");
                    Thread.sleep(500);
                }
                finished = false;
                statusLabel.setText("");
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    });


    t1.start();
    t2.start();
}

private void sobeNovaBaseFornecedor(){
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            // do anything
            finished= true;
        }
    });

    Thread t2 = new Thread(new Runnable() {
        @Override
        public void run() {
            try{
                while(!finished){
                    statusLabel.setText("P-R-O-C-E-S-S-A-N-D-O");
                    Thread.sleep(500);
                    statusLabel.setText("");
                    Thread.sleep(500);
                }
                finished = false;

            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    });

    t1.start();
    t2.start();
}

如您所见,线程2(t2)在所有方法中具有相同的行为。如何一次写t2?

2 个答案:

答案 0 :(得分:0)

创建一个实现Runnable的新类(例如:class Task2 implements Runnable)并在run方法中编写线程2的代码。然后在两种方法中实例化。

答案 1 :(得分:0)

您可以实现Runnable接口并将公共代码放在那里。 类似的东西:

public class ThreatT2 implements Runnable {
  private Boolean finished;

  public Test(Boolean finished) {
    this.finished = finished;
  }

  @Override
  public void run() {
    try{
      while(!finished){
        statusLabel.setText("P-R-O-C-E-S-S-A-N-D-O");
        Thread.sleep(500);
        statusLabel.setText("");
        Thread.sleep(500);
      }
      finished = false;
      statusLabel.setText("");
    } catch(InterruptedException e){
      e.printStackTrace();
    }
  }
}

然后在调用代码时可以调用run方法

ThreatT2 t = new ThreatT2(true);
t.start();