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:

You can safely call Pattern.matcher() on the same pattern from different threads and safely use the matchers concurrently.

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的更多相关文章

  1. Regular Expressions --正则表达式官方教程

    http://docs.oracle.com/javase/tutorial/essential/regex/index.html This lesson explains how to use th ...

  2. Effective Java 70 Document thread safety

    Principle The presence of the synchronized modifier in a method declaration is an implementation det ...

  3. 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 ...

  4. Thread Safety in Java(java中的线程安全)

    Thread Safety in Java is a very important topic. Java provide multi-threaded environment support usi ...

  5. Introducing Regular Expressions 学习笔记

    Introducing Regular Expressions 读书笔记 工具: regexbuddy:http://download.csdn.net/tag/regexbuddy%E7%A0%B4 ...

  6. 正则表达式(Regular expressions)使用笔记

    Regular expressions are a powerful language for matching text patterns. This page gives a basic intr ...

  7. 线程安全 Thread Safety Problem scala concurrency 并发

    小结: 1.基于java并发模型 Scala concurrency is built on top of the Java concurrency model. 2. 将每个请求放入一个新的线程 T ...

  8. 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 ...

  9. Thread Safety线程安全

    Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分 如果disabled就选择nts(php_stomp-1.0.9-5.5-nts-vc11-x86.zi ...

随机推荐

  1. 关于新装ubuntu系统update失败和build-essential失败的解决办法

    我是12月4日在新电脑上的vmware-workstation 10 上安装的ubuntu14.04LTS,但安装后再校园环境下总是build-essential失败,上网一查,说是要先update, ...

  2. linux 程序运行监控

    一 . 使用supervise 是daemon-tools 的一个功能,系统的守护进程.在进程挂掉的时候可以自动重启. 二   安装 wget http://cr.yp.to/daemontools/ ...

  3. Linux下安装QT和OpenGL后QT无法使用OpenGL的解决方法

    我的系统为Ubuntu14.04,用apt-get安装了实现了OpenGl的mesa,QT则是用官网下载的run文件来安装的. 好了,现在两个都分别有了,所以要在qt下尝试写OpenGl代码. 之前试 ...

  4. Html 笔记1

    标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. <h1>这是标题</h1> 段落是通过 <p> 标签进行定义的. ...

  5. 微信网页授权java实现

    功能:主要用于在用户通过手机端微信访问第三方H5页面时获取用户的身份信息(openId,昵称,头像,所在地等..)可用来实现微信登录.微信账号绑定.用户身份鉴权等功能.     开发前的准备: 1.需 ...

  6. LINUX查看硬件配置命令

    LINUX查看硬件配置命令   系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinf ...

  7. Oracle EBS-SQL (GL-3):从总帐追溯到发票

    SELECT je_header_id, je_line_num, trx_class_name, trx_type_name, trx_number_displayed, trx_date,comm ...

  8. 不能取组织ID

    应用 Oracle   Purchasing 层 Level Function 函数名 Funcgtion Name PO_POXBWVRP 表单名 Form Name POXBWVRP 说明 Des ...

  9. Silverlight第三方控件专题

    原文http://www.cnblogs.com/nasa/archive/2008/12/01/1344927.html 这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏 ...

  10. java实现二维码生成的几个方法

    1: 使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.jp/projects/qrcode/ ...