Thread-safety with regular expressions in Java
As mentioned in our introduction to the Pattern and Matcher classes, the Java regular expression API has been designed to allow a single compiled pattern to be shared across multiple match operations. Our examples focussed on creating multiple Matchers in the same thread. But in fact:
In other words, the Pattern is a "fixed representation" of the regular expression. All of the logic around performing a match— and hence, any state that changes during a match operation— is held in the Matcher object.
It is also safe to construct matchers without synchronization (using the call to Pattern.matcher()). Although the method isn't synchronized, internal to the Pattern class, a volatile variable called compiled is always set after constructing a pattern and read at the start of the call to matcher(). This forces any thread referring to the Pattern to correctly "see" the contents of that object1.
On the other hand, you shouldn't share a Matcher between different threads. Or at least, if you ever did, you should use explicit synchronization. But it's not clear why you'd want to do that...
1. If you're not sure what is meant by correctly see, then see the section of this site on synchronization of variables with main memory.
From: http://www.javamex.com/tutorials/regular_expressions/thread_safety.shtml#.UxCAJc4rk3Y
http://stackoverflow.com/questions/9756453/multithreading-java-regex
Thread-safety with regular expressions in Java的更多相关文章
- Regular Expressions --正则表达式官方教程
http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...
- Effective Java 70 Document thread safety
Principle The presence of the synchronized modifier in a method declaration is an implementation det ...
- Java Concurrency In Practice -Chapter 2 Thread Safety
Writing thread-safe code is managing access to state and in particular to shared, mutable state. Obj ...
- Thread Safety in Java(java中的线程安全)
Thread Safety in Java is a very important topic. Java provide multi-threaded environment support usi ...
- Introducing Regular Expressions 学习笔记
Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...
- 正则表达式(Regular expressions)使用笔记
Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...
- 线程安全 Thread Safety Problem scala concurrency 并发
小结: 1.基于java并发模型 Scala concurrency is built on top of the Java concurrency model. 2. 将每个请求放入一个新的线程 T ...
- Finding Comments in Source Code Using Regular Expressions
Many text editors have advanced find (and replace) features. When I’m programming, I like to use an ...
- Thread Safety线程安全
Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分 如果disabled就选择nts(php_stomp-1.0.9-5.5-nts-vc11-x86.zi ...
随机推荐
- 《UNIX环境高级编程》笔记--线程的标识、创建和终止
1.线程标识 就像每个进程都有一个进程ID一样,每个线程都有一个线程ID.进程ID在整个系统中是唯一的,但线程ID只在它所属的 进程环境中有效. 线程ID使用pthread_t数据类型来表示,实现的时 ...
- Arduino周边模块:LED部件
Arduino周边模块:LED部件 Arduino周边模块:LED部件 1. LED的使用 LED的原理: LED是会发光的二极管,它具有单向导电性.两端加上正向电压,即能将电能转化为光能. 正向电压 ...
- submit与onsubmit(转)
发生顺序:onsubmit -> submit 1.阻止表单提单: <script>function submitFun(){ //逻辑判断 return true; / ...
- java设计模式之 单例模式 Singleton
static 的应用 单例模式 Singleton 单例:保证一个类在系统中最多只创建一个实例. 好处:由于过多创建对象实例,会产生过多的系统垃圾,需要GC频繁回收,由于GC会占用较大的系统资源,所有 ...
- GoF——职责链模式
职责链模式(chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它 ...
- 标准C编程-笔记全集
C语言的基本概念 编写一个简单的C程序,后缀名保存为c(本次文件名为a.c) gcc:对c程序进行编译和连接:gcc a.c ./a.out:运行程序,输出程序的结果:其中a是c程序的文件名 说明:其 ...
- 数据库中字段类型对应C#中的数据类型
数据库中字段类型对应C#中的数据类型:数据库 C#程序 int int32 text string bigint int64 binary System.Byte[] bit Boolean char ...
- Linux-VPN安装配置方法
VNP服务器IP地址为:192.168.6.6 一.编译安装: 注意:可能需要ppp.libcap.libcap-devel ncurses-devel RPM 包支持,如果没有请安装 libca ...
- 跟我一起学extjs5(16--各种Grid列的自己定义渲染)
跟我一起学extjs5(16--各种Grid列的自己定义渲染) Grid各列已经可以展示出来了.列的类型包含字符型,整型,浮点型,货币型,百分比型,日期型和布尔型,我自己定义了各种类型 ...
- [置顶] android 自定义圆角ImageView以及锯齿的处理
看到很多人开发过程中要使用圆角图片时,解决方法有: 1.重新绘制一张图片 2.通过布局来配置 3.通过重写View来实现 其中1,2在这里就不讲了,重点讲讲方法三的实现. 实现一:通过截取画布一个圆形 ...