S
S
SHentai2017-11-07 05:10:14
Java
SHentai, 2017-11-07 05:10:14

How to run multiple threads in JAVA?

Hello, there is a code:

Rst i1 = new Rst();
new Thread(i1).start();
Rst i2 = new Rst();
new Thread(i2).start();
Rst i3 = new Rst();
new Thread(i3).start();

So everything works fine, but is it possible to somehow make the launch of threads through a loop so that it is convenient to regulate how much is needed?
I just can't figure it out at all like this: "Rst i1 = new Rst ();", for example, change i1 in a cycle to i2, well, you understand, this is the creation of a new variable.
To have something like this:
int streaming = 3;
for(int i = 0; streaming >= i; i++) {
     Rst i{#потока} = new Rst();
     new Thread(i{#потока}).start();
}

I would appreciate any help and advice, thanks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-11-07
@SHentai

In general, it's good practice to run many threads in a thread pool.
Here is a good introductory article on Habré.
Specifically, in your example, you can not create a variable, because. outside of the loop it doesn't make sense, then the code will look like this

int streaming = 3;
for(int i = 0; streaming <= i; i++) {
     new Thread(new Rst()).start();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question