正则表达式、JAVA线程

前端之家收集整理的这篇文章主要介绍了正则表达式、JAVA线程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

领卓教育培训第十四天

正则表达式

定义

指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。
因此,典型的调用顺序是

  1. Pattern p = Pattern.compile("a*b");
  2. Matcher m = p.matcher("aaaaab");
  3. boolean b = m.matches();

构造摘要

  1. 字符
    x 字符 x
    \ 反斜线字符、
  2. 字符类
    [abc] a、b 或 c(简单类)
    [^abc] 任何字符,除了 a、b 或 c(否定)
    [a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
    [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
    [a-z&&[def]] d、e 或 f(交集)
    [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
    [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
  3. 预定义字符类
    . 任何字符(与行结束符可能匹配也可能不匹配)
    \d 数字:[0-9]
    \D 非数字: [^0-9]
    \s 空白字符:[ \t\n\x0B\f\r]
    \S 非空白字符:[^\s]
    \w 单词字符:[a-zA-Z_0-9]
    \W 非单词字符:[^\w]
  4. POSIX 字符类(仅 US-ASCII)
    \p{Lower} 小写字母字符:[a-z]
    \p{Upper} 大写字母字符:[A-Z]
    \p{Alpha} 字母字符:[\p{Lower}\p{Upper}]
    \p{Digit} 十进制数字:[0-9]
    \p{Alnum} 字母数字字符:[\p{Alpha}\p{Digit}]
    \p{Punct} 标点符号:!”#$%&’()*+,-./:;<=>?@[]^_`{|}~
  5. Greedy 数量
    X? X,一次或一次也没有
    X* X,零次或多次
    X+ X,一次或多次
    X{n} X,恰好 n 次
    X{n,} X,至少 n 次
    X{n,m} X,至少 n 次,但是不超过 m 次

代码示例

  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class Test {
  5. public static void main(String[] args) {
  6. //验证手机号
  7. Pattern p1 = Pattern.compile("^(13|15|17|18)\\d{9}$");//规定格式
  8. Matcher m1 = p1.matcher("13140168161");//要进行验证的字符串
  9. boolean b1 = m1.matches();
  10. System.out.println(b1);
  11. //验证身份证号
  12. Pattern p2 = Pattern.compile("^\\d{17}((\\d{1})|X)$");//规定格式
  13. Matcher m2 = p2.matcher("13140168161452156X");//要进行验证的字符串
  14. boolean b2 = m2.matches();
  15. System.out.println(b2);
  16. //验证密码
  17. Pattern p3 = Pattern.compile("^(\\p{Alnum}){8,16}$");//规定格式
  18. Matcher m3 = p3.matcher("131ASD68ad145215");//要进行验证的字符串
  19. boolean b3 = m3.matches();
  20. System.out.println(b3);
  21. //验证网址
  22. Pattern p4 = Pattern.compile("^w{3}\\.(\\p{Alnum})+\\.((com)|(cn)|(net)(org)$");//规定格式
  23. Matcher m4 = p4.matcher("www.baidu.com");//要进行验证的字符串
  24. boolean b4 = m4.matches();
  25. System.out.println(b4);
  26. //验证邮箱
  27. Pattern p5 = Pattern.compile("^\\w+@\\w+(.com|.cn|.net){1,2}$");//规定格式
  28. Matcher m5 = p5.matcher("yadg12_dt@1yt.com.cn");//要进行验证的字符串
  29. boolean b5 = m5.matches();
  30. System.out.println(b5);
  31. }
  32. }

线程

简介

  1. 线程 是程序中的执行线程。Java 虚拟机允许应用程序并发地运行多个执行线程。
  2. 每个线程都有一个优先级,高优先级线程的执行优先于低优先级线程。每个线程都可以或不可以标记为一个守护程序。当某个线程中运行的代码创建一个新 Thread 对象时,该新线程的初始优先级被设定为创建线程的优先级,并且当且仅当创建线程是守护线程时,新线程才是守护程序。

创建线程

  1. 方法一:将类声明为 Thread 的子类。该子类应重写 Thread 类的 run 方法。接下来可以分配并启动该子类的实例。
    创建SellThread类
  1. public class SellThread extends Thread{
  2. @Override
  3. public void run() {
  4. for (int i = 1; i <=10; i++) {
  5. try {
  6. Thread.sleep(1000);
  7. } catch (InterruptedException e) {
  8. e.printStackTrace();
  9. }
  10. System.out.println(Thread.currentThread().toString()+i);
  11. }
  12. }
  13. }

创建Test类

  1. public class Test {
  2. public static void main(String[] args) {
  3. SellThread s1 = new SellThread();
  4. SellThread s2 = new SellThread();
  5. s1.start();
  6. s2.start();
  7. }
  8. }
  1. 方法二:声明实现 Runnable 接口的类。该类然后实现 run 方法。然后可以分配该类的实例,在创建 Thread 时作为一个参数来传递并启动。
    创建SellThread类
  1. public class SellThread implements Runnable {
  2. int i = 1;
  3. String s = "abc";
  4.  
  5. @Override
  6. public void run() {
  7. while (i <= 10) {
  8. try {
  9. Thread.sleep(1000);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. /*synchronized (s) {//同步代码块 if (i <= 10) { System.out.println(Thread.currentThread().toString() + i); i++; } }*/
  14. sell();
  15. }
  16. }
  17. private synchronized void sell(){ //同步方法
  18. if (i <= 10) {
  19. System.out.println(Thread.currentThread().toString() + i);
  20. i++;
  21. }
  22. }
  23. }

创建Test类

  1. public class Test {
  2. public static void main(String[] args) {
  3. SellThread s = new SellThread();//共享数据
  4. Thread t1 = new Thread(s);
  5. Thread t2 = new Thread(s);
  6. t1.start();
  7. t2.start();
  8. }
  9. }

代码示例

有一个银行账户,两个人到这个账户取钱,每次取100元,余额不足100元,则不能取出!
创建Acccount类

  1. public class Acccount implements Runnable {
  2. Integer yue = 1090;
  3. String s = "abc";
  4.  
  5. @Override
  6. public void run() {
  7.  
  8. while(yue>=100) {
  9. try {
  10. Thread.sleep(500);
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. synchronized (s) {
  15.  
  16. if (yue >= 100) {
  17. System.out.println(Thread.currentThread().toString()+"您已成功取出RMB:100元!");
  18. } else {
  19. System.out.println(Thread.currentThread().toString()+"对不起,您的余额不足!");
  20. }
  21. yue = yue - 100;
  22. }
  23. }
  24. }
  25. }

创建Test类

  1. public class Test {
  2. public static void main(String[] args) {
  3. Acccount a = new Acccount();
  4. Thread t1= new Thread(a);
  5. Thread t2= new Thread(a);
  6. t1.start();
  7. t2.start();
  8. }
  9. }

运行结果:

死锁

创建SyncThread类

  1. public class SyncThread implements Runnable {
  2. private String s1;
  3. private String s2;
  4.  
  5. public SyncThread(String o1,String o2) {
  6. this.s1 = o1;
  7. this.s2 = o2;
  8. }
  9.  
  10. @Override
  11. public void run() {
  12. String name = Thread.currentThread().getName();//得到当前进程的名称
  13. System.out.println(name + "刚进Run的锁" + s1);
  14. synchronized (s1) {
  15. System.out.println(name + "现在持有的锁" + s1);
  16. sleep();
  17. System.out.println(name + "等待的锁" + s2);
  18. synchronized (s2) {
  19. System.out.println(name + "内部持有的锁" + s2);
  20. sleep();
  21. }
  22. System.out.println(name+"释放的内部锁"+s2);
  23. }
  24. System.out.println(name+"释放的外部锁"+s1);
  25. System.out.println(name+"完成执行");
  26. }
  27.  
  28. private void sleep() {
  29. try {
  30. Thread.sleep(2000);
  31. } catch (InterruptedException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }

创建ThreadDeadLocked类

  1. public class ThreadDeadLocked {
  2. public static void main(String[] args) {
  3. String s1="a";
  4. String s2="b";
  5. String s3="c";
  6. SyncThread st1= new SyncThread(s1,s2);
  7. SyncThread st2= new SyncThread(s2,s3);
  8. SyncThread st3= new SyncThread(s3,s1);
  9. Thread t1 = new Thread(st1);
  10. Thread t2 = new Thread(st2);
  11. Thread t3 = new Thread(st3);
  12. t1.start();
  13. try {
  14. Thread.sleep(1000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. t2.start();
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. t3.start();
  25. }
  26. }

运行结果:程序阻塞

Join()

简介

public final void join()throws InterruptedException
等待该线程终止。
抛出: InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的中断状态 被清除。

代码示例

  1. public class MyThread1 implements Runnable{
  2.  
  3. @Override
  4. public void run() {
  5.  
  6. System.out.println("线程1开始");
  7. try {
  8. Thread.sleep(5000);
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. }
  12. System.out.println("线程1结束");
  13. }
  14. }
  1. public class MyThread2 implements Runnable {
  2.  
  3. @Override
  4. public void run() {
  5. System.out.println("线程2开始");
  6. System.out.println("线程2结束");
  7. }
  8. }
  1. public class Test { //join
  2. public static void main(String[] args) {
  3. MyThread1 myThread1 = new MyThread1();
  4. MyThread2 myThread2= new MyThread2();
  5. Thread t1 = new Thread(myThread1);
  6. Thread t2 = new Thread(myThread2);
  7. t1.start();
  8. try {
  9. t1.join();//必须等到t1执行完,之后再执行t2
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. t2.start();
  14. }
  15. }

运行结果:

wait()和notify()

简介

这两个方法在java.lang.object类里边
1. public final void wait()throws InterruptedException
当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。
2. public final void notify()唤醒在此对象监视器上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程。选择是任意性的,并在对实现做出决定时发生。线程通过调用其中一个 wait 方法,在对象的监视器上等待。
直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程。被唤醒的线程将以常规方式与在该对象上主动同步的其他所有线程进行竞争。

代码示例

创建MyThread1类

  1. public class MyThread1 implements Runnable{
  2. private String s="abc";
  3.  
  4. @Override
  5. public void run() {
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. synchronized (s) {
  12. try {
  13. System.out.println(Thread.currentThread().getName()+"开始等待");
  14. s.wait();
  15. System.out.println(Thread.currentThread().getName()+"结束等待");
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. }
  22. }

创建

  1. public class MyThread2 implements Runnable {
  2. private String s="abc";
  3.  
  4. @Override
  5. public void run() {
  6. try {
  7. Thread.sleep(2000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. synchronized (s) {
  12. System.out.println(Thread.currentThread().getName()+"唤醒前");
  13. try {
  14. Thread.sleep(5000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. s.notify();
  19. }
  20. System.out.println(Thread.currentThread().getName()+"唤醒后");
  21. }
  22. }

创建Test类

  1. public class Test { //wait()和notify()的用法
  2. public static void main(String[] args) {
  3. MyThread1 myThread1 = new MyThread1();
  4. MyThread2 myThread2= new MyThread2();
  5. Thread t1 = new Thread(myThread1);
  6. Thread t2 = new Thread(myThread2);
  7. t1.start();
  8. t2.start();
  9. }
  10. }

运行结果:

生产者和消费者问题

生产者与消费者模型中,要保证以下几点:
1. 同一时间内只能有一个生产者生产
2. 同一时间内只能有一个消费者消费
3. 生产者生产的同时消费者不能消费
4. 消费者消费的同时生产者不能生产
5. 共享空间空时消费者不能继续消费。消费前判断是否为空,空的话将该线程wait,释放锁允许其他同步方法执行
6. 共享空间满时生产者不能继续生产。生产前判断是否为满,满的话将该线程wait,释放锁允许其他同步方法执行

代码示例

创建MakeProductThread类

  1. //生产产品
  2. public class MakeProductThread implements Runnable {
  3. private ProductData product;
  4. public MakeProductThread(ProductData product){
  5. this.product=product;
  6. }
  7.  
  8. @Override
  9. public void run() {
  10. while(true){
  11. product.make();
  12. }
  13. }
  14. }

创建ConsumeProductThread类

  1. //消费产品
  2. public class ConsumeProductThread implements Runnable{
  3. private ProductData product;
  4. public ConsumeProductThread(ProductData product){
  5. this.product=product;
  6. }
  7.  
  8. @Override
  9. public void run() {
  10. while(true){
  11. try {
  12. Thread.sleep(1000); //消费者每次等待生产之后再消费
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. }
  16. product.consume();
  17. }
  18. }
  19. }

创建ProductData类

  1. //产品数据共享
  2. public class ProductData {
  3. private boolean isProduct=false;
  4. public synchronized void make(){
  5. if(isProduct){//没有产品,开始生产
  6. try {
  7. wait();
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. System.out.println("开始生产产品");
  13. try {
  14. Thread.sleep(2000);
  15. } catch (InterruptedException e) {
  16. e.printStackTrace();
  17. }
  18. System.out.println("生产了一件产品");
  19. isProduct=true;
  20. notify();//有产品,通知消费
  21. }
  22. public synchronized void consume(){
  23. if(!isProduct){//没有产品,等待生产
  24. System.out.println("等待商家生产产品");
  25. try {
  26. wait();
  27. } catch (InterruptedException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. System.out.println("商家生产完成");
  32. System.out.println("开始消费产品");
  33. try {
  34. Thread.sleep(1000);
  35. } catch (InterruptedException e) {
  36. e.printStackTrace();
  37. }
  38. System.out.println("消费产品完成");
  39. System.out.println("----------");
  40. isProduct=false;
  41. notify();//消费完成,通知生产
  42. }
  43. }

创建Test类

  1. public class Test {
  2. public static void main(String[] args) {
  3. ProductData product = new ProductData();
  4. MakeProductThread mpt = new MakeProductThread(product);
  5. ConsumeProductThread cpt = new ConsumeProductThread(product);
  6. Thread t1 = new Thread(mpt);//生产
  7. Thread t2 = new Thread(cpt);//消费
  8. t1.start();
  9. try {
  10. Thread.sleep(1000);
  11. } catch (InterruptedException e) {
  12. e.printStackTrace();
  13. }
  14. t2.start();
  15. }
  16. }

运行结果:

猜你在找的正则表达式相关文章