Java Thread Learning Summary

Source: Internet
Author: User
Tags new set requires resource sleep thread

When to use multithreaded technology and when to avoid it, this is an important topic we need to master. The main purpose of the arm is to manage a large number of tasks in an orderly manner. By mixing multiple tasks, you can use your computer resources more efficiently, or it will be more convenient for users. The classic question of resource equalization is how to use the CPU during the IO wait. As for the convenience of the user, the most classic question is how to monitor and respond sensitively to a "stop" button during a long download.
The main drawbacks of multithreading include:
(1) Waiting to use the shared resource causes the program to run slowly.
(2) The additional CPU overhead required to manage threads.
(3) An insignificant increase in complexity, such as a foolish idea to update each element of an array with a separate thread.
(4) long waiting, wasting energy competition and deadlock, and other multithreading symptoms.
Another advantage of threading is that they replace the "heavy" process scene switch (1000 instructions) with a "mild" execution switch (order of 100 instructions). Because all threads within a process share the same memory space, a "mild" scenario switch changes only the execution of the program and the local variables. When a "heavy" scenario is switched, a process change requires that the memory space be completely swapped.
Threading seems to go into a whole new field that seems to require us to learn a whole new programming language-or at least to learn a new set of language concepts. Since most computer operating systems provide support for threads, there are also extensions to threads in programming languages or libraries. Regardless of the circumstances, the programming involved in threading:
(1) At the beginning, people will not be able to touch the mind, request to change our traditional programming ideas;
(2) The support of threads in other languages appears to be similar. So once you've mastered the concept of threading, there's not much difficulty in other environments. Although support for threads has increased the complexity of the Java language somewhat, don't blame Java. After all, using threads can do a lot of good things.
Multiple threads may share the same resource (such as memory in an object), which is one of the biggest problems in using threads. You must ensure that multiple threads do not simultaneously attempt to read and modify that resource. This requires the skilful use of the synchronized (sync) keyword. It is a useful tool, but must really master it, because if the operation is improper, it is very easy to appear deadlock.
In addition, you should pay attention to a very special problem when using threads. Because of the Java design, it allows us to create as many threads as we need--at least in theory (for example, if you create millions of threads for an engineering finite element analysis, which is not practical for Java). However, we generally have to control the upper limit of the number of threads we create. Because in some cases, a large number of threads will make a mess of the scene, so the work will almost be stalled. The tipping point is not as good as thousands of of objects, but below 100. In general, we only create a few key threads to solve a particular problem. At this time the limit of the number of problems is not. But in some of the more conventional designs, this restriction does make us feel shackled.
You should pay attention to a problem that is not very intuitive in thread handling. Because of the thread "dispatch" mechanism, you can generally make your program run faster by inserting a call to sleep () in the main loop of run (). This makes it very demanding on programming skills, especially when longer delays seem to be able to lift performance. Of course, this situation occurs because a shorter delay may cause a break in the sleep () end scheduling mechanism before the running thread prepares to go into hibernation. This forces the scheduling mechanism to abort and then reboot later so that it can do its own thing and then go into hibernation. You have to think more before you realize the real problem of things.
One of the things omitted in this chapter is an animated example, which is the most popular application of the current program. However, the Java JDK Kit provides a complete set of solutions to this problem (and can play sounds) and can be downloaded to Java.sun.com's demo area. In addition, we have every reason to believe that future versions of Java will provide better animation support-although the current web has spawned many non-Java, unstructured animations that are completely different from the traditional approach. If you want to learn how the Java animation works, refer to the core java--kernel Java, written by Cornell&horstmann, Prentice-hall published in 1997. For more in-depth understanding of threading, refer to concurrent programming in the Concurrent programming in Java--java, published by Doug Lea, Addison-wiseley in 1997, or Java threads-- Java thread, Oaks&wong, O ' Reilly published in 1997.

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.