Pool

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Pool {

static int[][] field;
static int nums=0;
static int[][] dir = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

public static void main(String[] args) throws FileNotFoundException {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(new File("src/pool"));
    int N=sc.nextInt();
    field=new int[N+2][N+2];
    for(int i=0;i<N+2;i++){
        field[i][0]=0;
        field[0][i]=0;
        field[i][N+1]=0;
        field[N+1][i]=0;
    }
    for(int i=1;i<N+1;i++){
        for(int j=1;j<N+1;j++){
            field[i][j]=sc.nextInt();
        }
    }
    for(int i=1;i<N+1;i++){
        for(int j=1;j<N+1;j++){
            if(field[i][j]==1){
                nums++;
                Markpool(i,j,nums+1);
            }
        }
    }
    System.out.println(nums);
}

private static void Markpool(int x, int y, int seq) {
    // TODO Auto-generated method stub
    if(field[x][y]!=1)
        return;
    field[x][y]=seq;
    for(int i=0;i<4;i++){
        Markpool(x + dir[i][0], y + dir[i][1], seq);
    }
}

}

03-12
### Pool的概念 在计算机科学和编程环境中,Pool通常指预先分配的一组资源,这些资源可以被重复利用以减少创建和销毁资源的成本。这种机制广泛应用于不同场景中,比如内存池、线程池等。 #### 内存池 内存池是一种用于管理动态内存的技术。通过预先分配一块较大的连续内存区域作为“池”,当程序需要申请小块内存时可以直接从这个预分配的大块里划分出来,而不需要每次都向操作系统请求新的内存空间[^1]。 ```c++ // 创建一个固定大小的内存池 MemoryPool pool(1024 * 1024); // 1MB 的内存池 void* ptr = pool.allocate(64); // 分配64字节的空间 pool.deallocate(ptr); // 归还给内存池 ``` #### 线程池 线程池则是用来管理和重用工作线程的数据结构。它会初始化一定数量的工作线程并保持它们处于等待状态;一旦有任务提交进来就立即执行该任务而不必再经历启动新线程所带来的开销。 ```java ExecutorService threadPool = Executors.newFixedThreadPool(10); threadPool.submit(() -> { System.out.println("Task is running"); }); ``` #### 数据库连接池 数据库连接也是一种昂贵的操作,在应用程序频繁访问数据库的情况下建立和关闭连接会造成性能瓶颈。因此引入了数据库连接池来缓存已经建立好的连接对象供后续查询复用。 ```python import psycopg2.pool db_pool = psycopg2.pool.SimpleConnectionPool( minconn=1, maxconn=10, dbname="test", user="postgres" ) connection = db_pool.getconn() cursor = connection.cursor() cursor.execute('SELECT * FROM users') for row in cursor.fetchall(): print(row) db_pool.putconn(connection) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值