正则表达式、JAVA线程
领卓教育培训第十四天 正则表达式定义指定为字符串的正则表达式必须首先被编译为此类的实例。然后,可将得到的模式用于创建 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("yadg12_dt@1yt.com.cn");//要进行验证的字符串
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元,则不能取出! 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();
}
}
运行结果:程序阻塞 Join()简介public final void join()throws 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();
}
}
运行结果: wait()和notify()简介这两个方法在java.lang.object类里边 代码示例创建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();
}
}
运行结果: 生产者和消费者问题生产者与消费者模型中,要保证以下几点: 代码示例创建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();
}
}
运行结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |