Thread advantages

The key in using threads to simplify your code is to try to keep your functions and methods on one train of thought. Rather than turn your code into an arthritic mess trying to interleave multiple simple simultaneous tasks, code each task separately. Code each line block with one mind. Let the OS thread scheduler do the interleaving.

I don’t get the point of this.May you explain more?

Consider a simulation application, where you simulate the interaction
between multiple entities. Giving each entity its own thread can greatly simplify many
simulation and modeling applications.

1-It makes no sense for me. May you explain more?

2-Could anyone explain about usage of thread in web service in real world(as yahoo massenger)

Consider a simulation application, where you simulate the interaction
between multiple entities. Giving each entity its own thread can greatly simplify many
simulation and modeling applications.

to make it more simple, take an example of cricket, suppose you want to simulate bowler and batsman.

  1. Assume you are doing this without thread implementation. In that case you need to write code to process bowler and process to batsman and call them interleaving. The invocation of the function will be sort of hard coded and the logic will be more complex.
  2. Assume you are doing this WITH thread implementation. In that case you need to write 2 separate functions one for bowler and 2nd for batsman. create 2 threads and call them interleaving. the speed of bowler can be easily increased by reducing thread’s waiting period and increasing batsman’s waiting period.

Assuming the logic for bowler and batsman in in the different functions for both the cases, (the bowler function will be only concentrating on the bowler’s action and batsman function will only concentrate on batsman.) the main controlling function is simpler and dynamic in 2nd case as compared to 1st case.

Say we are programming an air combat simulator

A plane fires a missile to enemy plane.

We can create two functions for both. one for missile and one for enemy plane.

1-If we don’t use threads, We must call them interleavely

2-If we use threads , it will be more supple and easy, Because, in 1st case, We only call two function.But in 2st, We use threads. and there are many function to control how a thread must be.stop(),sleep() and…

Please tell me your idea about what I said.

A question :

Why interleave is so hard?