15/7/27/正则表达式/进程线程/wait/notify

正则表达式

进程和线程

正则表达式的简单应用

1.正则表达式比较简单主要是看Pattern的API

    public static void main(String[] args) {
//      Pattern.compile功能是 将给定的正则表达式编译到模式中
//       Pattern p = Pattern.compile("^\\w+@\\w+((.com)|(.cn)|(.net))$");//邮箱的正则表达式
//       创建匹配给定输入与此模式的匹配器
//       Matcher m = p.matcher("aww@qq.com");
//       编译给定正则表达式并尝试将给定输入与其匹配。如果匹配则true错误则false
//       boolean b = m.matches();
//       System.out.println(b);
//      验证手机号
//       Pattern p = Pattern.compile("^(13|15|17|18){1}\\d{9}$");
//       Matcher m = p.matcher("18300605531");
//       boolean b = m.matches();
//       System.out.println(b);
//      验证身份证
//       Pattern p = Pattern.compile("^\\d{17}((\\d)|[x])$");
//       Matcher m = p.matcher("41082319920928043x");
//       boolean b = m.matches();
//       System.out.println(b);
//      验证密码
//       Pattern p = Pattern.compile("^[a-zA-Z0-9]{8,16}$");//^\\p{Alnum}{8,16}$
//       Matcher m = p.matcher("12345678sdfa1235");
//       boolean b = m.matches();
//       System.out.println(b);
//      验证网址
         Pattern p = Pattern.compile("^((http://)|(https://)).+$");
         Matcher m = p.matcher("http://www.baidu.com");
         boolean b = m.matches();
         System.out.println(b);
}

多线程的定义

1.进程内部的一个执行单元,它的程序中一个单一的顺序控制流程。如果在一个进程中同时运行了多个线程,用来完成不同的工作,则称之为多线程
2.进程是系统资源分配的单位,可包括多个线程
3.线程是独立调度和分派的基本单位,共享进程资源。
4.引入进程是为了多个线程并发执行,提高资源的利用率和系统吞吐量。
5.线程要么继承Thread的线程,要么就实现Runable接口的线程。
启动线程一般是以start()来经行启动,运行完成以后就会自动停止
6.每个线程执行时都具有一定的优先级。当调度线程时,会优先考虑级别高的线程
7.默认情况下,一个线程继承其父类线程的优先级
8.使用《线程对象.setPriority(p)》来改变线程的优先级
9.优先级影响cpu在线程间切换,切换的原则是:(1)当一个线程通过显示放弃、睡眠或者阻塞、自愿放弃控制权时,所有线程均接受检查而优先级高级线程将会优先执行(2)一个线程可以被一个优先级高的线程抢占资源(3)同级别的线程之间,则通过控制权的释放,确保所有线程均有机会运行。

多线程的一般应用
        Thread方法
//      SellTickets st1=new SellTickets();
//      SellTickets st2=new SellTickets();
        启动线程
//      st1.start();//两个线程同时运行,如果两个线程对象相同,那么两个线程的结果是一样的
//      st2.start();

//      共享线程数据
        Tickets t1=new Tickets();
        Thread thread1=new Thread(t1);//共享t1这一个数据
        Thread thread2=new Thread(t1);
        Thread thread3=new Thread(t1);
        Thread thread4=new Thread(t1);
        Thread thread5=new Thread(t1);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
Jion 的使用
public static void main(String[] args) {
        Test03 test = new Test03();
        MyRunnable1 run1 = test.new MyRunnable1();//内部类的表示方法
        test.t1 = new Thread(run1);
        MyRunnable2 run2 = test.new MyRunnable2();
        test.t2 = new Thread(run2);
        test.t1.start();
    }

    class MyRunnable1 implements Runnable {
        @Override
        public void run() {
            System.out.println("运行了第一个线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("线程一休眠结束");
            try {
//      join的调用方法是先调用start然后调用join,之后执行完Start之后再执行接下来的程序
//      join的对象会占用线程直到运行结束才将线程释放给原来的任务
                System.out.println("线程二加入");
                t2.start();
                t2.join();
                System.out.println("线程二在线程一中结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程一运行结束");
        }
    }

    class MyRunnable2 implements Runnable {
        @Override
        public void run() {
            System.out.println("运行了第二个线程");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程二休眠结束");

        }
    }
synchronized的一般应用

自锁程序


    public static void main(String[] args) {
        String t1 = "abc";
        String t2 = "def";
        String t3 = "qwe";
//      Thread构造方法的一种Thread(Runnable target, String name) 
//      分配新的 Thread 对象,以便将 target 作为其运行对象,将指定的 name 作为其名称
        Thread ts1 = new Thread(new Sync(t1, t2), "ts1");
        Thread ts2 = new Thread(new Sync(t2, t3), "ts2");
        Thread ts3 = new Thread(new Sync(t3, t1), "ts3");
        ts1.start();
        work(1000);//模拟延时1s
        ts2.start();
        work(1000);
        ts3.start();
        work(1000);

    }

    private String s1;
    private String s2;
//方法构造器,将定义的s1,s2两个参数传到Sync里面
    public Sync(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);
            work(2000);
            System.out.println(name + "等待的锁" + s2);
            synchronized (s2) {
                System.out.println(name + "内部持有的锁" + s2);
                work(2000);
            }
            System.out.println(name + "需要的锁" + s2);
        }
        System.out.println(name + "需要的锁" + s1);
    }

    public static void work(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
wait 与notify

当遇到wait时系统会停止当前线程的操作从而释放锁,等待其他线程中的notify发出信号唤醒wait,然后继续执行当前的线程

public static void main(String[] args) {
//  这里的product是同一个,目的是方便synchronized进行上锁
        Product product=new Product();
        Consumer consumer=new Consumer(product);
        CreatProduct creatproduct=new CreatProduct(product);
        Thread t1=new Thread(consumer);
        Thread t2=new Thread(creatproduct);
        t1.start();
        t2.start();
    }

    private boolean isHave = false;

    public synchronized void creat() {
        if (isHave) {//如果有产品就等待
            try {
                System.out.println("等待消费者消费");
                System.out.println("生产者释放锁");
                wait();
                System.out.println("生产者被唤醒");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
//      否则开始生产产品
        System.out.println("开始生产");
        System.out.println("生产了一件产品");
        isHave = true;//生产完成,返回布尔值
        notify();//生产完成释放信号,通知消费者消费
        System.out.println("这是生产者最后方法");
    }

    public synchronized void sumer() {
        if (!isHave) {//没有产品则等待
            System.out.println("等待商家生产");
            try {
                System.out.println("消费者释放锁");
                wait();
                System.out.println("消费者被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //有产品就开始消费
        System.out.println("消费了产品");
        System.out.println("-----------");
        isHave=false;//消费完成后返回布尔值
        notify();//释放信号,让工厂开始生产
    }

    private Product product;
    public CreatProduct(Product product){
        this.product=product;
    }
//  由于这是个死循环所以creat和cumer两种方法如果在同一时间内开始运行的话就会出现抢锁的现象
//  为了防止出现抢锁现象的发生就将他们的运行时间分隔开让某一个线程始终先运行,来达到符合逻辑的目的
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        product.creat();
        }
    }

    private Product product;
    public Consumer(Product product){
        this.product=product;
    }
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        product.sumer();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值