2017-05-25 1 views
0

suis nouveau thread java j'ai une petite idée abou fil, permet de dire que j'ai 10 fils et je commence un par un comme celui-cicommencer fil un par un sans utiliser join() en java

public class BaseRunnable implements Runnable { 
    String ThreadNo; 
    public BaseRunnable(String ThreadNo) { 
     // TODO Auto-generated constructor stub 
     this.ThreadNo=ThreadNo; 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 

     for(int i=0;i<50;i++) 
     { 
      System.out.println("Thread no "+ThreadNo+" Running with index > "+i); 
     } 
    } 
} 

Et dans le principal methord

BaseRunnable runnable=new BaseRunnable("1");//run on main thread 
BaseRunnable runnableTwo=new BaseRunnable("2");//run on main thread 
BaseRunnable runnable3=new BaseRunnable("3");//run on main thread 
BaseRunnable runnable4=new BaseRunnable("4");//run on main thread 
BaseRunnable runnabl5=new BaseRunnable("5");//run on main thread 
BaseRunnable runnable6=new BaseRunnable("6");//run on main thread 
BaseRunnable runnable7=new BaseRunnable("7");//run on main thread 
BaseRunnable runnable8=new BaseRunnable("8");//run on main thread 


Thread one=new Thread(runnable); 
Thread two=new Thread(runnableTwo); 
Thread thr3=new Thread(runnable3); 
Thread thr4=new Thread(runnable4); 
Thread thr5=new Thread(runnabl5); 
Thread thr6=new Thread(runnable6); 
Thread thr7=new Thread(runnable7); 
Thread thr8=new Thread(runnable8); 

one.start(); 
two.start(); 
thr3.start(); 
thr4.start(); 
thr5.start(); 
thr6.start(); 
thr7.start(); 
thr8.start(); 

Je sais que si j'utilise join() je peux courir un thready par un (one.start(); one.join();)

je tente une autre façon en utilisant synchronisée qui a fait exécuter un thread o ne par un, mais il n'a pas été en ordre, donc tout expert me aider pour activer ce filiformes une première, Discussion 2 etc sans join()

S'il vous plaît considérer cela comme une question d'un débutant, s'il vous plaît aider

+0

S'il y a plusieurs tâches à faire, mais ces tâches doivent être fait dans un certain ordre et la tâche suivante ne peut pas démarrer tant que la tâche avant est terminée, alors pourquoi essayez-vous utiliser des threads du tout? S'il vous plaît fournir plus de contexte autour de ce que vous essayez de faire. –

+0

Je suis en train de tester et d'apprendre, j'ai récemment eu une question sur mon interview pour réaliser ceci, donc j'ai commencé à comprendre comment faire cela. S'il vous plaît considérez comme étudiant apprenant. Merci –

+0

Vous souvenez-vous de la question d'entrevue exacte? Est-ce vraiment la même chose que votre question postée? [This] (https://stackoverflow.com/questions/3741765/ordering-threads-to-run-in-the-order-they-were-created-started) est ancien mais s'applique toujours. –

Répondre

0

Vous pouvez atteindre votre objectif de cette façon:

 List<BaseRunnable> list = new ArrayList<>(); 
     for (int i = 0; i < 8; i++) { 
      list.add(new BaseRunnable(Integer.toString(i))); 
     } 

     for (BaseRunnable baseRunnable : list) { 
      Thread thread = new Thread(baseRunnable); 
      thread.start(); 
      while (thread.isAlive()) { 
       Thread.sleep(10); 
      } 
     }