In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to do concurrent programming in Tomcat". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to do concurrent programming in Tomcat.
The first is the most commonly used synchronized
In the container startup process, it starts from Server to each lower-level container. The following code is that Server finds the configured Service and traverses the startup.
Private final Object servicesLock = new Object ()
/ / Start our defined Services
Synchronized (servicesLock) {
For (int I = 0; I
< services.length; i++) { services[i].start(); } } 其次,是锁范围的减小 service的启动过程,其实是这样的 protected void startInternal() throws LifecycleException { // Start our defined Container first if (container != null) { synchronized (container) { container.start(); } } synchronized (executors) { for (Executor executor: executors) { executor.start(); } } } 上面的代码,我们看到,并不是整个方法进行加锁,而是对于各个容器内组件的启动进行分别加锁。这种对于锁作用范围和持有时间的缩小,可以降低锁竞争,提升可伸缩性。当然,如果说所有的内容都分别加锁反而会影响性能。感兴趣的朋友可以阅读Java并发编程实战的性能与可伸缩性一章,了解更多内容。 用线程池启动容器内组件 // Start our child containers, if any Container children[] = findChildren(); List results = new ArrayList(); for (int i = 0; i < children.length; i++) { results.add(startStopExecutor.submit(new StartChild(children[i]))); } boolean fail = false; for (Future result : results) { try { result.get(); } catch (Exception e) { }} 各个容器获取到其子组件后,将其组装成一个任务,提交到任务执行线程池中。任务的执行结果,在通过其Future对象获取。 通过Callable封装带返回值的任务 private static class StartChild implements Callable { private Container child; public StartChild(Container child) { this.child = child; } public Void call() throws LifecycleException { child.start(); return null; } } 由于组件的启动并不需要返回值,此处使用Void类型,可以在实际使用过程中换成具体的值返回具体的结果。在全部任务执行完成后,从Future中get返回值。 Volatile的使用 private volatile boolean close = false; // Time to terminate? if (close) { timeout(0, false); try { selector.close(); } catch (IOException ioe) { } 通过使用volatile值,来保证多线程环境中关闭标识的可见性,从而能正确的在标识改变后退出特定逻辑。 wait/notify的使用 在前面的概念中,我们提到使用wait/notify的时候,一定要在拿到锁的情况下进行。Tomcat在进行Servlet实例allocate和deallocate的时候,会使用到这两个操作。 synchronized (instancePool) { while (countAllocated.get() >= nInstances) {
If (nInstances < maxInstances) {
InstancePool.push (loadServlet ())
NInstances++
} else {
InstancePool.wait ()
}
}
When uninstalling, the code looks like this
Synchronized (instancePool) {
CountAllocated.decrementAndGet ()
InstancePool.push (servlet)
InstancePool.notify ()
}
In both cases, the lock is obtained first.
Of course, there are many uses of components in the JDK concurrency package in Tomcat, such as the following one for CountDownLatch
Private volatile CountDownLatch stopLatch = null
StopLatch = new CountDownLatch (pollerThreadCount); / / when Endpoint performs bind operation, set the CountDownLatch of the corresponding number of poller
/ / when processing destory, the countDown operation is performed, and the subsequent shutdown operation will wait according to the data of stopLatch.
StopLatch.countDown ()
Thank you for your reading, the above is the content of "Tomcat how to carry out concurrent programming". After the study of this article, I believe you have a deeper understanding of the problem of Tomcat concurrent programming, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.