HA-BLOG
Multithreading in Java
(Ref. https://www.educative.io/)
What is multithreading?
(Ref www.educative.io)
Multithreading is a programming technique that allows for the simultaneous (concurrent) execution of two or more portions of a programme to maximise CPU efficiency. Multithreading, for example, allows you to create code in one programme while listening to music in another. Processes and threads are the building blocks of programmes.
Consider the following scenario:
• A programme is a file that can be run, such as chrome.exe
• A process is a programme that is currently running. When you double-click the Google Chrome icon on your computer, a process is launched that runs the Google Chrome application.
• A thread is a process's smallest executable unit. There can be numerous threads in a process, but only one main thread. In this case, a single thread could be displaying the current tab, while another thread could be displaying another tab.
Why use Multithreading?
• Multithreading has been incredibly crucial in terms of the efficiency of your application since the introduction of many cores. Your programme would have to shift back and forth to give the illusion of multitasking with several threads and a single core.
• Your application can use several cores to take advantage of the underlying hardware by running individual threads through a dedicated core, making it more responsive and efficient. Multithreading essentially allows you to fully utilise your CPU and its multiple cores, ensuring that no processing power is wasted on idle cores.
• Responsive programmes that offer the appearance of multitasking; efficient resource consumption In comparison to starting a new process, thread generation is lightweight, and web servers that employ threads instead of spawning a new process when receiving web requests require significantly fewer resources.
Basic concepts in multithreading
(Ref www.boggotoboggo.com)
Concurrency
Multithreading allows your programme to deal (not do) with multiple tasks at the same time, which is known as concurrency. Concurrency should not be confused with parallelism, which is doing many tasks at the same time.
Switching Contexts
Context switching is a multitasking approach in which CPU time is divided across all executing programmes.
A thread pool is a collection of worker threads that are all allocated to the same task. After completing a task, a worker thread is returned to the pool.Thread pools are often bound to a queue from which jobs are dequeued for worker thread execution.
• The system won't run out of memory because threads aren't created indefinitely.• By fine-tuning the thread pool, we can regulate the system's throughput. We can have enough threads to keep all processors engaged, but not too many that the system becomes overburdened.
• If the system is overloaded, the application will gently degrade.
Locking
Locks are a crucial characteristic that allows for multithreading. Locks are a synchronisation strategy for limiting access to a resource in an environment with many execution threads. A mutex is a nice example of a lock.
Mutex
Mutex indicates mutual exclusion, as the name suggests. A mutex is a form of lock that is used to protect shared data like a linked list, an array, or any simple primitive type. A mutex restricts access to a resource to a single thread.
Thread Security
Thread safety refers to the ability for many threads to use the same resources without revealing erroneous behaviour or causing unpredictable outcomes such as a race scenario or a deadlock. Various synchronisation approaches can be used to achieve thread safety.
Problems Associated with Multiple Threads
(Ref www.ntu.edu.sg)
Deadlock
Deadlocks occur when two or more threads are unable to continue because the first thread's resource is held by the second thread, and the second thread's resource is held by the first.
Livelock
A livelock occurs when two threads continue to take actions in reaction to one other rather than progressing. Think of two people trying to cross each other in a hallway as an analogy. Arun moves to his right to allow John to pass, while John moves to his left to allow Arun to pass.
Now they're both blocking each other. John moves to his right as he realises he's now blocking Arun, and Arun moves to his left as he realises he's now blocking John. They never cross paths and continue to obstruct each other.
(ref www.educative.io)
How do you avoid becoming stuck in a rut?
Avoid Unnecessary Locks: Only the members who are essential should be locked. Having too many locks can result in a deadlock. Try to minimise the need to lock items as much as possible as a best practise.
What can you do to avoid a racing condition?
Within the vital area of your code, race situations can occur. These can be prevented by employing thread synchronisation techniques such as locks, atomic variables, and message forwarding within essential areas.
How can you avoid going hungry?
The best approach to avoid starvation is to use a mutex or a lock like ReentrantLock. This creates a "fair" lock that gives priority to the thread that has been waiting the longest. You can use a semaphore to have multiple threads running at the same time while avoiding starvation.
How can you avoid getting stuck in a livelock?
Livelocks can be prevented by using ReentrantLock to figure out which thread has been waiting the longest and assigning it a lock. Blocking locks is bad practise; if a thread can't get a lock, it should release previously acquired locks and try again later.
Conclusion
(Ref www.Reddit.com)
• Multithreading in Java is an important feature that every Java developer should be familiar with. It aids in the efficiency of the programme and decreases the use of storage resources. It allows the CPUs to be used to their full potential, regardless of their complexity.
• If you want to learn more about threads, you'll need to know about the Thread class and the Runnable interface, as both provide a path and make threads popular among programmers. So go ahead and do your homework, and then let us know if we can assist you in any way!
References
-
Research Papers
- Kavi, Krishna. (1998). Multithreading Implementations. 17.
- Trahay, François & Brunet, Elisabeth & Denis, Alexandre. (2009). An analysis of the impact of multi-threading on communication performance. 1 - 7. 10.1109/IPDPS.2009.5160893.
- Murarka, P.R., Reza, M., Panda, R.R. (2014). Analysis of Multithreading in Java for Symbolic Computation on Multicore Processors. In: Sengupta, S., Das, K., Khan, G. (eds) Emerging Trends in Computing and Communication. Lecture Notes in Electrical Engineering, vol 298. Springer
- Murarka, Pawan & Reza, Motahar & Panda, Rama Ranjan. (2014). Analysis of Multithreading in Java for Symbolic Computation on Multicore Processors.
Good Content.
ReplyDeletepoints that are covered are good.
ReplyDeleteNice content ...now I can crack my collage exam by uing reference of this post . Thank you very much..
ReplyDeleteInformative content
ReplyDeleteA great blog indeed, got to learn many new concepts in multithreading.
ReplyDeleteInformative 👍
ReplyDeleteHelpful content
ReplyDeleteMulti-threading is such a dynamic concept which needs to be explained from the basics, which you guys did in a really great manner. Great research ani presentation 👌
ReplyDeletethis is an insightful article- excellent work indeed!
ReplyDeleteGreat work 💯💯 .
ReplyDeleteThe objective was to explain the audience what multithreading is, you have accomplished it.
ReplyDeleteEven a non IT guy like me understood it (not everything though).
The clean and simple representation was helpful, nice work.