指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
因此,典型的调用顺序是
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
//验证手机号
Pattern p1 = Pattern.compile("^(13|15|17|18)\\d{9}$");//规定格式
Matcher m1 = p1.matcher("13140168161");//要进行验证的字符串
boolean b1 = m1.matches();
System.out.println(b1);
//验证身份证号
Pattern p2 = Pattern.compile("^\\d{17}((\\d{1})|X)$");//规定格式
Matcher m2 = p2.matcher("13140168161452156X");//要进行验证的字符串
boolean b2 = m2.matches();
System.out.println(b2);
//验证密码
Pattern p3 = Pattern.compile("^(\\p{Alnum}){8,16}$");//规定格式
Matcher m3 = p3.matcher("131ASD68ad145215");//要进行验证的字符串
boolean b3 = m3.matches();
System.out.println(b3);
//验证网址
Pattern p4 = Pattern.compile("^w{3}\\.(\\p{Alnum})+\\.((com)|(cn)|(net)(org)$");//规定格式
Matcher m4 = p4.matcher("www.baidu.com");//要进行验证的字符串
boolean b4 = m4.matches();
System.out.println(b4);
//验证邮箱
Pattern p5 = Pattern.compile("^\\w+@\\w+(.com|.cn|.net){1,2}$");//规定格式
Matcher m5 = p5.matcher("[email protected]");//要进行验证的字符串
boolean b5 = m5.matches();
System.out.println(b5);
}
}
public class SellThread extends Thread{
@Override
public void run() {
for (int i = 1; i <=10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().toString()+i);
}
}
}
创建Test类
public class Test {
public static void main(String[] args) {
SellThread s1 = new SellThread();
SellThread s2 = new SellThread();
s1.start();
s2.start();
}
}
public class SellThread implements Runnable {
int i = 1;
String s = "abc";
@Override
public void run() {
while (i <= 10) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/*synchronized (s) {//同步代码块 if (i <= 10) { System.out.println(Thread.currentThread().toString() + i); i++; } }*/
sell();
}
}
private synchronized void sell(){ //同步方法
if (i <= 10) {
System.out.println(Thread.currentThread().toString() + i);
i++;
}
}
}
创建Test类
public class Test {
public static void main(String[] args) {
SellThread s = new SellThread();//共享数据
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
t1.start();
t2.start();
}
}
有一个银行账户,两个人到这个账户取钱,每次取100元,余额不足100元,则不能取出!
创建Acccount类
public class Acccount implements Runnable {
Integer yue = 1090;
String s = "abc";
@Override
public void run() {
while(yue>=100) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s) {
if (yue >= 100) {
System.out.println(Thread.currentThread().toString()+"您已成功取出RMB:100元!");
} else {
System.out.println(Thread.currentThread().toString()+"对不起,您的余额不足!");
}
yue = yue - 100;
}
}
}
}
创建Test类
public class Test {
public static void main(String[] args) {
Acccount a = new Acccount();
Thread t1= new Thread(a);
Thread t2= new Thread(a);
t1.start();
t2.start();
}
}
创建SyncThread类
public class SyncThread implements Runnable {
private String s1;
private String s2;
public SyncThread(String o1, String o2) {
this.s1 = o1;
this.s2 = o2;
}
@Override
public void run() {
String name = Thread.currentThread().getName();//得到当前进程的名称
System.out.println(name + "刚进Run的锁" + s1);
synchronized (s1) {
System.out.println(name + "现在持有的锁" + s1);
sleep();
System.out.println(name + "等待的锁" + s2);
synchronized (s2) {
System.out.println(name + "内部持有的锁" + s2);
sleep();
}
System.out.println(name+"释放的内部锁"+s2);
}
System.out.println(name+"释放的外部锁"+s1);
System.out.println(name+"完成执行");
}
private void sleep() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
创建ThreadDeadLocked类
public class ThreadDeadLocked {
public static void main(String[] args) {
String s1="a";
String s2="b";
String s3="c";
SyncThread st1= new SyncThread(s1, s2);
SyncThread st2= new SyncThread(s2, s3);
SyncThread st3= new SyncThread(s3, s1);
Thread t1 = new Thread(st1);
Thread t2 = new Thread(st2);
Thread t3 = new Thread(st3);
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t3.start();
}
}
public final void join()throws InterruptedException
等待该线程终止。
抛出: InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。
public class MyThread1 implements Runnable{
@Override
public void run() {
System.out.println("线程1开始");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1结束");
}
}
public class MyThread2 implements Runnable {
@Override
public void run() {
System.out.println("线程2开始");
System.out.println("线程2结束");
}
}
public class Test { //join
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1();
MyThread2 myThread2= new MyThread2();
Thread t1 = new Thread(myThread1);
Thread t2 = new Thread(myThread2);
t1.start();
try {
t1.join();//必须等到t1执行完,之后再执行t2
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}
这两个方法在java.lang.object类里边
1. public final void wait()throws InterruptedException
当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。
2. public final void notify()唤醒在此对象监视器上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程。选择是任意性的,并在对实现做出决定时发生。线程通过调用其中一个 wait 方法,在对象的监视器上等待。
直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程。被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争。
创建MyThread1类
public class MyThread1 implements Runnable{
private String s="abc";
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s) {
try {
System.out.println(Thread.currentThread().getName()+"开始等待");
s.wait();
System.out.println(Thread.currentThread().getName()+"结束等待");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
创建
public class MyThread2 implements Runnable {
private String s="abc";
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s) {
System.out.println(Thread.currentThread().getName()+"唤醒前");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
s.notify();
}
System.out.println(Thread.currentThread().getName()+"唤醒后");
}
}
创建Test类
public class Test { //wait()和notify()的用法
public static void main(String[] args) {
MyThread1 myThread1 = new MyThread1();
MyThread2 myThread2= new MyThread2();
Thread t1 = new Thread(myThread1);
Thread t2 = new Thread(myThread2);
t1.start();
t2.start();
}
}
运行结果:
生产者与消费者模型中,要保证以下几点:
1. 同一时间内只能有一个生产者生产
2. 同一时间内只能有一个消费者消费
3. 生产者生产的同时消费者不能消费
4. 消费者消费的同时生产者不能生产
5. 共享空间空时消费者不能继续消费。消费前判断是否为空,空的话将该线程wait,释放锁允许其他同步方法执行
6. 共享空间满时生产者不能继续生产。生产前判断是否为满,满的话将该线程wait,释放锁允许其他同步方法执行
创建MakeProductThread类
//生产产品
public class MakeProductThread implements Runnable {
private ProductData product;
public MakeProductThread(ProductData product){
this.product=product;
}
@Override
public void run() {
while(true){
product.make();
}
}
}
创建ConsumeProductThread类
//消费产品
public class ConsumeProductThread implements Runnable{
private ProductData product;
public ConsumeProductThread(ProductData product){
this.product=product;
}
@Override
public void run() {
while(true){
try {
Thread.sleep(1000); //消费者每次等待生产之后再消费
} catch (InterruptedException e) {
e.printStackTrace();
}
product.consume();
}
}
}
创建ProductData类
//产品数据共享
public class ProductData {
private boolean isProduct=false;
public synchronized void make(){
if(isProduct){//没有产品,开始生产
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("开始生产产品");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("生产了一件产品");
isProduct=true;
notify();//有产品,通知消费
}
public synchronized void consume(){
if(!isProduct){//没有产品,等待生产
System.out.println("等待商家生产产品");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("商家生产完成");
System.out.println("开始消费产品");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费产品完成");
System.out.println("----------");
isProduct=false;
notify();//消费完成,通知生产
}
}
创建Test类
public class Test {
public static void main(String[] args) {
ProductData product = new ProductData();
MakeProductThread mpt = new MakeProductThread(product);
ConsumeProductThread cpt = new ConsumeProductThread(product);
Thread t1 = new Thread(mpt);//生产
Thread t2 = new Thread(cpt);//消费
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
}
}