Java - Creating Multiple Threads with a For Loop

HSeldon :

I am trying to create multiple threads, the number of which is dependent on the input from the command line. I know extending Thread isn't the best OO practice unless you are making a specialized version of Thread, but hypothetically is this code creating the desired result?

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);
    }
}
}
Daniel Hershcovich :

Yes, it is creating and starting n threads, all ending immediately after printing Run: and their name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating and naming multiple, simultaneous threads with a for loop

From Dev

Creating multiple Threads Using Java RecursiveTasks

From Dev

Creating threads in infiinite loop

From Java

How to create multiple threads using a loop in java

From Dev

Creating multiple threads in C

From Java

Creating multiple arrays using one for loop in Java

From Java

Creating multiple variables "name + i" in a for loop (JAVA)

From Java

Creating a socket server which allows multiple connections via threads and Java

From Dev

Creating two threads in Java

From Dev

Creating threads recursively in java

From Dev

Loop for creating threads and joining with variable number of threads

From Dev

Creating multiple threads with shared resources

From Dev

Issue creating multiple threads with pthreads

From Dev

creating multiple threads in C issue

From Dev

Python creating multiple threads and creating processing time

From Dev

Java run multiple threads from for loop with different names

From Dev

How to make multiple Threads run at the same time using a for loop in Java?

From Java

Java while loop and Threads!

From Dev

Creating multiple objects in loop

From Dev

Creating multiple dataframes in loop

From Dev

Creating multiple variables in a loop

From Dev

Creating multiple matrices with a for loop

From Dev

Creating multiple scatterplots with a for loop

From Dev

Creating multiple dataframes with a loop

From Java

Creating too many threads in Java

From Dev

input buffer while creating multiple objects using for loop in java

From Dev

c - creating threads & joining them in a for loop

From Dev

creating array of threads on window in a loop and wait statement

From Dev

Multiple Threads in Java for ConcurrentHashMap

Related Related

  1. 1

    Creating and naming multiple, simultaneous threads with a for loop

  2. 2

    Creating multiple Threads Using Java RecursiveTasks

  3. 3

    Creating threads in infiinite loop

  4. 4

    How to create multiple threads using a loop in java

  5. 5

    Creating multiple threads in C

  6. 6

    Creating multiple arrays using one for loop in Java

  7. 7

    Creating multiple variables "name + i" in a for loop (JAVA)

  8. 8

    Creating a socket server which allows multiple connections via threads and Java

  9. 9

    Creating two threads in Java

  10. 10

    Creating threads recursively in java

  11. 11

    Loop for creating threads and joining with variable number of threads

  12. 12

    Creating multiple threads with shared resources

  13. 13

    Issue creating multiple threads with pthreads

  14. 14

    creating multiple threads in C issue

  15. 15

    Python creating multiple threads and creating processing time

  16. 16

    Java run multiple threads from for loop with different names

  17. 17

    How to make multiple Threads run at the same time using a for loop in Java?

  18. 18

    Java while loop and Threads!

  19. 19

    Creating multiple objects in loop

  20. 20

    Creating multiple dataframes in loop

  21. 21

    Creating multiple variables in a loop

  22. 22

    Creating multiple matrices with a for loop

  23. 23

    Creating multiple scatterplots with a for loop

  24. 24

    Creating multiple dataframes with a loop

  25. 25

    Creating too many threads in Java

  26. 26

    input buffer while creating multiple objects using for loop in java

  27. 27

    c - creating threads & joining them in a for loop

  28. 28

    creating array of threads on window in a loop and wait statement

  29. 29

    Multiple Threads in Java for ConcurrentHashMap

HotTag

Archive