What Are Threads (What is a Thread)?

前端 未结 5 578
鱼传尺愫
鱼传尺愫 2020-12-31 21:48

I\'m always confused about thread concepts. I haven\'t a chance to use them in a real environment so far. It would be helpful if someone could explain threads for me.

5条回答
  •  囚心锁ツ
    2020-12-31 22:45

    A simple explanation would be that you have a job to do, and you get a single person to do the job.

    This single person is similar to a thread in a computer.

    One person can do one thing at a time, so to do the job, he proceeds through the tasks of the job, one task at a time, one operation on each task at a time.

    To speed up a job, you can put more people to the same job. For instance, let's say you need to paint a house. You hire 4 people to do this.

    These 4 people could be similar to 4 threads in that they work with the same resources (same house, same buckets of paint) and they can divide up the work.

    A process would be akin to the job of painting that house.

    This simple explanation somewhat breaks down when it comes to machines that doesn't have enough CPU cores to run all the threads simultaneously, but I'm going to ignore this here.

提交回复
热议问题