2012-03-11 17 views
10

Próbuję utworzyć wiele wątków, których liczba zależy od danych wejściowych z wiersza poleceń. Wiem, że rozszerzenie wątku nie jest najlepszą praktyką OO, chyba że robisz wyspecjalizowaną wersję wątku, ale hipotetycznie jest ten kod, który tworzy pożądany rezultat?Java - Tworzenie wielu wątków za pomocą pętli

class MyThread extends Thread { 

    public MyThread (String s) { 
    super(s); 
    } 

    public void run() { 
    System.out.println("Run: "+ getName()); 
    } 
} 


class TestThread { 
    public static void main (String arg[]) { 

    Scanner input = new Scanner(System.in); 
    System.out.println("Please input the number of Threads you want to create: "); 
    int n = input.nextInt(); 
    System.out.println("You selected " + n + " Threads"); 

    for (int x=0; x<n; x++) 
    { 
     MyThread temp= new MyThread("Thread #" + x); 
     temp.start(); 
     System.out.println("Started Thread:" + x); 
    } 
} 
} 
+0

Powiedziałeś: „rozszerzenie Temat nie jest najlepsza praktyka OO chyba robisz wyspecjalizowaną wersję wątku”. Jednak twój przykład robi wyspecjalizowaną wersję wątku; dla mnie wygląda dobrze. – apollodude217

+1

Jeśli chcesz sprawdzić, możesz uruchomić swój program w trybie debugowania i sprawdzić, ile wątków jest pełnych. – Rockin

Odpowiedz

6

Tak, to jest tworzenie i uruchamianie n wątki, wszystko kończąc natychmiast po wydrukowaniu Run: i ich nazwy.

1

Jedna ważna rzecz, java JVM może utworzyć 20000 wątków naraz. Tworzenie 255 wątków w Javie

class MyThread1 extends Thread { 
    int k; 
    public MyThread1(int i) { 
      k = i; 
    } 

    @Override 
    public void run() { 
     //Your Code 
     System.out.println("Thread no. "+k); 

    } 
} 
class MainClass { 

    public static void main(String arg[]) throws UnknownHostException { 
     Refresh() ; 
    } 

    public static void Refresh(){ 

     //create 255 Thread using for loop 
     for (int x = 0; x < 256; x++) { 
      // Create Thread class 
      MyThread1 temp = new MyThread1(x); 
       temp.start(); 
      try { 
       temp.join(10); 
      } 
      catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
1

Masz lepszą alternatywę z kodem ExecutorService

próbki:

import java.util.concurrent.*; 

public class ExecutorTest{ 
    public static void main(String args[]){ 

     int numberOfTasks = Integer.parseInt(args[0]); 
     ExecutorService executor= Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); 
     try{ 
      for (int i=0; i < numberOfTasks; i++){ 
       executor.execute(new MyRunnable(i));     
      } 
     }catch(Exception err){ 
      err.printStackTrace(); 
     } 
     executor.shutdown(); // once you are done with ExecutorService 
    } 
} 
class MyRunnable implements Runnable{ 
    int id; 
    public MyRunnable(int i){ 
     this.id = i; 
    } 
    public void run(){ 
     try{ 
      System.out.println("Runnable started id:"+id); 
      System.out.println("Run: "+ Thread.currentThread().getName()); 
      System.out.println("Runnable ended id:"+id); 
     }catch(Exception err){ 
      err.printStackTrace(); 
     } 
    } 
} 

wykorzystania:

java ExecutorTest 2 

Runnable started id:0 
Run: pool-1-thread-1 
Runnable ended id:0 
Runnable started id:1 
Run: pool-1-thread-2 
Runnable ended id:1 

Podobne posty: (zalety korzystania ExecutorService jako zamiennik zwykłego Thread)

ExecutorService vs Casual Thread Spawner

How to properly use Java Executor?

Powiązane problemy