+ 7

Every program in Java must have a thread?

It is question in quiz and the statement is false. How is it possible? Every program runs atleast at 1 thread (main thread). Maybe what they meant is explicitly declaring thread in program, which would be false. It is kind of misleading for me. edit: I still think that the answer is yes, every program must have a thread, because every program has main thread. Am I wrong?

6th Oct 2017, 9:49 AM
Jan Moravec
Jan Moravec - avatar
6 Respuestas
+ 3
The question is simple: Every program in Java must have a thread? (not Thread but thread). The answer is clear, in my opinion: YES. It is not question of threading functionality, it is not question if you consider main() as just entry point, it is clear from Apoorva's detailed explanation that JVM makes a thread at the beginning of program and then starts execution from main() method. Please change the answer to this challenge to YES.
13th Aug 2018, 2:06 PM
boskojevtic
boskojevtic - avatar
+ 33
Here are some basics for you, When the JVM starts, it creates a thread called "Main". Your program will run on this thread, unless you create additional threads yourself. The first thing the "Main" thread does is to look for your static void main(String[] argv) method and invoke it. That is the entry-point to your program. If you want things to happen "at the same time", you can create multiple threads, and give each something to execute. They will then continue to do these things concurrently. The JVM also creates some internal threads for background work such as garbage collection. You can't use this keyword because main is a static method, this refers to the current instance and there is none. You can create a Runnable object that you can pass into the thread: myThread = new Thread(new Server()); myThread.start(); That will cause whatever you put in the Server class' run method to be executed by myThread. There are two separate concepts here, the Thread and the Runnable. The Runnable specifies what work needs to be done, the Thread is the mechanism that executes the Runnable. Although Thread has a run method that you can extend, you can ignore that and use a separate Runnable. I hope you can clearly see why main() is not considered as a thread per se but a start point to your program. Hence not all program require threading functionality. Not necessary to have one always.
6th Oct 2017, 9:59 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 7
Actually there's not only the main thread always present but also the garbage collector which runs in it's own thread
6th Oct 2017, 10:43 AM
Schindlabua
Schindlabua - avatar
+ 3
@Apoorva Shenoy Nayak, great answer, concise, informative. thank you
10th Oct 2017, 3:34 AM
Vincent Peluso
Vincent Peluso - avatar
0
If you think about creating a custom thread using new Thread(..)...than answer is no.
8th Oct 2017, 2:32 PM
Adam Dec
Adam Dec - avatar
- 5
No
7th Oct 2017, 4:24 PM
Pradheepviki😎😀
Pradheepviki😎😀 - avatar