首页 文章 精选 留言 我的

精选列表

搜索[socket编程],共10000篇文章
优秀的个人博客,低调大师

在进行数据库编程时,连接池有什么作用?

由于创建连接和释放连接都有很大的开销(尤其是数据库服务器不在本地时,每次建立连接都需要进行TCP的三次握手,释放连接需要进行TCP四次握手,造成的开销是不可忽视的),为了提升系统访问数据库的性能,可以事先创建若干连接置于连接池中,需要时直接从连接池获取,使用结束时归还连接池而不必关闭连接,从而避免频繁创建和释放连接所造成的开销,这是典型的用空间换取时间的策略(浪费了空间存储连接,但节省了创建和释放连接的时间)。池化技术在Java开发中是很常见的,在使用线程时创建线程池的道理与此相同。基于Java的开源数据库连接池主要有:C3P0、Proxool、DBCP、BoneCP、Druid等。

优秀的个人博客,低调大师

Python全套零基础视频教程+软件2018最新编程视频!

零基础学习Python必须明确的几点: 1.明确你将来是做什么工作的,需要掌握哪些技能,很多人连这个就不知道就盲目的学,你首先清楚,现在公司需要什么人才,你应该奔着什么目标努力。Python的学习方向有很多,主要还是爬虫 Pythonweb 数据分析,人工智能等等 2.系统的学习规划,规划好你每天学习的新知识和每天做的作业和练习,很多人想自学Python,两个就看完了,这样的能找到工作算是出奇了,现在学习Python想找到工作,没有4-6个月的根本不行,所以规划是一定要有的,我建议半年时间 3.注意学习方法,很多人在学习Python的时候,开始学那一刻起就选择了错误的学习方法,所以最后注定会放弃,回到原来的岗位继续做着自己不喜欢的事情,学习Python需要一定的技巧,在开始学之前多跟别人问问,不要自己盲目的自学,浪费时间。 需要视频资料软件可以再评论区留言

优秀的个人博客,低调大师

Java并发编程-撸一个数据库连接池

章节目录 等待超时模式的使用场景 可以掌握的技能 等待/通知 消费者/生产者模式 CountDownLatch、AtomicInteger、静态内部类、LinkedList、动态代理的使用 1.等待超时模式 场景 当我们调用方法时,这个方法返回的资源比较重要,比如获取数据库连接池中连接句柄。但是这个资源的返回随着业务量的增加,那么获取资源(连接池连接)的时间就会增加,那么调用一个方法时就要等待一段时间(一般来说是给定一个时间段),如果该方法能够在一段时间内获取到结果,那么将结果立刻返回,反之,超时返回默认结果。 等待/通知的经典范式,即加锁、条件循环、处理逻辑3个步骤,这种范式没办法做到超时等待,对经典范式做很小的改动,就可以实现超时等待。 等待超时模式伪代码: public synchronized Object get(long mills) throws InterruptedException { Object result = null; long future = System.currentTimeMills() + mills; long remaining = mills; while (result == null && remaining > 0) { wait(remaining);//释放锁,阻塞 mills 毫秒 remaining = future - System.currentTimeMills(); } return result;//如果超时之后获取到result则不返回null } 超时等待的作用就是不会永远阻塞调用者,但是 超时之后被唤醒,知识将线程从等待队列移动至阻塞队列,继续向下进行返回result还是要重新获取锁,如果一直获取不到锁,那么result也不会打印。只是增加了灵活性。 2.可以掌握的技能 实战 使用等待超时模式撸一个简单数据库连接池,在示例中模拟: 从连接池获取连接 RunnerThread 使用连接 RunnerThread 释放连接 RunnerThread注意:客户端获取(消费)连接的过程被设定为等待超时、等待/通知两种模式ConnectionPool.java-数据库连接池 package org.seckill.DBConnection; import java.sql.Connection; import java.util.LinkedList; /** * 数据库连接池对象 */ public class ConnectionPool { //链表list(池)维护 connection 连接对象 private LinkedList<Connection> pool = new LinkedList<Connection>(); //构造方法 初始化池中连接 public ConnectionPool(int initialSize) { if (initialSize > 0) { for (int i = 0; i < initialSize; i++) { pool.addLast(ConnectionDriver.createConnection());//创建initialSize个代理Connection对象 } } } //释放connection ,相当于-生产者 public void releaseConnection(Connection connection) { if (connection != null) {//有效归还连接 synchronized (pool) { pool.addLast(connection); //生产者动作完毕后,需要唤醒所有消费者 pool.notifyAll(); } } } //获取connection句柄,相当于消费者,采用超时等待与等待/通知两种策略 public Connection fetchConnection(long mills) throws InterruptedException { synchronized (pool) { if (mills < 0) {//非超时等待模式,采用等待/通知模式 while (pool.isEmpty()) { pool.wait();//本示例中不演示这种模式下获取连接的情景 } return pool.removeFirst(); } else {//超时等待模式 long future = System.currentTimeMillis() + mills; long remaining = mills; while (pool.isEmpty() && remaining > 0) { pool.wait(remaining); remaining = future - System.currentTimeMillis(); } Connection connection = null; if (!pool.isEmpty()) { connection = pool.removeFirst();//返回头结点对象 } return connection; } } } } ConnectionDriver.java-动态生成Connection代理对象 package org.seckill.DBConnection; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; /** * 数据库连接驱动, * 动态代理获取实现java.sql.Connection 接口的代理对象 */ public class ConnectionDriver { static class ConnectionHandler implements InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName() == "commit") { Thread.sleep(100); } return null; } } //获取Connection的动态代理类 public static final Connection createConnection() { return (Connection) Proxy.newProxyInstance( ConnectionDriver.class.getClassLoader(),//类加载器 new Class<?>[]{Connection.class},//Connection实现的接口列表,包含Connection接口 new ConnectionHandler());//与代理对象绑定的handler } } ConnectionPoolTest.java--测试类 package org.seckill.DBConnection; import java.sql.Connection; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class ConnectionPoolTest { //线程池中初始化10个连接 static ConnectionPool connectionPool = new ConnectionPool(10); //保证所有的ConnectionRunner 能够同时开始 static CountDownLatch start = new CountDownLatch(1); //main线程将等待所有的Connection Runner结束后才开始执行 static CountDownLatch end; public static void main(String[] args) throws Exception { //ConnectionRunner 线程数量,可以修改线程数量进行观察 int threadCount = 50; end = new CountDownLatch(threadCount); int count = 20;//每个线程进行20次fetchConnetion动作 AtomicInteger got = new AtomicInteger(); AtomicInteger notGot = new AtomicInteger(); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(new ConnectionRunner(count, got, notGot), "ConnectionRunnerThread"); thread.start(); } start.countDown();//使所有线程同时运行 end.await();//主线程等待所有线程运行完 System.out.println("总的请求次数" + threadCount * count); System.out.println("获取到的连接总数" + got); System.out.println("未获取到的连接总数" + notGot); } static class ConnectionRunner implements Runnable { int count;//每个线程fetchConnetion的次数 AtomicInteger got;//记录fetchConnection 成功的次数 AtomicInteger notGot;//记录fetchConnetion 未成功的次数 public ConnectionRunner(int count, AtomicInteger got, AtomicInteger notGot) { this.count = count; this.got = got; this.notGot = notGot; } public void run() { try { start.await();//等待 所有ConnectionRunner 初始化成功且处于Runnable状态,同时开始运行,由主线程控制的 } catch (InterruptedException e) { e.printStackTrace(); } while (count > 0) { try { //从连接池中获取连接,如果1000ms内无法获取到,将会返回null。 Connection connection = connectionPool.fetchConnection(1000); if (connection != null) { try { connection.createStatement(); connection.commit(); } finally { //归还连接 connectionPool.releaseConnection(connection); got.incrementAndGet();//对获取次数状态进行更改 } } else { notGot.incrementAndGet();//对未获取次数状态进行更改 } } catch (Exception e) { } finally { count--;//运行次数递减 } } end.countDown(); } } } 运行结果 1.设置RunnerConnection threadCount数为10 threadCount = 10 2.设置RunnerConnection threadCount数为20 threadCount = 20 3.设置RunnerConnection threadCount数为50 threadCount = 50 4.设置RunnerConnection threadCount数为100 threadCount = 50 可以看到随着 runnerConnection 连接线程数的递增,连接的稳定性是越来越低的。但用户调用不会长时间阻塞到connect fetch 上,而是按时返回。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册
修改密码
忘记密码