Multi threading


Classe de tri
public class Tri extends Thread {
public int []t;
public int ind;
public int min ;
public int ret=0;
public int indret;
public Tri(int[]tab,int Indice,int min)
{
this.t=tab;
this.ind=Indice;
this.min=min;
this.ret=0;
start();
}

   public void run() {
 
     for(int i =this.ind;i<this.t.length ;i++)
     {
    if(min > t[i])
    {
    min = t[i];
    indret =i;
      ret = -1;
    }
     }
     }
}
----
Classe de remplacement :

public class Remplace extends Thread {
public int []tab;
public int indice1;
public  int indice2;
int var;

public Remplace(int[] t ,int nb1,int nb2 )
{
this.tab=t;
this.indice1=nb1;
this.indice2=nb2;
start();
}

   public void run() {
     

    var=  this.tab[indice1];
   // System.out.println("indice1  "+this.tab[indice1]);
    this.tab[indice1]=this.tab[indice2];
    this.tab[indice2]=var;
 
   }
}

-----main

public class Mainproject {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int ind=0;
int[]t = {5, 8, 3, 2, 7, 10, 1};
int var;
Tri tr ;
Remplace rem;
rem = new Remplace(t,0,0);
int x ,indret;
tr=new Tri(t,0,t[0]);
for(int i =0; i<t.length;i++)
{
if (i>0)
{
tr= new Tri(t,i,t[i]);
t=tr.t;
}
tr.run();
indret=tr.indret;
//System.out.println("indret "+indret);
if(tr.ret==-1)
{
tr.interrupt();
// System.out.println("yyyyyindret"+indret);

rem = new Remplace(t,i,indret);
rem.run();
t=rem.tab;
}
tr.interrupt();
rem.interrupt();

}
for(int j =0; j<t.length;j++)
{
System.out.println("t "+t[j]);
}
}}


Commentaires

Posts les plus consultés de ce blog

factorielle

testthread

tri avec multithread