线程和正则表达式

来源:互联网 发布:mac os x sierra壁纸 编辑:程序博客网 时间:2024/05/01 20:11

正则表达式

正则表达式通常用于判断语句中,用来检查某一字符串是否满足某一格式。
接下来用几个常用例子来说明正则表达式的具体用法。

package com.lingzhuo.Test;import java.util.regex.Matcher;import java.util.regex.Pattern;public class ZhengZe {    public static void main(String[] args) {        //邮箱    /*  Pattern p=Pattern.compile("\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}");        Matcher m=p.matcher("123@qq.com.cn");        boolean b=m.matches();        System.out.println(b);*/        //电话号码        /*Pattern p=Pattern.compile("^((13)|(15)|(18)|(17))\\d{9}$");        Matcher m=p.matcher("13140168171");        System.out.println(m.matches());*/        //身份证号        Pattern p=Pattern.compile("^\\d{17}(\\d{1}|x)$");        Matcher m=p.matcher("12345678912345678x");        System.out.println(m.matches());        //密码6到8位字母数字下划线组合        /*Pattern p=Pattern.compile("^\\w{6,8}$");        Matcher m=p.matcher("13_6zzz");        System.out.println(m.matches());*/    }}

在上边的代码中,实现了判断邮箱是否符合邮箱的基本格式,电话号码是否符合一般的电话号码形式,还有平时使用密码格式。
这里写图片描述
这里写图片描述
这里举出了常见的基本的集中正则表达式。

线程

在java中主要提供两种方式实现线程,分别为集成java.lang.Thread类与实现java.lang.Runnable接口。

继承Thread类

package com.lingzhuo.Thread;public class SellTicketsThread extends Thread{    @Override    public void run() {        // TODO Auto-generated method stub        for (int i = 1; i < 11; i++) {            try {                Thread.sleep(500);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            System.out.println(Thread.currentThread().toString()+i);        }    }}

实现runnable接口

package com.lingzhuo.Thread;public class SellRunnable implements Runnable {    int i = 100;    String str = "abc";    @Override    public void run() {        // TODO Auto-generated method stub        while (i > 0) {            try {                Thread.sleep(100);// 休眠100ms;            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            } // 四个线程进入while循环之后,先进行休眠,使得进入锁的线程执行完之后不能立即与其他三个线程抢占锁。                // (解决运行速度太快的问题)            sell();        }    }    private synchronized void sell() {        if (i > 0) {            System.out.println(Thread.currentThread().toString() + i);            i--;        }    }}

对两种方式进行测试:

package com.lingzhuo.Thread;public class ThreadTest {    public static void main(String[] args) {        //对Thread类进行测试        /*SellTicketsThread th1=new SellTicketsThread();        SellTicketsThread th2=new SellTicketsThread();        th1.start();        th2.start();*/        //对实现runnable接口进行测试        SellRunnable sr=new SellRunnable();        Thread thread1=new Thread(sr);        Thread thread2=new Thread(sr);        Thread thread3=new Thread(sr);        Thread thread4=new Thread(sr);        thread1.start();        thread2.start();        thread3.start();        thread4.start();    }}

这里写图片描述
这里写图片描述
1. 由运行结果可以看出,在继承Thread类中,会出现同一件事情被两个线程都运行了一次。
2. 为解决这样的问题,采用了实现runnable接口,实现数据共享。但是这中间还有问题就是由于线程运行较快,抢占CPU的顺序不确定,使得某些事情运行不到,而有些事情被多次运行。
3. 鉴于这种问题,这里使用了锁,当某一个线程进入run运行,其他线程只能等待。关键字为synchronized。

线程的wait()方法

package com.lingzhuo.Thread;public class WaitThread implements Runnable{    String s="abc";    public WaitThread() {        // TODO Auto-generated constructor stub    }    @Override    public void run() {        // TODO Auto-generated method stub        System.out.println("进入等待run");        synchronized (s) {            System.out.println("等待前");            try {                s.wait();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            System.out.println("等待后");        }    }}

线程的唤醒

package com.lingzhuo.Thread;public class NotifyThread implements Runnable{    String s="abc";    public NotifyThread() {        // TODO Auto-generated constructor stub    }    @Override    public void run() {        // TODO Auto-generated method stub        System.out.println("进入唤醒run");        synchronized (s) {            System.out.println("唤醒前");            try {                Thread.sleep(5000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            s.notify();            System.out.println("唤醒后");        }    }}

这里写图片描述

package com.lingzhuo.Thread;public class NotifyWaitTest {    public static void main(String[] args) {        Thread t1=new Thread(new WaitThread());        Thread t2=new Thread(new NotifyThread());        t1.start();        try {            Thread.sleep(2000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        t2.start();    }}

线程的加入

package com.lingzhuo.Thread;public class JoinTest {    public static void main(String[] args) {        Thread t=new Thread(new SellRunnable());        t.start();        try {            t.join();        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("程序运行完成");    }}

运行结果:
这里写图片描述

线程中的死锁

package com.lingzhuo.Thread;public class SiSuoThread implements Runnable {    String s1;    String s2;    public SiSuoThread(String s11,String s22){        this.s1=s11;        this.s2=s22;    }    public void run() {        synchronized(s1){            System.out.println(Thread.currentThread().toString()+"持有第一个锁"+s1);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            synchronized (s2) {                System.out.println(Thread.currentThread().toString()+"持有第二个锁"+s2);            }        }    }}
package com.lingzhuo.Thread;public class SiSuoTest {    public static void main(String[] args) {        String s1="abc";        String s2="def";        String s3="ghi";        Thread thread1=new Thread(new SiSuoThread(s1,s2), "thread1");        Thread thread2=new Thread(new SiSuoThread(s2,s3), "thread2");        Thread thread3=new Thread(new SiSuoThread(s3,s1), "thread3");        thread1.start();        try {            Thread.sleep(500);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        thread2.start();        try {            Thread.sleep(500);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        thread3.start();    }}

运行结果:
这里写图片描述

wait()和notify()方式的一个实例

生产者package com.lingzhuo.Thread;public class ProductedThread implements Runnable{    ShengchanXiaofei sf2;    public ProductedThread(ShengchanXiaofei sf) {        // TODO Auto-generated constructor stub        this.sf2=sf;    }    @Override    public void run() {        // TODO Auto-generated method stub        while(true){            sf2.product();        }       }   }
消费者package com.lingzhuo.Thread;public class ConsumerThread implements Runnable{    ShengchanXiaofei sf1;    public ConsumerThread(ShengchanXiaofei sf) {        // TODO Auto-generated constructor stub        this.sf1=sf;    }    @Override    public void run() {        // TODO Auto-generated method stub        while(true){            sf1.consumer();        }       }}
生产消费关系类package com.lingzhuo.Thread;public class ShengchanXiaofei {    boolean ishave=false;    public synchronized void product(){        if(!ishave){            System.out.println("开始生产");                         shuiMian();            System.out.println("生产完成");            notify();            shuiMian();            ishave=true;        }    }    public synchronized void consumer(){        if(!ishave){            System.out.println("没有产品,等待");            try {                wait();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            System.out.println("消费");            shuiMian();        }        System.out.println("消费完成");        ishave=false;        notify();        shuiMian();        shuiMian();    }    public static void shuiMian(){        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
测试类package com.lingzhuo.Thread;public class ShengchanXiaofeiTest {    public static void main(String[] args) {        ShengchanXiaofei sf=new ShengchanXiaofei();        Thread t1=new Thread(new ConsumerThread(sf));        Thread t2=new Thread(new ProductedThread(sf));              t1.start();        ShengchanXiaofei.shuiMian();        t2.start();    }}

运行结果:
这里写图片描述

0 0
原创粉丝点击