Skip to content
Advertisement

Does runnable run() create a thread everytime I call it?

I wrote a class which I use like as follows:

JavaScript

My code is the following:

JavaScript

My question is: Do I create a new thread every time I execute this if it’s instant i.e. the EventWrappers duration is zero?

I obviously know that the

JavaScript

creates a thread and execute it after the scheduled time but the

JavaScript

is real time without a thread right? I don’t want my code to create threads and therefore to be heavy when it executes run().

Advertisement

Answer

No, calling run() of the Runnable interface will not spawn a new thread. On the other hand, when you wrap the Runnable with a Thread class and call start() then the JVM will spawn a new thread and execute the Runnable in it’s context.

In your code, only the Async tasks will run in a separate thread since the Threadpool is managing their execution.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement