Java – create multiple threads using a for loop
                                        
                    •
                    Java                                    
                I'm trying to create multiple threads, the number of which depends on the command line input I know that extending thread is not the best OO exercise, unless you are making a special version of thread, but suppose this code creates the required results?
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);
    }
}
}
Solution
Yes, it is creating and starting n threads, all of which end immediately after printing run: and its name
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        