最近从PHP转到了JAVA。 但是一直迷惑一个问题。就是JAVA 为什么会有连接池这个概念呢?
PHP的运行方式是从脚本开始到脚本结束,脚本结束,对象自动就没了。java是如何运行的呢?java难道是不同线程调用的还是同一个对象,那不就乱了??不同用户来,怎么区别谁是谁的对象??我都搞晕了。现在我都不敢new 对象了。
回复内容:
最近从PHP转到了JAVA。 但是一直迷惑一个问题。就是JAVA 为什么会有连接池这个概念呢?
PHP的运行方式是从脚本开始到脚本结束,脚本结束,对象自动就没了。java是如何运行的呢?java难道是不同线程调用的还是同一个对象,那不就乱了??不同用户来,怎么区别谁是谁的对象??我都搞晕了。现在我都不敢new 对象了。
php和java都搞过,来帮楼主理理。
疑问兼推测:楼主是用php和java做web。
当然,这也不重要,不过如果都是做web可能由于进程和线程的区别,刚开始确实会混淆。
先说结论:java运行方式,自始至终都是main方法为入口,启动一个JVM进程。
1、如果楼主只是运行一个简单java脚本,跟其他语言一样,都是main方法运行->结束,对象都销毁。
2、如果是运行一个web,那我用tomcat+servlet的方式来解释和php+cgi的不一样:
(1)对于一个web程序(用tomcat),java运行开始就是从tomcat启动的jvm,这个时候就是一个进程在运行,所有对象都在这个进程中。
(2)当一个http请求过来,tomcat有专门一个对象会去接收请求之后会创建一个线程去加载对应的servlet或者jsp页面(它类似php),然后去执行和响应。
区别就是这里,php通过webserver+cgi是去启动一个进程执行php脚本直到运行结束,那么这次请求是没有任何对象还或者了。
但是java,由于tomcat一直没有关闭,所以整个进程是一直存活着。你访问一个jsp,如果它有全局变量,那么即便jsp脚本执行完响应结束,全局变量还是会留在tomcat这个启动的这个jvm内存中。
不知道楼主看得明白不。。
“java难道是不同线程调用的还是同一个对象,那不就乱了?”
关于这个疑问。就像我上面提到的那个专门对象是一样的。由它接收到个请求,然后再给每个请求分配一个线程去处理。
在并发的情况下,这个对象完全可能被多个线程在同时使用。 那为什么可以是同一个对象处理,就是线程安全这块了,这个你可以看@捏造的信仰 的解答,写得很清楚,但你要了解多线程。
我只针对多线程方面的东西解释下。首先楼主可能需要理解,在 Java 中,什么样的对象是可以多个线程共享的,什么样的对象是不可以的。这个标准取决于对象的成员是否可以在线程之间共享。比如
<code>public class A { public int value; } </code>
显然,如果多个线程同时访问同一个 A 对象,就会有危险,因为它们会把 value 的值给“搞乱了”。而 A 类也被称为非线程安全的。不过,如果我改成这样:
<code>public class A { private int value; // 1 public A(int value) {this.value = value;} // 2 public int getValue() {return this.value;} // 3 } </code>
这样 A 就变成线程安全的了,为什么呢,因为它的成员 value 在(1)处变成私有的了,而且只能在构造函数(2)中赋值,且一旦赋值就只能读取(3),而读取是可以多个线程同时操作的。
所以,一个简单的原则就是,一个可赋值的成员会成为多线程中的隐患。如果一个类包含可赋值的成员,那么这个类的对象应该在线程当中独立存在,而不应该共享给多个线程同时访问,除非保证只有一个线程会去写。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools