java - Sort/ Merge arrays using multi threading -


i'm trying learn multi-threading cant hang of it. i've example here. idea use 2 threads sort 2 arrays , b , use thread merge sorted arrays array c. code. tried threads didnt work, i've put code without threads

public class main {   public static void main(string[] args){     random r = new random(system.currenttimemillis());     int n = r.nextint(101) + 50;     int[] = new int[n];     for(int = 0; < n; i++)       a[i] = r.nextint(100);     n = r.nextint(101) + 50;     int[] b = new int[n];     for(int = 0; < 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 = 0; < 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 = from; < 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;   } } 

a thread should implement interface runnable , should override method run.

please read complete reference java, has many examples.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -