Threads and processes

Source: Internet
Author: User

objectives of the program design
Performance: High-performance programs should be equivalent to CPU utilization, the higher the CPU utilization (always working, no idle time), the higher the performance of the program.
Experience: The experience here is not just how beautiful the interface, how handy, the experience here refers to the response speed of the program, the faster the response, the better the user experience.
Socket Network Programming
In the same JVM runtime environment, classes and classes can be called directly to each other, but classes in different JVMs cannot be called directly between them, and the socket network is programmed to communicate between classes that are in different JVMs or even different computers.
Socket: Equivalent to telephone landline
IP: Equivalent to phone number
common methods of socket API:
Accept ()--block;
getInputStream ()--Get the network connection input stream
Getoutputstream ()--Get the network connection output stream

CPU idle---solution for single-threaded multitasking IO blocking (for example: Disk operations): [ Single-threaded multitasking asynchronous IO--Get Ready task priority execution]
single-threaded multitasking, There are time-consuming calculations, CPU time-consuming blocking----solution: [Multithreaded Program (can perform task switching)]

process : A standalone program running in the operating system (Desk)
thread : A separate execution branch in a process. (a person)
multi-Threading with multiple processes:
a group of people (multiple threads) on a table (process) to eat, they will involve some problems, such as many people may clip a dish (competition), A and b at the same time see a piece of meat inside the plate, while sticking out chopsticks to clip, a first clip away, b late a little stretched to the plate when the time has gone, can only Mutually exclusive), there is a snack that needs to be eaten together with a bun. A clip of meat, b sandwiched a bun, a need B of the bun, B needs a meat, they deadlocked no one will budge (deadlock).

The sharing of resources between multiple threads is very convenient because they share the resource space of the process (on a table), but need to pay attention to a series of problems, competition, deadlock, synchronization and so on. If you open a table next to it (process). It is not convenient to talk between tables, to hand things (interprocess communication), and to open a table is more expensive than to add a person to a table. The number of people on the other table is unlikely to increase indefinitely, the capacity of the table is limited and so many persons (the thread handle of the process is limited). One table is broken. The dining situation of the person on the other table is not affected (the process is duli each other, one process crashes without affecting the other), and a person on the table needs to be sent to the hospital, it is estimated that the table will be scattered (the thread hangs will cause the whole process to hang up). So multi-threading and multi-process are each have advantages and disadvantages, can not generalize.
Summary
single-threaded: Suitable for IO asynchronous, can not block, can not have a lot of CPU-consuming calculation. Typical like Nodejs, there are some network programs
multithreaded Program: For CPU-intensive programs
* Multithreaded execution does not have a definite order, not the first thread must first execute, the current moment who seize the CPU resources, who will first execute.
* The main thread is only responsible for starting a threaded, does not need to wait for the county to run the end, if the main thread calls the Run method instead of the Start method, then just make the normal call, this is only a single-threaded
* The same thread cannot be started multiple times.
How to implement multithreading through the Runnable interface
1, because there is no start () in the runnable, so the boot thread still calls to start () in the thread class;

Threads and processes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.