I want to work on multi-threading, but my current project do not have such opportunities.Can someone please guide me where should I start.I need real time scenarios so that I can directly jump onto coding. I am reading side by side as well.
Can you please refer some websites for practicing.
1. A very good example of thread-based multithreading is a word processing program that checks the spelling of words in a document while writing the document. This is possible only if each action is performed by a separate thread.
Real-life ExampleSuppose you are using two tasks at a time on the computer, be it using Microsoft Word and listening to music. These two tasks are called processes. So you start typing in Word and at the same time start music app, this is called multitasking.
Multithreading in Java gives the ability to execute code by different threads to perform tasks in parallel or as a separate task without waiting for other to complete. Computer games are the best examples of the Multithreading concept.
However, we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games, animation, etc.
Google can transfer you to practicing tutorial websites (much better than I can). A nice real time scenario could include any of the following (may seem academic, but the skills are absolutely transferable to practice):
Some more specific ones:
Enjoy.
Multi-threading simulated by user-level threads. These threads are implemented at application level not OS Kernel level.
These threads also called Green threads because, so fresh(brand new) and young(not ripe or mature).
Real world use cases
When single thread is taking long time to complete a computational activity, you should break it into small tasks and use multi-threading to reduce computation time.
You can see lot of use cases for multi threading in your project.
Where some tasks can run independently with-out depending on other tasks ( Just span a new Thread
or submit Runnable/Callable
tasks to ExecutorService/ThreadPoolExecutor
)
When you need to wait for completion of multiple parallel tasks to proceed with next task ( invokeAll()
)
e.g. Task 1 starts three independent tasks Task 2, Task 3 and Task 4 but Task 5 has to start after completion of Task 2, Task 3 and Task 4.
Task 1 -----> Task 2 -----> Task 3 -----> Task 4 -----> Task 5 -------------------------->
Related posts:
How to properly use Java Executor?
Whether to use invokeAll or submit - java Executor service
Java's Fork/Join vs ExecutorService - when to use which?
wait until all threads finish their work in java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With