-1

I'm trying to learn multi-threading but cant get the hang of it. I've an example here. The idea is to use two threads to sort the two arrays a and b and use another thread to merge the sorted arrays to array c. This is my code. I tried it with threads didnt work, so i've put up the code without the threads

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;
  }
}
1

2 Answers 2

0

A thread should implement the interface runnable and should override the method run.

Please Read Complete reference JAVA, it has many good examples.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. I got that part. where I am falling short is not calling the run method explicitly.
Yes you should initiate the thread. The thread does not inititate itself :)
like I said I put up my code without the thread part just to show what I am going for. I get the "extends Thread/implements Runnable part." However still not getting the not calling the run method. any suggestions or a link to any examples is highly appreciated
Like i mentioned even if you extend thread/implement runnable, the thread you are creating has to be started by a start method, which should be overrided again. public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } After this new can create a new instance of your thread and start it . In your case It should be t1.start and t2.start
0

Like i mentioned even if you extend thread/implement runnable, the thread you are creating has to be started by a start method, which should be overrided again.

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

After this new can create a new instance of your thread and start it . In your case It should be

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.