Thread is a light weight process. In java Thread can be said as
1.An instance of class java.lang.Thread
2.A thread of execution
An Instance of Thread is just like anyother objects in java , that has variables and methods , and lives and dies on the heap.
Whereas 'thread of execution' is a light weight process that has its own call stack.
That is ,'instance of class Thread' is related to 'worker' and 'a thread of execution' is related to 'the work to be done'.
From this, it is known that , you need a worker to do the work.So the thread is the worker and the work to be done is present in the run() method.
In java there is one thread per call stack and vice-versa.
Even if you dont create any new threads in your peogram , threads are there running in the back.For example , the main() method runs in one thread called main thread.
We have heared that threads are running concurrently or parallely.We also have heared that processor executes one thing at a time.In which case , both are contradictory.
Here comes the concept of scheduling . What actually happens is ,JVM gets its turn at the CPU by whatever scheduling mechanism the underlying OS uses, and operates like a mini-OS and schedules its own threads regardless of the underlying operating system. In some JVMs, the Java threads are actually mapped to native OS threads.
So, "When it comes to threads, very little is guaranteed".That is ,the behaviour in which the threads work will differ from machine to machine.
No comments:
Post a Comment