【问题标题】:Sort/ Merge arrays using multi threading使用多线程对数组进行排序/合并
【发布时间】:2016-02-08 03:54:35
【问题描述】:

我正在尝试学习多线程,但无法掌握它。我这里有一个例子。 这个想法是使用两个线程对两个数组 a 和 b 进行排序,并使用另一个线程将排序后的数组合并到数组 c。 这是我的代码。我用线程试过了没用,所以我放了没有线程的代码

public class Main {
  public static void main(String[] args){
    Random r = new Random(System.currentTimeMillis());
    int n = r.nextInt(101) + 50;
    int[] a = new int[n];
    for(int i = 0; i < n; i++)
      a[i] = r.nextInt(100);
    n = r.nextInt(101) + 50;
    int[] b = new int[n];
    for(int i = 0; i < n; i++)
      b[i] = r.nextInt(100);
    SortThread t1 = new SortThread(a);  
    SortThread t2 = new SortThread(b);  
    MergeThread m = new MergeThread(t1.get(),t2.get());
    System.out.println(Arrays.toString(m.get()));
  }
}

public class SortThread {
  int[] x; 

  public SortThread(int[] x){
    this.x = x;
    run();    
  }
    public void run(){
      sort(x);
  }

  private void sort(int[] x){
    for(int i = 0; i < x.length ; i++){
      int indexOfSmallest = findIndexOfSmallest(x, i);
      int t = x[i];
      x[i] = x[indexOfSmallest];
      x[indexOfSmallest] = t;
    }
  }

  private int findIndexOfSmallest(int[] a, int from){
    int indexOfSmallest = from;

    for(int i = from; i < a.length; i++)
      if(a[i] < a[indexOfSmallest])
        indexOfSmallest = i;
    return indexOfSmallest;
  }


  public int[] get(){
    return x;
  }
}

public class MergeThread {
  int[] a;
  int[] b;
  int[] c;

  public MergeThread(int[] a, int[] b){
    this.a = a;
    this.b = b;
    c = new int[a.length + b.length];
    run();      
  }
  public void run(){
    merge();
  }

  private void merge(){
    int aIndex = 0, bIndex = 0, cIndex = 0;
    while(aIndex < a.length && bIndex < b.length)
      if(a[aIndex] < b[bIndex])
        c[cIndex++] = a[aIndex++];
      else
        c[cIndex++] = b[bIndex++];

    while(aIndex < a.length)
      c[cIndex++] = a[aIndex++];

    while(bIndex < b.length)
      c[cIndex++] = b[bIndex++];
  }

  public int[] get(){
    return c;
  }
}

【问题讨论】:

标签: java multithreading sorting merge


【解决方案1】:

一个线程应该实现接口runnable并且应该覆盖方法run。

请阅读完整的参考JAVA,它有很多很好的例子。

【讨论】:

  • 感谢您的回复。我得到了那部分。我的不足之处是没有明确调用 run 方法。
  • 是的,您应该启动线程。线程不会自行启动:)
  • 就像我说的那样,我在没有线程部分的情况下放置了我的代码,只是为了展示我的目标。我得到“扩展线程/实现可运行部分”。但是仍然没有得到不调用 run 方法。非常感谢任何建议或任何示例的链接
  • 就像我提到的那样,即使你扩展了线程/实现可运行,你正在创建的线程也必须由一个 start 方法启动,它应该再次被覆盖。 public void start () { System.out.println("开始" + threadName ); if (t == null) { t = new Thread (this, threadName); t.start(); } } 之后这个 new 可以创建一个你的线程的新实例并启动它。在你的情况下应该是 t1.start 和 t2.start
【解决方案2】:

就像我提到的那样,即使您扩展了线程/实现可运行,您正在创建的线程也必须由 start 方法启动,该方法应该再次被覆盖。

 public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } }

在这个 new 之后可以创建一个新的线程实例并启动它。在你的情况下应该是

  t1.start()
  t2.start()
  m.start()

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2014-12-11
    相关资源
    最近更新 更多