0

I want to create a multi-threading function, can this function do the job? I also created a subclass called ThreadClass that extends the Thread and it included run();, then this main function is in the main class:

public static void main(String [] args) {
    ThreadClass function = new ThreadClass();
    for(int iThread = 0; iThread < 10000; iThread++) {
        function.start();
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • What's a `ThreadClass` ? – whatamidoingwithmylife Sep 08 '19 at 19:56
  • It is from my subclass called ThreadClass that extends Thread. class ThreadClass extends Thread{ – user9894502 Sep 08 '19 at 19:57
  • There are different limits regarding threads, depending upon those limits and the way in which you implemented your subclass specific number of threads may run, 10.000 threads will not be run according to my information. and even if there were no these limits, nothing guarantees that the thread which was first run will not finish execution until last thread is fired up. But that's just based on some basic information that I have, wait for some concurrency expert to explain it. – whatamidoingwithmylife Sep 08 '19 at 20:00
  • have you tried to run it? [thread-safety]?? – user85421 Sep 08 '19 at 20:31

2 Answers2

2

ThreadClass function = new ThreadClass();

Given your comment, I think we can treat ThreadClass as "almost the same" as Thread here.

And then, your code creates a single thread object. On which you then call start() many many times. Which is, simply not "valid" (see here for example).

So, from that point of view: your code creates a single thread, and then does something it shouldn't do.

Now, if you did:

for(int iThread = 0; iThread < 10000; iThread++) {
    ThreadClass function = new ThreadClass();
    function.start();

create one thread object per loop, then you would be creating 10K threads. What happens then ... very much depends. If all these threads stay alive, and do some real work, then most likely, you will either crash your machine, or some OS limitation will kick in and prevent your JVM from creating more threads than it ought to.

Assuming that your own class simply extends Thread, I would assume: you didn't overwrite the Thread run() method. Therefore, all these threads will start, do nothing, and end. You create a lot of churn, but maybe, depending on OS and hardware, even the modified version that really creates 10K threads passes fine. It wouldn't do anything useful. Just the JVM asking the underlying OS to create a thread, invoke that, and then get rid of it soon thereafter.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Depends...

If you did not override the start()-method of java's Thread, your code will cause an exception.

If you did override the start()-method and your start()-method calls run() on the ThreadClass object, then your code will execute but only on one thread (the main thread).

To create 10000 threads, you will have to create each of them with new Thread().

A better approach to acheive parallellism in java is to use a ThreadPool with a pre-defined number of threads that can execute your tasks.

ThoG
  • 51
  • 4