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

绍兴市住房与城乡建设厅网站女教师遭网课入侵直播录屏曝光se

绍兴市住房与城乡建设厅网站,女教师遭网课入侵直播录屏曝光se,郑州高端网站建设团队,安卓手机如何做网站文章目录1. ThreadLocal介绍1.1 官方介绍1.2 基本使用1.2.1 常用方法1.2.2 使用案例1.3 ThreadLocal类与synchronized关键字1.3.1 synchronized同步方式1.3.2 ThreadLocal与synchronized的区别2. 运用场景_事务案例2.1 转账案例2.1.1 场景构建2.1.2 引入事务2.2 常规解决方案2.…

文章目录

    • 1. ThreadLocal介绍
      • 1.1 官方介绍
      • 1.2 基本使用
        • 1.2.1 常用方法
        • 1.2.2 使用案例
      • 1.3 ThreadLocal类与synchronized关键字
        • 1.3.1 synchronized同步方式
        • 1.3.2 ThreadLocal与synchronized的区别
    • 2. 运用场景_事务案例
      • 2.1 转账案例
        • 2.1.1 场景构建
        • 2.1.2 引入事务
      • 2.2 常规解决方案
        • 2.2.1 常规方案的实现
        • 2.2.2 常规方案的弊端
      • 2.3 ThreadLocal解决方案
        • 2.3.1 ThreadLocal方案的实现
        • 2.3.2 ThreadLocal方案的好处

1. ThreadLocal介绍

1.1 官方介绍

/*** This class provides thread-local variables.  These variables differ from* their normal counterparts in that each thread that accesses one (via its* {@code get} or {@code set} method) has its own, independently initialized* copy of the variable.  {@code ThreadLocal} instances are typically private* static fields in classes that wish to associate state with a thread (e.g.,* a user ID or Transaction ID).** <p>For example, the class below generates unique identifiers local to each* thread.* A thread's id is assigned the first time it invokes {@code ThreadId.get()}* and remains unchanged on subsequent calls.* <pre>* import java.util.concurrent.atomic.AtomicInteger;** public class ThreadId {*     // Atomic integer containing the next thread ID to be assigned*     private static final AtomicInteger nextId = new AtomicInteger(0);**     // Thread local variable containing each thread's ID*     private static final ThreadLocal&lt;Integer&gt; threadId =*         new ThreadLocal&lt;Integer&gt;() {*             &#64;Override protected Integer initialValue() {*                 return nextId.getAndIncrement();*         }*     };**     // Returns the current thread's unique ID, assigning it if necessary*     public static int get() {*         return threadId.get();*     }* }* </pre>* <p>Each thread holds an implicit reference to its copy of a thread-local* variable as long as the thread is alive and the {@code ThreadLocal}* instance is accessible; after a thread goes away, all of its copies of* thread-local instances are subject to garbage collection (unless other* references to these copies exist).** @author  Josh Bloch and Doug Lea* @since   1.2*/
public class ThreadLocal<T> {...

​ 从Java官方文档中的描述:ThreadLocal类用来提供线程内部的局部变量。这种变量在多线程环境下访问(通过get和set方法访问)时能保证各个线程的变量相对独立于其他线程内的变量。ThreadLocal实例通常来说都是private static类型的,用于关联线程和线程上下文。

我们可以得知 ThreadLocal 的作用是:提供线程内的局部变量,不同的线程之间
不会相互干扰,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数
或组件之间一些公共变量传递的复杂度。
总结:
1. 线程并发: 在多线程并发的场景下
2. 传递数据: 我们可以通过ThreadLocal在同一线程,不同组件中传递公共变量
3. 线程隔离: 每个线程的变量都是独立的,不会互相影响

1.2 基本使用

1.2.1 常用方法

​ 在使用之前,我们先来认识几个ThreadLocal的常用方法

方法声明描述
ThreadLocal()创建ThreadLocal对象
public void set( T value)设置当前线程绑定的局部变量
public T get()获取当前线程绑定的局部变量
public void remove()移除当前线程绑定的局部变量

1.2.2 使用案例

我们来看下面这个案例, 感受一下ThreadLocal 线程隔离的特点: 
public class MyDemo {private String content;private String getContent() {return content;}private void setContent(String content) {this.content = content;}public static void main(String[] args) {MyDemo demo = new MyDemo();for (int i = 0; i < 5; i++) {Thread thread = new Thread(new Runnable() {@Overridepublic void run() {demo.setContent(Thread.currentThread().getName() + "的数据");System.out.println("-----------------------");System.out.println(Thread.currentThread().getName() + "--->" + demo.getContent());}});thread.setName("线程" + i);thread.start();}}
}

打印结果:
在这里插入图片描述

​ 从结果可以看出多个线程在访问同一个变量的时候出现的异常,线程间的数据没有隔离。下面我们来看下采用 ThreadLocal 的方式来解决这个问题的例子。

public class MyDemo {private static ThreadLocal<String> tl = new ThreadLocal<>();private String content;private String getContent() {return tl.get();}private void setContent(String content) {tl.set(content);}public static void main(String[] args) {MyDemo demo = new MyDemo();for (int i = 0; i < 5; i++) {Thread thread = new Thread(new Runnable() {@Overridepublic void run() {demo.setContent(Thread.currentThread().getName() + "的数据");System.out.println("-----------------------");System.out.println(Thread.currentThread().getName() + "--->" + demo.getContent());}});thread.setName("线程" + i);thread.start();}}
}

打印结果:
在这里插入图片描述

从结果来看,这样很好的解决了多线程之间数据隔离的问题,十分方便。

1.3 ThreadLocal类与synchronized关键字

1.3.1 synchronized同步方式

​ 这里可能有的朋友会觉得在上述例子中我们完全可以通过加锁来实现这个功能。我们首先来看一下用synchronized代码块实现的效果:

public class Demo02 {private String content;public String getContent() {return content;}public void setContent(String content) {this.content = content;}public static void main(String[] args) {Demo02 demo02 = new Demo02();for (int i = 0; i < 5; i++) {Thread t = new Thread(){@Overridepublic void run() {synchronized (Demo02.class){demo02.setContent(Thread.currentThread().getName() + "的数据");System.out.println("-------------------------------------");String content = demo02.getContent();System.out.println(Thread.currentThread().getName() + "--->" + content);}}};t.setName("线程" + i);t.start();}}
}

打印结果:

​			[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eiSxFVJd-1677730042406)(img\007.png)]

​ 从结果可以发现, 加锁确实可以解决这个问题,但是在这里我们强调的是线程数据隔离的问题,并不是多线程共享数据的问题, 在这个案例中使用synchronized关键字是不合适的。

1.3.2 ThreadLocal与synchronized的区别

​ 虽然ThreadLocal模式与synchronized关键字都用于处理多线程并发访问变量的问题, 不过两者处理问题的角度和思路不同。

synchronizedThreadLocal
原理同步机制采用’以时间换空间’的方式, 只提供了一份变量,让不同的线程排队访问ThreadLocal采用’以空间换时间’的方式, 为每一个线程都提供了一份变量的副本,从而实现同时访问而相不干扰
侧重点多个线程之间访问资源的同步多线程中让每个线程之间的数据相互隔离
总结: 在刚刚的案例中,虽然使用ThreadLocal和synchronized都能解决问题,
但是使用ThreadLocal更为合适,因为这样可以使程序拥有更高的并发性。

2. 运用场景_事务案例

​ 通过以上的介绍,我们已经基本了解ThreadLocal的特点。但是它具体是运用在什么场景中呢? 接下来让我们看一个案例: 事务操作。

2.1 转账案例

2.1.1 场景构建

​ 这里我们先构建一个简单的转账场景: 有一个数据表account,里面有两个用户Jack和Rose,用户Jack 给用户Rose 转账。

​ 案例的实现主要用mysql数据库,JDBC 和 C3P0 框架。以下是详细代码 :

​ (1) 项目结构

在这里插入图片描述

​ (2) 数据准备

-- 使用数据库
use test;
-- 创建一张账户表
create table account(id int primary key auto_increment,name varchar(20),money double
);
-- 初始化数据
insert into account values(null, 'Jack', 1000);
insert into account values(null, 'Rose', 0);

​ (3) C3P0配置文件和工具类

<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config><!--  连接参数 --><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password">1234</property><!-- 连接池参数 --><property name="initialPoolSize">5</property><property name="maxPoolSize">10</property><property name="checkoutTimeout">3000</property>
</default-config></c3p0-config>

​ (4) 工具类 : JdbcUtils

package com.itheima.transfer.utils;import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.SQLException;public class JdbcUtils {// c3p0 数据库连接池对象属性private static final ComboPooledDataSource ds = new ComboPooledDataSource();// 获取连接public static Connection getConnection() throws SQLException {return ds.getConnection();}//释放资源public static void release(AutoCloseable... ios){for (AutoCloseable io : ios) {if(io != null){try {io.close();} catch (Exception e) {e.printStackTrace();}}}}public static void commitAndClose(Connection conn) {try {if(conn != null){//提交事务conn.commit();//释放连接conn.close();}} catch (SQLException e) {e.printStackTrace();}}public static void rollbackAndClose(Connection conn) {try {if(conn != null){//回滚事务conn.rollback();//释放连接conn.close();}} catch (SQLException e) {e.printStackTrace();}}
}

​ (5) dao层代码 : AccountDao

package com.itheima.transfer.dao;import com.itheima.transfer.utils.JdbcUtils;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class AccountDao {public void out(String outUser, int money) throws SQLException {String sql = "update account set money = money - ? where name = ?";Connection conn = JdbcUtils.getConnection();PreparedStatement pstm = conn.prepareStatement(sql);pstm.setInt(1,money);pstm.setString(2,outUser);pstm.executeUpdate();JdbcUtils.release(pstm,conn);}public void in(String inUser, int money) throws SQLException {String sql = "update account set money = money + ? where name = ?";Connection conn = JdbcUtils.getConnection();PreparedStatement pstm = conn.prepareStatement(sql);pstm.setInt(1,money);pstm.setString(2,inUser);pstm.executeUpdate();JdbcUtils.release(pstm,conn);}
}

​ (6) service层代码 : AccountService

package com.itheima.transfer.service;import com.itheima.transfer.dao.AccountDao;
import java.sql.SQLException;public class AccountService {public boolean transfer(String outUser, String inUser, int money) {AccountDao ad = new AccountDao();try {// 转出ad.out(outUser, money);// 转入ad.in(inUser, money);} catch (Exception e) {e.printStackTrace();return false;}return true;}
}

​ (7) web层代码 : AccountWeb

package com.itheima.transfer.web;import com.itheima.transfer.service.AccountService;public class AccountWeb {public static void main(String[] args) {// 模拟数据 : Jack 给 Rose 转账 100String outUser = "Jack";String inUser = "Rose";int money = 100;AccountService as = new AccountService();boolean result = as.transfer(outUser, inUser, money);if (result == false) {System.out.println("转账失败!");} else {System.out.println("转账成功!");}}
}

2.1.2 引入事务

​ 案例中的转账涉及两个DML操作: 一个转出,一个转入。这些操作是需要具备原子性的,不可分割。不然就有可能出现数据修改异常情况。

public class AccountService {public boolean transfer(String outUser, String inUser, int money) {AccountDao ad = new AccountDao();try {// 转出ad.out(outUser, money);// 模拟转账过程中的异常int i = 1/0;// 转入ad.in(inUser, money);} catch (Exception e) {e.printStackTrace();return false;}return true;}
}

​ 所以这里就需要操作事务,来保证转出和转入操作具备原子性,要么同时成功,要么同时失败。

(1) JDBC中关于事务的操作的api

Connection接口的方法作用
void setAutoCommit(false)禁用事务自动提交(改为手动)
void commit();提交事务
void rollback();回滚事务

(2) 开启事务的注意点:

  • 为了保证所有的操作在一个事务中,案例中使用的连接必须是同一个: service层开启事务的connection需要跟dao层访问数据库的connection保持一致

  • 线程并发情况下, 每个线程只能操作各自的 connection

2.2 常规解决方案

2.2.1 常规方案的实现

基于上面给出的前提, 大家通常想到的解决方案是 :

  • 传参: 从service层将connection对象向dao层传递
  • 加锁

以下是代码实现修改的部分:

​ (1 ) AccountService 类

package com.itheima.transfer.service;import com.itheima.transfer.dao.AccountDao;
import com.itheima.transfer.utils.JdbcUtils;
import java.sql.Connection;public class AccountService {public boolean transfer(String outUser, String inUser, int money) {AccountDao ad = new AccountDao();//线程并发情况下,为了保证每个线程使用各自的connection,故加锁synchronized (AccountService.class) {Connection conn = null;try {conn = JdbcUtils.getConnection();//开启事务conn.setAutoCommit(false);// 转出ad.out(conn, outUser, money);// 模拟转账过程中的异常
//            int i = 1/0;// 转入ad.in(conn, inUser, money);//事务提交JdbcUtils.commitAndClose(conn);} catch (Exception e) {e.printStackTrace();//事务回滚JdbcUtils.rollbackAndClose(conn);return false;}return true;}}
}

​ (2) AccountDao 类 (这里需要注意的是: connection不能在dao层释放,要在service层,不然在dao层释放,service层就无法使用了)

package com.itheima.transfer.dao;import com.itheima.transfer.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class AccountDao {public void out(Connection conn, String outUser, int money) throws SQLException{String sql = "update account set money = money - ? where name = ?";//注释从连接池获取连接的代码,使用从service中传递过来的connection
//        Connection conn = JdbcUtils.getConnection();PreparedStatement pstm = conn.prepareStatement(sql);pstm.setInt(1,money);pstm.setString(2,outUser);pstm.executeUpdate();//连接不能在这里释放,service层中还需要使用
//        JdbcUtils.release(pstm,conn);JdbcUtils.release(pstm);}public void in(Connection conn, String inUser, int money) throws SQLException {String sql = "update account set money = money + ? where name = ?";
//        Connection conn = JdbcUtils.getConnection();PreparedStatement pstm = conn.prepareStatement(sql);pstm.setInt(1,money);pstm.setString(2,inUser);pstm.executeUpdate();
//        JdbcUtils.release(pstm,conn);JdbcUtils.release(pstm);}
}

2.2.2 常规方案的弊端

上述方式我们看到的确按要求解决了问题,但是仔细观察,会发现这样实现的弊端:

  1. 直接从service层传递connection到dao层, 造成代码耦合度提高

  2. 加锁会造成线程失去并发性,程序性能降低

2.3 ThreadLocal解决方案

2.3.1 ThreadLocal方案的实现

像这种需要在项目中进行数据传递线程隔离的场景,我们不妨用ThreadLocal来解决:

​ (1) 工具类的修改: 加入ThreadLocal

package com.itheima.transfer.utils;import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.SQLException;public class JdbcUtils {//ThreadLocal对象 : 将connection绑定在当前线程中private static final ThreadLocal<Connection> tl = new ThreadLocal();// c3p0 数据库连接池对象属性private static final ComboPooledDataSource ds = new ComboPooledDataSource();// 获取连接public static Connection getConnection() throws SQLException {//取出当前线程绑定的connection对象Connection conn = tl.get();if (conn == null) {//如果没有,则从连接池中取出conn = ds.getConnection();//再将connection对象绑定到当前线程中tl.set(conn);}return conn;}//释放资源public static void release(AutoCloseable... ios) {for (AutoCloseable io : ios) {if (io != null) {try {io.close();} catch (Exception e) {e.printStackTrace();}}}}public static void commitAndClose() {try {Connection conn = getConnection();//提交事务conn.commit();//解除绑定tl.remove();//释放连接conn.close();} catch (SQLException e) {e.printStackTrace();}}public static void rollbackAndClose() {try {Connection conn = getConnection();//回滚事务conn.rollback();//解除绑定tl.remove();//释放连接conn.close();} catch (SQLException e) {e.printStackTrace();}}
}

​ (2) AccountService类的修改:不需要传递connection对象

package com.itheima.transfer.service;import com.itheima.transfer.dao.AccountDao;
import com.itheima.transfer.utils.JdbcUtils;
import java.sql.Connection;public class AccountService {public boolean transfer(String outUser, String inUser, int money) {AccountDao ad = new AccountDao();try {Connection conn = JdbcUtils.getConnection();//开启事务conn.setAutoCommit(false);// 转出 : 这里不需要传参了 !ad.out(outUser, money);// 模拟转账过程中的异常
//            int i = 1 / 0;// 转入ad.in(inUser, money);//事务提交JdbcUtils.commitAndClose();} catch (Exception e) {e.printStackTrace();//事务回滚JdbcUtils.rollbackAndClose();return false;}return true;}
}

​ (3) AccountDao类的修改:照常使用

package com.itheima.transfer.dao;import com.itheima.transfer.utils.JdbcUtils;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;public class AccountDao {public void out(String outUser, int money) throws SQLException {String sql = "update account set money = money - ? where name = ?";Connection conn = JdbcUtils.getConnection();PreparedStatement pstm = conn.prepareStatement(sql);pstm.setInt(1,money);pstm.setString(2,outUser);pstm.executeUpdate();//照常使用
//        JdbcUtils.release(pstm,conn);JdbcUtils.release(pstm);}public void in(String inUser, int money) throws SQLException {String sql = "update account set money = money + ? where name = ?";Connection conn = JdbcUtils.getConnection();PreparedStatement pstm = conn.prepareStatement(sql);pstm.setInt(1,money);pstm.setString(2,inUser);pstm.executeUpdate();
//        JdbcUtils.release(pstm,conn);JdbcUtils.release(pstm);}
}

2.3.2 ThreadLocal方案的好处

从上述的案例中我们可以看到, 在一些特定场景下,ThreadLocal方案有两个突出的优势:

  1. 传递数据 : 保存每个线程绑定的数据,在需要的地方可以直接获取, 避免参数直接传递带来的代码耦合问题

  2. 线程隔离 : 各线程之间的数据相互隔离却又具备并发性,避免同步方式带来的性能损失


文章转载自:
http://vichyssoise.xsfg.cn
http://teamwork.xsfg.cn
http://haulyard.xsfg.cn
http://photorepeater.xsfg.cn
http://parral.xsfg.cn
http://hygrometry.xsfg.cn
http://trabeate.xsfg.cn
http://waterlocked.xsfg.cn
http://suspire.xsfg.cn
http://laughingly.xsfg.cn
http://cornstalk.xsfg.cn
http://excitement.xsfg.cn
http://recriminate.xsfg.cn
http://motivic.xsfg.cn
http://furbelow.xsfg.cn
http://brunch.xsfg.cn
http://ouagadougou.xsfg.cn
http://tropo.xsfg.cn
http://imbibition.xsfg.cn
http://loggerhead.xsfg.cn
http://outlandish.xsfg.cn
http://acrobat.xsfg.cn
http://suited.xsfg.cn
http://stolidly.xsfg.cn
http://endotherm.xsfg.cn
http://congregation.xsfg.cn
http://transposon.xsfg.cn
http://kami.xsfg.cn
http://july.xsfg.cn
http://poc.xsfg.cn
http://haloperidol.xsfg.cn
http://didst.xsfg.cn
http://cheops.xsfg.cn
http://retravirus.xsfg.cn
http://capric.xsfg.cn
http://chiefdom.xsfg.cn
http://veratridine.xsfg.cn
http://prelection.xsfg.cn
http://arcjet.xsfg.cn
http://sheepishly.xsfg.cn
http://brute.xsfg.cn
http://jumby.xsfg.cn
http://hyperbaric.xsfg.cn
http://calcifuge.xsfg.cn
http://lyncean.xsfg.cn
http://reposition.xsfg.cn
http://ontogenesis.xsfg.cn
http://uniterm.xsfg.cn
http://rampart.xsfg.cn
http://ventromedial.xsfg.cn
http://internuncial.xsfg.cn
http://leeway.xsfg.cn
http://tritheist.xsfg.cn
http://titrimetry.xsfg.cn
http://dicta.xsfg.cn
http://stringbark.xsfg.cn
http://humbling.xsfg.cn
http://fascicle.xsfg.cn
http://hydrokinetic.xsfg.cn
http://obduracy.xsfg.cn
http://dulcinea.xsfg.cn
http://globuliferous.xsfg.cn
http://actinia.xsfg.cn
http://urania.xsfg.cn
http://desirable.xsfg.cn
http://lavash.xsfg.cn
http://dewlap.xsfg.cn
http://autism.xsfg.cn
http://illimitable.xsfg.cn
http://glycosphingolipid.xsfg.cn
http://genre.xsfg.cn
http://inflator.xsfg.cn
http://clone.xsfg.cn
http://separatism.xsfg.cn
http://saanen.xsfg.cn
http://everest.xsfg.cn
http://gerard.xsfg.cn
http://cottonweed.xsfg.cn
http://multimillion.xsfg.cn
http://lunch.xsfg.cn
http://picker.xsfg.cn
http://landside.xsfg.cn
http://reverse.xsfg.cn
http://antialcoholism.xsfg.cn
http://begun.xsfg.cn
http://aganippe.xsfg.cn
http://nasial.xsfg.cn
http://mafia.xsfg.cn
http://oaten.xsfg.cn
http://uranography.xsfg.cn
http://ceremonialist.xsfg.cn
http://bonami.xsfg.cn
http://phonologist.xsfg.cn
http://qrp.xsfg.cn
http://catholicon.xsfg.cn
http://nectar.xsfg.cn
http://how.xsfg.cn
http://cavelike.xsfg.cn
http://thumper.xsfg.cn
http://lubricous.xsfg.cn
http://www.hrbkazy.com/news/66583.html

相关文章:

  • 深圳机票网站建设互联网营销是什么意思
  • 购物网站开发vue seo 优化方案
  • 建筑公司有哪些国内seo公司排名
  • 广东中山网站建设武汉seo关键词排名
  • 自己编辑网站怎么做的app怎么推广
  • 东莞网络推广公司旧版优化大师
  • 网站logo在哪里汕头自动seo
  • wordpress别名404上海seo外包
  • wordpress中常用插件安装谷歌seo排名技巧
  • 外国优秀网站设计宁波seo自然优化技术
  • 微网站需要备案吗培训心得简短200字
  • 沧州网络运营中心seo排名培训公司
  • 哪些网站可以找到做药人的信息怎样在百度上做广告
  • 外贸公司网站如何做网上推广网络销售平台上市公司有哪些
  • 免费做爰小说网站百度收录网站需要多久
  • 做网站学的是代码吗南宁seo专员
  • php网站开发专业介绍seo服务公司上海
  • 东莞多语言网站建设百度seo公司兴田德润
  • 如何做网站架构手机软文广告300字
  • 图文排版设计济南seo关键词优化方案
  • 海口可信的海南网站建设学seo需要学什么专业
  • 如何建设手机网站国际站seo优化是什么意思
  • 怎么自己做论坛网站互联网广告代理加盟
  • 网站的在线qq客服链接怎么做的百度站长工具添加不了站点
  • 设计做图免费网站2000元代理微信朋友圈广告
  • 北京网站的建立公司网页制作流程
  • wordpress实现mp4播放器seo网站建设是什么意思
  • 深圳做装修网站费用免费大数据网站
  • 网站置顶代码广东做seo的公司
  • 大型网站改版镇江优化推广