16

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.

C4CodeE4Exe
  • 3,164
  • 8
  • 35
  • 45

6 Answers6

12

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.

Jaco Van Niekerk
  • 3,894
  • 2
  • 18
  • 41
3

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.
Premraj
  • 56,385
  • 22
  • 212
  • 157
3

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

Community
  • 1
  • 1
Ravindra babu
  • 42,401
  • 8
  • 208
  • 194
2

I would have a look at the disruptor library. This introduces a lot of advanced multi-threading concepts and once you understand it and can use it, you will know more than most on this subject.

Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
  • If the asker wants to learn about threading, this library might be a little too much . The concepts and implementation are pretty advanced. – Robert Munteanu Nov 28 '11 at 07:58
  • @RobertMunteanu, it is not a good starting point as you suggest, but if he wants to test his understand of multi-threading, it may help. – Peter Lawrey Nov 28 '11 at 08:01
2

There are several problems with Java that are just crying out for a multi-threaded solution. Two I've hit recently and solved were:

  • File.list has horrible problems if there are many thousands of files in the folder.
  • SAXParser.parse is a push parser. Many xml users would prefer a pull parser but would rather not add yet another library.

Both of these can be helped considerably using a simple two-thread solution.

In the case of File.list, run the File.list in a separate thread with a special FileFilter that posts all files presented to it to a BlockingQueue. An iterator on the BlockingQueue can then be used to deliver the files to the caller. An enhancement to list the directory recursively is an easy addition if the code is written correctly.

The SAXParser.parse can be turned inside-out in a similar way.

I have done both of these quite recently and found the experience very enlightening.

OldCurmudgeon
  • 60,862
  • 15
  • 108
  • 197
0

I would recommend you to design following system to start with the subject:

  1. Elevator(3 in the row) keeping the following parameters in consideration:

    Energy Consumption i.e. nearest elevator will take your request depending on whether it is moving up or now and the user request, weight on the elevator

    Time Consumption.

  2. File reader and writer system where multiple user can:

    Edit/Write/Search/Delete a file

Indzi
  • 390
  • 4
  • 24
Ankush soni
  • 1,325
  • 1
  • 13
  • 29