Reputation: 660
So I'm trying to find out a bit about Threads and everywhere its said that every process has at least one thread. That would mean if I only have a main class it would be a thread, wouldn't it?
But: I thought the class becomes a thread by extending Thread. So how come every process has a Thread, when I do not extend it in main?
Having more than one Thread only makes the process faster, if I use different resources, which can be used at the same time... like if I read in input from user one thread can wait for the user to type in and press enter, while another can work with the last input and so on.. right?
But if I want to calculate a bunch of equations.. it would NOT be faster with more than one thread, because they cannot calculate at the same time anyway (if I do not have several processors..)
Can someone clear things up for me? I tried to read a lot about this, but everyone just use the same words and that just doesn't help me with my problem!
Upvotes: 1
Views: 1847
Reputation: 116858
Jon's answer is great but it doesn't address all your questions. Some of the other answers are extremely misleading. So I thought I'd add my 2 cents.
... every process has at least one thread. That would mean if I only have a main class it would be a thread, wouldn't it?
Right. When the JVM starts, there is a single user thread which calls the main
method. There are also background threads that run the garbage collector and other services but you don't have to worry about them.
But: I thought the class becomes a thread by extending Thread.
This is somewhat confusing. You can create an instance of the Thread
class by instantiating your MyThread
class which extends Thread
. But this doesn't actually start a new thread until you call the start()
method on it. That is the method that creates a new running thread.
// creates an instance of the class
MyThread thread = new MyThread();
// actually starts the new thread running
thread.start();
FYI, it is recommended to implement Runnable
instead of extending Thread
:
public class MyRunnable implements Runnable {
public void run() {
...
}
}
...
// this is how you start a thread with your runnable
Thread thread = new Thread(new MyRunnable());
thread.start();
So how come every process has a Thread, when I do not extend it in main?
The main
thread is special and atypical. The JVM starts it for you. You don't extend Thread
in your main class at all.
Having more than one Thread only makes the process faster, if I use different resources, which can be used at the same time...
Yes. That is one of the primary reasons why we write multithreaded code. This is especially true when you are dealing with IO (network, disk).
But if I want to calculate a bunch of equations.. it would NOT be faster with more than one thread, because they cannot calculate at the same time anyway (if I do not have several processors..)
If you only have one CPU and you only have processor calculations, then you are correct. Your program would not get any faster by starting more threads. If your one CPU had multiple internal CPU "cores" then you should get a speed increase of course.
It is also important to realize that in most situations, you are reading input from a file or writing results to logs. Very few jobs are truly 100% CPU in my experience.
Hope this helps. Feel free to ask questions in the comments and I'll flesh out my answers.
Upvotes: 1
Reputation: 1406
But : I thought the class becomes a thread by extending Thread.
Exactly, JVM creates one thread for every program whether you extend Thread or not. But if you want a multi-threaded application, you have to extend Thread class. So, if you don't extend Thread, there will be only one Thread running your programm, but for your program to be multi-threaded, you need to implement Thread class.
Having more than one Thread only makes the process faster, if I use different resources, which can be used at the same time.
It's not compulsory that having more than one thread makes program run faster. I would say "Proper implementation of multi-threading in your application can improve the performance of your code." Threads are not magic, they are just proper utilization of CPU time.
Upvotes: -1
Reputation: 328598
So how come every process has a Thread, when I do not extend it in main?
The Java Virtual Machine, which executes your code, is a process created by your OS. That process in turns create threads, one of which is the thread that runs your main (generally called the main thread). That thread is automatically created by the JVM.
it would NOT be faster with more than one thread, because they cannot calculate at the same time anyway (if I do not have several processors..)
Yes you are right here. If a task is CPU bound (calcuating something), making it multi threaded will only improve performance if you have more than one processor to run the calculations.
Upvotes: 1
Reputation: 13356
When your program loaded, a new process will be setup -- process is the unit for resource management; however, thread is the unit for scheduling, thus every running Java program has at least one process to run your Main
.
As to your second question: multi-thread does not always promote the program performance. Multi-threads will introduce overhead for thread management. Your program can not be boosted in a computing-intensive environment on a single core CPU machine as you claimed. Nor does the IO-intensive program: in that case, all the threads may be blocked waiting for user input. The situation where multi-threading is more appropriate is that the program involves both computing and IO, e.g. GUI program, where the computing thread can be scheduled to run when IO blocked, e.g. program can render GUI while waiting for mouse input.
Upvotes: 0
Reputation: 377
for the second question:
first, multiple threading dosen't mean much faster: if your code can only execute in sequence, multi-thread means nothing for you that your task can not split to multiple sub-tasks and can't execute concurrent.
second, for single processor, multi-thread can increase your system throughput that makes your tasks seems be executed much faster. Because CPU always need to wait for the resources much slower than it to response, such as disk I/0, network, while OS(JVM) can schedule another thread on CPU to continuously execute not just waiting (single thread scenario)
third, for multiple processor, obviously multi-thread can actually execute task in parallel. that's definitely faster!
Upvotes: 0
Reputation: 10900
To answer your first question: yes, every process has at least 1 thread. In the case of Java, the method main (String[] args)
is executed by the main thread. This thread is created by the JVM when you start your program.
Regarding your second question: If a long calculation must be executed, then breaking it up in smaller pieces and running each piece on a different thread can (and will) improve performance. This is only if the CPU is a multi-core one. Note: Not all calculations/tasks can be broken in smaller pieces.
Upvotes: 2
Reputation: 1132
Thread Simply means an "Execution Flow." So if you just have a main class ; the normal flow of execution of statements inside itself is a thread. If you want to parallelize your program even further you may add different flows of execution which will flow in parallel. For this you need classes which extends Thread, Callable or Runnable.
Upvotes: 1
Reputation: 1500065
But: I thought the class becomes a thread by extending Thread.
A class isn't a thread. They're separate concepts. Threads execute code; there's no way code can execute without being executed by some thread or other.
You create new threads using the Thread
class (or using another class to do it for you) but a thread of execution is separate to the Thread
class itself. A Thread
object is just a representation of the thread of execution. The JVM itself starts up threads (including the "main" thread) in order to execute code.
It sounds like you're mostly right about the point of threads and when they can be useful... although these days it's pretty rare to be running on a machine with only one processor.
Upvotes: 10