• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Practical scenario

 
raja singh kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have three tasks task1, task2, task3 which each thread should complete. Now suppose there are 2 threads T1 and T2. So both threads have to complete the three tasks.

Suppose T1 and T2 are competing to complete the 3 tasks. Now my goal is atleast one of the threads should complete all the 3 tasks as early as possible. It does not matter which thread completes all the 3 tasks first. All I want is one thread to complete all the three tasks. In this scenario would it really make sense to go for multi-threading?

If this scenario is not appropriate then what would be a practical scenario to use multi-threading?
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say task1, task2 and task3 take t1, t2 and t3 time to be completed. Now if threads T1 and T2 are equivalent, it will take total t1+t2+t3 time for any of the threads (T1 or T2) to complete the 3 tasks. Now which thread will finish first will depend on the thread scheduling and you may see sometimes T1 is finishing first and sometimes T2. But total time will be always more or less same.In this scenario, I don't think one should go for multi-threading as we are not gaining anything.

Instead, if task1,task2 and task3 are independent and the order of execution does not matter, one can go for multi-threading to speed up the process. Say, T1 is executing task1 and T2 is executing task3 and whoever finishes first will take task2 to process. In this case total time to finish task1, task2 and task3 will be < (t1+t2+t3).

 
Campbell Ritchie
Sheriff
Posts: 51469
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amar Saikia wrote:. . . go for multi-threading to speed up the process. . . .
You mean parallelism rather than multi‑threading. In a multi‑threaded environment on a single core, it takes longer to execute multiple tasks than to do them sequentially, because of the time for scheduling and changing threads being processed actively. In a multi‑core system, however, you can run several tasks simultaneously, one on each core (or maybe two on each core because each core often supports two threads). That will allow for faster execution if the tasks are reasonably similar in size and can really be run independently.
 
raja singh kumar
Ranch Hand
Posts: 114
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a multi‑threaded environment on a single core, it takes longer to execute multiple tasks than to do them sequentially, because of the time for scheduling and changing threads being processed actively.

Thanks for that. Can you please give a real life example in which it makes sense to use multi-threading?
 
Henry Wong
author
Marshal
Pie
Posts: 22126
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
Thanks for that. Can you please give a real life example in which it makes sense to use multi-threading?


A classic example would be that of a background task -- that is started by a user.

Let's say you have a GUI application, with a button. When this button is pressed, the application has to go do something with a database that takes 15 minutes. Without threads, when the user presses the button, he/she would have to wait 15 minutes. With threads, the 15 minutes process can be done in the background. Additionally, you can even have communication between the background task thread and the GUI thread, where the GUI displays some sort of progress bar.

Henry
 
raja singh kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you have a GUI application, with a button. When this button is pressed, the application has to go do something with a database that takes 15 minutes. Without threads, when the user presses the button, he/she would have to wait 15 minutes. With threads, the 15 minutes process can be done in the background. Additionally, you can even have communication between the background task thread and the GUI thread, where the GUI displays some sort of progress bar.

I was talking about practical example. Also is this possible on single core processor?

Ok let me try

Suppose I am at my home working on my laptop as well as eating. Now everytime I complete writing a method on my laptop I have a bite and so on till I finish working as well as eating. Now instead of doing something like that I can either complete my work first and then eat or I can eat first and then complete my work. Is there any optimization in the time taken by using multithreading in this context?

 
Henry Wong
author
Marshal
Pie
Posts: 22126
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
I was talking about practical example. Also is this possible on single core processor?


The usage of threads in a GUI environment, to run background tasks, so that the users can do other tasks at the same time... is... in my opinion, the most common use of threads.  How is that not practical? Isn't that the very definition of practical? ... 

And of course, it works on a single core processor. The very first GUIs were  all on operating systems running on single core processors.

Henry
 
raja singh kumar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usage of threads in a GUI environment, to run background tasks, so that the users can do other tasks at the same time... is... in my opinion, the most common use of threads.  How is that not practical? Isn't that the very definition of practical? ...  

Ok sorry I should not have used the word practical. I am looking for a layman or day-to-day example
 
Henry Wong
author
Marshal
Pie
Posts: 22126
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
Ok let me try

Suppose I am at my home working on my laptop as well as eating. Now everytime I complete writing a method on my laptop I have a bite and so on till I finish working as well as eating. Now instead of doing something like that I can either complete my work first and then eat or I can eat first and then complete my work. Is there any optimization in the time taken by using multithreading in this context?


Okay, first of all, you definitely need to look up the definition of "practical"...


Anyway, you need to think of the computer as having resources. And threads is just another resource. The computer has a multiprocessor capability so that it can run multiple threads in parallel. And the computer has a scheduler, so that it can distribute processor time to multiple threads. And they are both designed to optimally use the cores on the box -- meaning to keep them busy.

Anyway, here is an example (kinda contrived as not very common)...

Let's say you have a task. It is completely compute intensive. It doesn't really have to do much I/O. It doesn't have to do much user interaction. It doesn't have to wait for other tasks. etc. This means that when it is ran on a computer, it will use up 100% of the processor / core cycles. It is very optimal in that it is running as fast as possible on a core.

Now, can we run two of these? Well, in the context of a multiple core machine. Yes. Since there is no (or little) interaction, two compute intensive threads will use two cores, and they will not (or have little) interfere with each other.  In other words, you can run two tasks in the same time it will take to run one task.

Now, in the context of a single core machine, and using the scheduler to time slice the two threads, it will also work. The two threads will still not interfere with each other, but it will have to share resources. In this case, the resource is the processor. It is not possible to give 100% of a processor core to two different threads when there is only one processor core. This means, while you can run two tasks concurrently, it should take twice as long to run them. It will work, but there is no time advantage to do so.


Now, back to the GUI example...

In that example, one of the threads is doing user interaction. It needs resources, such as access to the keyboard, mouse, monitor, etc., but for the most part it is using very little of the processor time. This is because the bulk of the time is "waiting" for the user to do something. The other thread, the background task, is doing database work, which means it is doing I/O. And it too, may be using very little of the processor time. This is because the bulk of the time is "waiting" for the I/O, or "waiting" for the database server to complete it's task.

Now, in the context of a multiple core machine, and in the context of a single core machine (and using the scheduler to time slice the two threads), it will both work. And interestingly, it will work optimally in both cases. This is because the two threads are not sharing resources. And for the one resource that it is sharing, which is the processor, it is using very little of it.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22126
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
Ok sorry I should not have used the word practical. I am looking for a layman or day-to-day example


I am going to speculate that you are looking for an "easy to understand" "high level" "general" use-case. The issue here is probably "high level" or even "general".

This is, because, as you seen in the examples, it really depends on what the threads are doing, on what resources the threads are using, and on what resources are available in the environment.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic