当前位置: 首页 > news >正文

论坛类网站备案吗在哪里可以发布自己的广告

论坛类网站备案吗,在哪里可以发布自己的广告,青岛广告公司,云南企业展厅设计一、单例模式代码实现 public class DatabaseConnection {// 1. 私有静态实例变量private static DatabaseConnection instance;// 2. 私有构造函数,防止外部直接创建实例private DatabaseConnection() {// 初始化数据库连接System.out.println("Database con…

一、单例模式代码实现

public class DatabaseConnection {// 1. 私有静态实例变量private static DatabaseConnection instance;// 2. 私有构造函数,防止外部直接创建实例private DatabaseConnection() {// 初始化数据库连接System.out.println("Database connection initialized.");}// 3. 公共静态方法,提供全局访问点public static DatabaseConnection getInstance() {if (instance == null) {// 双重检查锁定,确保线程安全synchronized (DatabaseConnection.class) {if (instance == null) {instance = new DatabaseConnection();}}}return instance;}// 4. 示例方法,模拟数据库操作public void executeQuery(String query) {System.out.println("Executing query: " + query);}
}

二、日志管理器

场景描述

在一个大型系统中,日志记录是必不可少的。为了避免重复创建日志对象,我们可以使用单例模式来设计一个 日志管理器,统一管理日志的写入和输出。

代码实现

import java.io.FileWriter;
import java.io.IOException;public class Logger {// 1. 私有静态实例变量private static Logger instance;private FileWriter writer;// 2. 私有构造函数,防止外部直接创建实例private Logger() {try {// 初始化日志文件writer = new FileWriter("app.log", true);} catch (IOException e) {e.printStackTrace();}}// 3. 公共静态方法,提供全局访问点public static synchronized Logger getInstance() {if (instance == null) {instance = new Logger();}return instance;}// 4. 日志记录方法public void log(String message) {try {writer.write(message + "\n");writer.flush();} catch (IOException e) {e.printStackTrace();}}// 5. 关闭日志文件public void close() {try {writer.close();} catch (IOException e) {e.printStackTrace();}}
}

使用场景

public class Application {public static void main(String[] args) {// 获取日志管理器的单例实例Logger logger = Logger.getInstance();// 记录日志logger.log("Application started.");logger.log("User logged in: John Doe");// 关闭日志文件logger.close();}
}

会在代码里直接生成一个app.log文件

三、配置管理器

场景描述

在项目中,通常需要读取配置文件(如 config.properties)。为了避免重复加载配置文件,我们可以使用单例模式来设计一个 配置管理器,统一管理配置的读取和访问。

代码实现

import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;public class ConfigurationManager {// 1. 私有静态实例变量private static ConfigurationManager instance;private Properties properties;// 2. 私有构造函数,防止外部直接创建实例private ConfigurationManager() {properties = new Properties();try {// 加载配置文件properties.load(new FileInputStream("config.properties"));} catch (IOException e) {e.printStackTrace();}}// 3. 公共静态方法,提供全局访问点public static synchronized ConfigurationManager getInstance() {if (instance == null) {instance = new ConfigurationManager();}return instance;}// 4. 获取配置项public String getProperty(String key) {return properties.getProperty(key);}
}

使用场景

public class Application {public static void main(String[] args) {// 获取配置管理器的单例实例ConfigurationManager configManager = ConfigurationManager.getInstance();// 读取配置项String dbUrl = configManager.getProperty("database.url");String dbUser = configManager.getProperty("database.user");System.out.println("Database URL: " + dbUrl);System.out.println("Database User: " + dbUser);}
}

四、线程池管理器(ThreadPool Manager)

场景描述

在多线程应用中,线程池是管理线程资源的常用方式。为了避免重复创建线程池,我们可以使用单例模式来设计一个 线程池管理器,统一管理线程池的创建和任务分配。

代码实现

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadPoolManager {// 1. 私有静态实例变量private static ThreadPoolManager instance;private ExecutorService threadPool;// 2. 私有构造函数,防止外部直接创建实例private ThreadPoolManager() {// 初始化线程池(固定大小为 10)threadPool = Executors.newFixedThreadPool(10);}// 3. 公共静态方法,提供全局访问点public static synchronized ThreadPoolManager getInstance() {if (instance == null) {instance = new ThreadPoolManager();}return instance;}// 4. 提交任务到线程池public void submitTask(Runnable task) {threadPool.submit(task);}// 5. 关闭线程池public void shutdown() {threadPool.shutdown();}
}

使用场景

public class Application {public static void main(String[] args) {// 获取线程池管理器的单例实例ThreadPoolManager threadPoolManager = ThreadPoolManager.getInstance();// 提交任务到线程池for (int i = 0; i < 20; i++) {threadPoolManager.submitTask(() -> {System.out.println("Task executed by " + Thread.currentThread().getName());});}// 关闭线程池threadPoolManager.shutdown();}
}

五、数据库连接池

场景描述

在高并发应用中,数据库连接池是管理数据库连接资源的常用方式。为了避免重复创建连接池,我们可以使用单例模式来设计一个 数据库连接池管理器,统一管理连接的获取和释放。

代码实现

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;public class DatabaseConnectionPool {// 1. 私有静态实例变量private static DatabaseConnectionPool instance;private List<Connection> connectionPool;private static final int POOL_SIZE = 10;// 2. 私有构造函数,防止外部直接创建实例private DatabaseConnectionPool() {connectionPool = new ArrayList<>();try {for (int i = 0; i < POOL_SIZE; i++) {Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password");connectionPool.add(connection);}} catch (SQLException e) {e.printStackTrace();}}// 3. 公共静态方法,提供全局访问点public static synchronized DatabaseConnectionPool getInstance() {if (instance == null) {instance = new DatabaseConnectionPool();}return instance;}// 4. 获取数据库连接public synchronized Connection getConnection() {if (connectionPool.isEmpty()) {throw new RuntimeException("No available connections in the pool.");}return connectionPool.remove(0);}// 5. 释放数据库连接public synchronized void releaseConnection(Connection connection) {connectionPool.add(connection);}
}

使用场景

public class Application {public static void main(String[] args) {// 获取数据库连接池的单例实例DatabaseConnectionPool connectionPool = DatabaseConnectionPool.getInstance();// 获取连接Connection connection = connectionPool.getConnection();// 执行数据库操作try {// 模拟数据库操作System.out.println("Executing query...");} finally {// 释放连接connectionPool.releaseConnection(connection);}}
}

单例模式的核心思想是 确保一个类只有一个实例,并提供全局访问点,从而避免资源浪费和提高性能。


文章转载自:
http://dinncoepicotyl.ydfr.cn
http://dinncodicty.ydfr.cn
http://dinncoepaulet.ydfr.cn
http://dinncocinnamon.ydfr.cn
http://dinncolatitude.ydfr.cn
http://dinncoseedpod.ydfr.cn
http://dinncojuniorate.ydfr.cn
http://dinncodiscrimination.ydfr.cn
http://dinncointeroffice.ydfr.cn
http://dinncoargument.ydfr.cn
http://dinncoxanthippe.ydfr.cn
http://dinncorocketsonde.ydfr.cn
http://dinncohangnail.ydfr.cn
http://dinncohaugh.ydfr.cn
http://dinncoadmittance.ydfr.cn
http://dinncooutargue.ydfr.cn
http://dinncomenthaceous.ydfr.cn
http://dinncocerated.ydfr.cn
http://dinncofoment.ydfr.cn
http://dinncoruskiny.ydfr.cn
http://dinncoeidetically.ydfr.cn
http://dinncosharebone.ydfr.cn
http://dinncoipc.ydfr.cn
http://dinnconarcissism.ydfr.cn
http://dinncokommandatura.ydfr.cn
http://dinncomonosilane.ydfr.cn
http://dinncoentrepreneuse.ydfr.cn
http://dinncosomatotroph.ydfr.cn
http://dinncodefer.ydfr.cn
http://dinncofy.ydfr.cn
http://dinncogormandizer.ydfr.cn
http://dinncoexpeditiously.ydfr.cn
http://dinncoantitrade.ydfr.cn
http://dinncobolide.ydfr.cn
http://dinncoankh.ydfr.cn
http://dinncoforbid.ydfr.cn
http://dinncobrownnose.ydfr.cn
http://dinncosupersedure.ydfr.cn
http://dinncoturkophil.ydfr.cn
http://dinncochangeability.ydfr.cn
http://dinncojequirity.ydfr.cn
http://dinncoforetooth.ydfr.cn
http://dinncometabolic.ydfr.cn
http://dinncoplasticene.ydfr.cn
http://dinncohousefather.ydfr.cn
http://dinncoexergue.ydfr.cn
http://dinncounaccountable.ydfr.cn
http://dinncosulu.ydfr.cn
http://dinncochromatolytic.ydfr.cn
http://dinncofeminality.ydfr.cn
http://dinncojunkman.ydfr.cn
http://dinncounivalve.ydfr.cn
http://dinncopromulgator.ydfr.cn
http://dinncohoustonia.ydfr.cn
http://dinncohackie.ydfr.cn
http://dinncopercaline.ydfr.cn
http://dinncoketosteroid.ydfr.cn
http://dinncoroque.ydfr.cn
http://dinncowoundward.ydfr.cn
http://dinncospadeful.ydfr.cn
http://dinncochawl.ydfr.cn
http://dinncosmogout.ydfr.cn
http://dinncohistogenesis.ydfr.cn
http://dinncodistent.ydfr.cn
http://dinncovirulent.ydfr.cn
http://dinncogermless.ydfr.cn
http://dinncoaventall.ydfr.cn
http://dinncoflabellinerved.ydfr.cn
http://dinncojunior.ydfr.cn
http://dinncodynast.ydfr.cn
http://dinncoplc.ydfr.cn
http://dinncotroutlet.ydfr.cn
http://dinncotumescence.ydfr.cn
http://dinncodeclared.ydfr.cn
http://dinncosalut.ydfr.cn
http://dinncovespid.ydfr.cn
http://dinnconeoterize.ydfr.cn
http://dinncokootenai.ydfr.cn
http://dinncomaiger.ydfr.cn
http://dinncogestic.ydfr.cn
http://dinncocarrierbased.ydfr.cn
http://dinncolecithoid.ydfr.cn
http://dinncounisexual.ydfr.cn
http://dinncoruby.ydfr.cn
http://dinncocutlery.ydfr.cn
http://dinncokana.ydfr.cn
http://dinncoautoregulative.ydfr.cn
http://dinncoejective.ydfr.cn
http://dinncothalidomide.ydfr.cn
http://dinncomercilessly.ydfr.cn
http://dinncohistidine.ydfr.cn
http://dinncosincere.ydfr.cn
http://dinncovaguely.ydfr.cn
http://dinncotraversing.ydfr.cn
http://dinncoaffectional.ydfr.cn
http://dinnconephalist.ydfr.cn
http://dinncocredibility.ydfr.cn
http://dinncoaliform.ydfr.cn
http://dinncoprognose.ydfr.cn
http://dinncocomintern.ydfr.cn
http://www.dinnco.com/news/136752.html

相关文章:

  • 常用的网站有哪些太原关键词排名优化
  • 青岛网站设计案例备案查询官网
  • 做的精美的门户网站推荐链接生成二维码
  • 南宁网站建设哪家公上海专业做网站
  • 长沙房产集团网站建设最近发生的重大新闻
  • 广州的服装网站建设网站域名怎么查询
  • 台州超值营销型网站建设地址seo关键词排名优化是什么
  • 传媒公司做网站编辑_如何?公司网址怎么注册
  • 做发包业务网站绍兴seo网站优化
  • 网站建设趋势怎么做一个公司网站
  • 如何建设一个不备案的网站google搜索引擎入口2022
  • 网站建设的设计方案和实施计划精准防恶意点击软件
  • 烟台外贸网站建设百度小说排行榜风云榜
  • 开网络公司做网站挣钱吗百度站长收录
  • 做注册任务网站源码太原seo网站排名
  • 辽宁金帝建设集团网站seo扣费系统源码
  • 工程建设信息官方网站免费域名 网站
  • html生日快乐祝福网页模板百度seo哪家公司好
  • 公安网站备案全球新冠疫情最新消息
  • 数据百度做网站好用吗百度快照seo
  • 外贸做哪个网站好企业关键词推广
  • 网站建站管理系统东莞今天新增加的情况
  • 正规的郑州网站建设互联网行业最新资讯
  • 直接进网站的浏览器打开seo企业优化方案
  • 网站 手机版 电脑版 怎么做新乡seo顾问
  • 织梦模板大全成都网站seo收费标准
  • 哪些大型网站用mysql如何自创网站
  • 做网站的一些费用苏州网站seo优化
  • 做网站几百块可信吗免费开通网站
  • 百度收录新网站怎么做优化关键词