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 ...
随机推荐
- 6.828 lab1 bootload
MIT6.828 lab1地址:http://pdos.csail.mit.edu/6.828/2014/labs/lab1/ 第一个练习,主要是让我们熟悉汇编,嗯,没什么好说的. Part 1: P ...
- Constructor JavaScript构造器模式。
构造器模式 : Constructor模式中, 通过在构造器前面加 new 关键字, 告诉JavaScript 像使用构造器一样实例化一个新对象,并且对象成员由该函数定义. 构造器内, 使用this ...
- listview 点击条目 自动置顶或者自动置底部
关于Listview点击条目,自动滑动到点击条目实现: map_searchresult_list.post(new Runnable() { @Override public void run() ...
- redis的内部实现机制
一 理论基础 redis
- 【Howie玩docker】-Docker常用命令操作
attach 附加到一个运行的容器上面 --no-stdin=false Do not attach stdin --sig-proxy=true Proxify al ...
- vb 添加状态栏
1.新建一工程2.添加"部件" ms windows common controls 6.03.将StatusBar控件加至窗体中4.右键点击该控件,选"属性" ...
- SQL Server 权限的分类
SQL Server 的权限可以分三类 第一类 server 层面上的: select * from sys.fn_builtin_permissions(default) where class_d ...
- iOS中Blocks的介绍
1. 什么是Blocks Blocks是C语言的扩充功能.如果用一句话来概括就是:带有自动变量的匿名函数. 第一次看见Blocks的时候,感觉很类似C语言的函数指针,尤其是Block类型变量,更是有极 ...
- poj2823_单调队列简单入门
题目链接:http://poj.org/problem?id=2823 #include<iostream> #include<cstdio> #define M 100000 ...
- swift3.0 扩展、协议(4)
扩展和协议是swift中的两个特性,用于对已有的类型进行扩展和修改. 扩展(extension) 向已经存在的类型添加新的功能(属性.方法.下标脚本等等),扩展使用extension关键字定义,语法 ...