Java multithreading (1) and java Multithreading

Source: Internet
Author: User

Java multithreading (1) and java Multithreading
I. Concept of operating system midline process and Process

A thread is an execution unit in a process and a schedulable entity in the process.

Differences between threads and processes:

(1) Address Space: The thread is an execution unit in the process; the process must have at least one thread (the main thread of the process); All threads of the Process share the address space of the process; the process has its own independent address space.

(2) resource ownership: a process is the unit of resource allocation and ownership. threads in the same process share the resources of the process.

(3) The thread is the basic unit for Processor Scheduling, but the process is not.

(4) both processes and threads can be executed concurrently.

Generally speaking:

A process is an application running in memory. Each process has its own memory space. A process can start multiple threads.

A thread is an execution process in a process. A process can run multiple threads. A thread always belongs to a process. multiple threads in the process share the memory of the process.

Ii. threads in java

In java, "Thread" refers to two different things.

1. an instance of the java. lang. Thread class.

2. Thread execution.


In java, threads use the java. lang. Thread class or the java. lang. Runnable interface to define threads, instantiate and start new threads.

A Thread class instance is just an object. Like any other object in java, it has variables and methods and is stored on the heap.

Every thread in java has a call stack. Even if no new thread is created in the program, the thread runs in the background.

Java programs always start to run from the main () method. The main () method runs in a thread, which is the main thread.

Each time a new thread is created, a new call stack is generated.

There are two types of threads: User Thread and Daemon Thread ).

When all user threads are executed, the JVM will automatically shut down. The daemon thread is generally created by the operating system or the user.

Daemon Thread is used to provide services for the running of other threads. For example, the main difference between GC threads and User threads is that the virtual machine leaves. If all User threads are evacuated, then the Daemon Thread does not provide services for the Thread, so the VM exits.

The standard for JRE to determine whether the program is executed is that all foreground execution threads are finished, regardless of the background thread status.




How to Learn about java multithreading?

Learn how to learn. You will have a basic understanding. Let's look at the API documentation and make two small examples. There is no difficulty. Only JAVA-based API documentation is required.
You want to give you a PPT and an email.
 
How to Learn java multithreading?

Learning Java should actually rise to the realm of How to Learn program design. In fact, learning program design also accepts a programming idea. The program design concepts of each language are similar, but they are just some minor differences brought about by the language characteristics. For example, interfaces in Java, you have hardly encountered them in your previous studies. The following 9 points are the study notes summarized by "xiao shu's blog". I hope it will help you !!
1. We must clarify a general direction, that is, the current Object-Oriented Programming scope. Although artificial intelligence has a wave (to see why Borland has TurboProlog), in the next 5-10 years, the industry will widely recognize and accept object-oriented programming.
2. Currently, the most popular object-oriented programming languages in the industry are C ++ and Java. So we can basically lock these two directions. And can be mastered at the same time.
3. Master the essential features of Java and be sure to know why. For example, Interface and multi-thread. Using Interfaces is a better model of multi-inheritance, while multithreading is an important feature designed at the language level. We need to fully understand why the interface is, and there are several common programming models with multithreading.
4. After understanding the characteristics of a language, you can try to rise to the design level. After all, you need to learn the language. Currently, a better development mode is a custom object-oriented design, coupled with the MVC pattern (you can take a look at the content I introduced about MVC ). First, we need to find the objects at the top level (which is often the most difficult), and then layer-by-layer recursion. Remember that each time we meet the principle of 7 +/-2, because our short memory is like this. Generally, the graphic user interface should be designed from the interface.
5. With the basic Design model, you can learn some Design patterns ). This is currently proven to be effective. For example, hierarchy,
Pipe/Filter pipelines or filters), design patterns (such as Object Pool in the Object Pool and buffer Pool Cache), and programming patterns (such as Copy-on-Write ).
After understanding these models, we will have a good grasp of the overall structure of the system, and there is also a tendency for an academic system to be composed of various models. The MT mentioned above actually has several modes, so you don't have to spend a lot of time trying it on your own. Another very important field is the parallel and distributed computing field, with about 20 types.
6. Next we can't talk about it on paper. The best method is practice. In general, examples in textbooks cannot be regarded as practices. They can only be used to help you master the language features. It is not very good to advocate the practical Project, because you have not yet mastered the ability to integrate various technologies, so that you are more and more confused. I think the better way is to find some classic examples. Each example is designed by comparing a programming idea in the set. From HotDraw, I learned what Framework is and how to construct it using rolemodel, so that I can apply it to other places. By the way, you will never think of this example as small, but as big as a commercial Framework.
7. With the previous design patterns, you can understand these classic examples. And you can use it to implement some simple systems. If you can make further modifications to him, find out what you think can improve the performance, and add your own design, it will become a higher level, and you will truly feel some gains.
8. It seems that the above talk has nothing to do with Java. In fact, we should have gone from simply learning the language to truly learning the programming field. There is no end to learning technology. It may take six months for you to learn the first language. In the future, every language should not be more than two months. Otherwise, you will feel that language learning is a burden and a pain point.
9. Learning is used to create value for your program. It is easier to grasp this principle.
... The remaining full text>

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.