What is the minimum unit for running JAVA?

Source: Internet
Author: User
Recently, I switched from PHP to JAVA. But I have been confused about the problem. Why is the concept of connection pool in JAVA? PHP runs from the script to the script, and the script ends, the object will be lost automatically. How does java run? Is java called by different threads or the same... recently switched from PHP to JAVA. But I have been confused about the problem. Why is the concept of connection pool in JAVA?
PHP runs from the script to the script, and the script ends, the object will be lost automatically. How does java run? Isn't java messy if it is called by different threads or the same object ?? How can we differentiate who is the object of different users ?? I am confused. Now I am afraid of new objects.

Reply content:

Recently, I switched from PHP to JAVA. But I have been confused about the problem. Why is the concept of connection pool in JAVA?
PHP runs from the script to the script, and the script ends, the object will be lost automatically. How does java run? Isn't java messy if it is called by different threads or the same object ?? How can we differentiate who is the object of different users ?? I am confused. Now I am afraid of new objects.

Both php and java have been involved to help the landlord handle the problem.
Question and speculation: the main author uses php and java for web.
Of course, this is not important, but if it is all done on the web, it may be confused at the beginning due to the difference between the Process and the thread.
Conclusion: The main method is used to start a JVM process from beginning to end.
1. If the main user just runs a simple java Script, like other languages, the main method runs-> ends, and all objects are destroyed.
2. If you are running a web application, I use tomcat + servlet to explain the differences with php + cgi:
(1) For a web program (using tomcat), java starts to run the jvm started from tomcat. At this time, a process is running and all objects are in this process.
(2) When an http request comes over, tomcat hasA special object will receive the requestLaterCreate a threadLoad the corresponding servlet or jsp page (similar to php), and then execute and respond.
The difference is that, through webserver + cgi, php starts a process to execute the php script until the execution ends, so there is no object for this request.
However, in java, tomcat has not been closed, so the entire process remains alive. If you access a jsp with a global variable, the global variable will remain in the jvm memory started by tomcat even after the jsp script finishes executing the response.
I don't know what the landlord can understand ..

"Isn't java a mess if it is called by different threads or the same object ?"
About this question. Just like the specialized object I mentioned above. It receives a request and assigns a thread to each request for processing.
In the case of concurrency, this object may be used by multiple threads at the same time. So why is it the same object processing, that is, thread security? You can read the answer to @ fabricated beliefs and write it clearly, but you need to understand multithreading.

I only want to explain the multi-threading. First, you may need to understand what kind of object is in Java.YesWhat objects are shared by multiple threads?No. This standard depends on the object'sMemberWhether it can be shared among threads. For example

public class A {    public int value;}

Obviously, if multiple threads access the same A object at the same time, it is dangerous because they will "mess up" the value ". Class A is also called non-thread-safe. However, if I change to this:

public class A {    private int value;                            // 1    public A(int value) {this.value = value;}     // 2    public int getValue() {return this.value;}    // 3}

In this way, A becomes thread-safe. Why? Because the value of its member is private at (1), it can only be assigned A value in Constructor (2, the value can only be read (3), while the read operation can be performed by multiple threads at the same time.

Therefore, a simple principle is that a Member with a value assignment will become a hidden risk in multithreading. If a class contains members that can be assigned values, the objects of this class should exist independently in the thread instead of being shared to multiple threads for simultaneous access, unless only one thread is guaranteed to write data.

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.