Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Multi Threading - real world use cases

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.

like image 417
C4CodeE4Exe Avatar asked Nov 28 '11 06:11

C4CodeE4Exe


People also ask

What are some real life examples of multi threading as we study in Java?

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.

What is multithreading real life example?

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.

Where are multithreading projects used in real time scenario?

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.

Where is Java multithreading used?

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.


3 Answers

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):

  1. Dining philosopher's problem.
  2. Reader/Writer problem.
  3. Consumer/Producer problem.

Some more specific ones:

  1. Concurrent alpha-beta search (this is seriously tricky).
  2. Any genetic algorithm can be implemented concurrently and since memory/storage are shared amongst threads, it can be quite challenging if done right (try the semi-transparent polygon drawing an image project. You can search google for that).
  3. Concurrent database-access can be rather interesting. Sketch a scenario for yourself.
  4. Have a look at http://projecteuler.net/ which includes a bunch of interesting problems. Many of the problems there can be implemented using threads.

Enjoy.

like image 199
Jaco Van Niekerk Avatar answered Oct 18 '22 18:10

Jaco Van Niekerk


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

  • In Computer Games, objects like cars, motor bikes etc. They are just threads.
  • Asynchronous communication can allow an application to perform additional tasks instead of waiting for tasks to complete.
  • Applications that have a number of tasks that can be performed in any order can often benefit from distributed asynchronous communication.
like image 3
Premraj Avatar answered Oct 18 '22 18:10

Premraj


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.

  1. 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)

  2. 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
-------------------------->
                  
  1. System is capable of using multiple CPU cores
  2. When you have use cases like Fork-Join tasks ( use: ForkJoinPool)
  3. When you want to show progress of long computation task ( Progress bar can be shown in one thread and long computation task can be executed by other Thread)

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

like image 3
Ravindra babu Avatar answered Oct 18 '22 18:10

Ravindra babu