Create a thread like this by creating a new Thread object.

 public void my_method() { //тут делаю всякие свои действия //Создание потока Thread thr1 = new Thread(new Runnable() { @Override public void run() { //исполняемый код } }); //тут запускаю поток thr1.start(); } 

It is necessary to create many threads that will simultaneously solve the same task, but with different source data.

Those. You need to create threads from thr1 to thr100 . Copying streams in the source code and giving them names from 1 to 100 (and there may be more in the future) is clearly not an option.

We need to do something so that the name of the thread is created according to this principle:

 i = i + 1 имя_потока = "thr"+i; Thread имя_потока = new Thread... ... 

But how to implement it in practice?

In this case, the flow is controlled from the same class, where it is created, and I would not want to lose this opportunity.

Alternatively, you can inherit from the class Thread , but then you need to create a thread in a separate class, which is very inconvenient in my situation. Or you can do without a separate class? But how?

  • naming through setName is not necessary? public void run() { Thread.currentThread().setName("My custom name here"); } public void run() { Thread.currentThread().setName("My custom name here"); } ...... I would also like to know why so many variables or naming is needed at all? For what? And would you like to use a pool of threads? - Alexey Shimansky
  • @ Alexey Shimansky about setName - not sure what this is necessary for the implementation of my task. But thanks for the idea, I will definitely try. In general, I am doing a multi-threaded application to work directly with several accounts of a certain service. About the pool of threads jai did not know, I will google. - andrshpa
  • If you do not consider a pool of threads, then who is stopping you from starting an array and starting 100 threads in a loop? - Russtam
  • @Russtam I do not understand how using the array and cycles to run 100 threads, while giving them different data. - andrshpa
  • See my answer below as an example. Correct such things to do using the ExecutorService. - Russtam 2:21 pm

4 answers 4

I can offer the following solution:

 public static void main(String[] args) throws InterruptedException, IOException { ExecutorService executorService = Executors.newFixedThreadPool(10, new CustomThreadFactory()); executorService.submit(() -> { //что то там }); } private static class CustomThreadFactory implements ThreadFactory { private final AtomicInteger counter = new AtomicInteger(0); private final String prefix = "th"; @Override public Thread newThread(Runnable task) { return new Thread(task, prefix + counter.incrementAndGet()); } } 

    As an option:

    Inheriting the class Thread :

     public class MyThread extends Thread { private static int id = 0; public MyThread(Runnable target) { super(target, "thr " + id++); } } 

    Well, then:

     MyThread thr1 = new MyThread(new Runnable() { @Override public void run() { //исполняемый код } }); 

      This is how you can create 100 threads in a loop and transfer data (see dataForThread, as an example)

      Implementing class Runnable:

       public class MyRunnable implements Runnable { private int dataForThread; public MyRunnable(int dataForThread) { this.dataForThread = dataForThread; } @Override public void run() { System.out.println(dataForThread); } } 

      Create and run 100 threads:

       public static void main(String[] args) throws Exception { Thread[] threads = new Thread[100]; for (int i = 0; i < 100; i++) { threads[i] = new Thread(new MyRunnable(i), "MyThread " + i); threads[i].start(); } //TODO ждем когда потоки закончат работу, можно в цикле через join() } 

        Everything is very simple:

         private i = 1; public void my_method() { //тут делаешь всякие свои действия //Создание потока Thread thr = new Thread(new Runnable() { @Override public void run() { int data = Integer.valueOf(getName()); //исполняемый код } }, i++); //тут запускаешь поток thr.start(); }