Secrets of || JavaThreads
You Must read this…..
Hi all, Welcome to another techie talk, in this session i will talk about this old topic java threads. lets go into the rest.
⚜ What is JAVA Threads? ⚜
We can simply says java thread is a Flow of Execution. That means multiple instructions executing in the same time.
Everyone thinks that threading is a tool to use to reduce execution time. For example, one woman takes 10 months to give birth to a child, but according to this, 10 women take only 1 month. It can’t be (1.1). threads also same because we cant grantee to increasing the number of threads can be reduce the execution time.

OK, i will explain it you with a real world scenario. think we had project with 5 processes.

In this process with one thread it takes 10 seconds to complete all the stages. But we use more threads to speed up the process, which doesn’t reduce what we think, because the save part can’t be done before the validation. But currency rate can be run in parallel with validation. It can save some time but its not depend on the number of threads.
I hope you have a clear understand of how this works.!!! 🚩
⚜ Multi-Threading ⚜
Multi-threading is a multiple processors.that means a technique by which a single set of code can be used by several processors at different stages of execution.

〽Process Based Multitasking
Which is running multiple independent programs in a same time. example listening music while writing an document and downloading some movie.that means when we stops the downloading that will not effect to the music. because they are run interdependently this one called process based multitasking
〽Thread based Multitasking
Computer identify these kind of process are single process because single processor use for these tasking but it has more threads think if we need to process 50 files (get data and save to database)think if we use one thread it need 50 minutes to complete. what about we use 50 threads, yes it will reduce the time maybe 20–10 min. but what will happened when we increase the threads to 100 is it reduce the time to 1 min?
Answer is no , because two threads cant get same file at once therefore in this scenario we maximum amount of threads can use is 50.
🧠 Multiple thread is multiple flow of execution🧠
⚜ How we can create a Thread ? 🤔
Java have 2 ways to create a thread
01). Extend thread class.
02). Implement runnable interface.
There is no matter how we create the thread but the case is what advantages and disadvantages of these 2 ways of creation.
Disadvantage

Look the image. it is inheritance hierarchy runner inherit sportman it inherit man, man inherit human so there have a relationship. ok what happened when we extend the runner with thread class. yes! it will break the relationship and there have no more connection with that after that runner act as a thread. because java doesn't support multiple inheritance. this was a disadvantage of a creating a thread.
advantages
- Threads improve the overall performance of a program.
- Context Switching time in threads is faster.
- Communication is faster in threads.
- Threads increases the responsiveness of the program.
01). Extend thread class.
Process of Thread
We cant grantee the thread is run in an order look below example;



Application class command to run a Start(); method but there is no start method in a printer class then where it is, it is in the Thread class because printer class extends the Thread class. and when we execute this program you can see the output is jumbled. so that we can grantee there is no order.
Why we have call to the start() method 💭
ok try with using Run(); method and get the output ans see what will we get,

yes first run the child and then main what was happened is there no create another thread. it means this is work as normal implementation with one thread. so we cant get all the benefit of the threads.
When main method exits is the whole program dead? ⚠
NO! java program doesn't dead when the main method exits if it has some child threads(non-demon) running program will still running when it is completed their jobs.
*Programs terminates all the non-demon threads terminate*
02). Implement runnable interface.
When you implement something from the runnable interface if your class implement the runnable interface to give the thread behavior that class does not have any start method to invoke so that you have to create a instance from the thread class and then you have to pass your runnable.



You can see we pass the runnable object inside the thread constructor that means keep in mind threads can have multiple constructors also.
Thread Priority
In java threads have a priority the maximum is 10 and lowest is 1. if you are deal with index then it start with a 0 but thread is not an index it is a value that’s why it start with 1
Which one is the highest priority
in threads it wont work like ranking system therefore maximum number is the highest priority that’s mean 10 lowest is 1 and mid is 5.
Does 5 is the default priority?
No its not, because any thread you created it gets the parent thread value. Example if the main thread create thread t1 then thread t1 inherit main thread priority. like wise if t1 create thread t2 the t2 thread inherit t1 priority therefore the default priority is not the 5.
What happened when we use priority beyond the 10?
thread.setPriority(12);

What happens if the TWO threads got same priority?
In this scenario there is no way to predict what will be the first.
Conclusion
- Threads are not indexes so it is not starting from 0.
- Thread priority always go to 1–10
- If you set something beyond this you got IllegalArgumentException.
- Threads are not rank so 1 is the lowest and 10 is the highest.
- Default thread priority is not always 5, only main method has the default priority as 5. all other thread inherit from the main thread.