JAVA学习第十天

时间:2023-02-25 11:29:31

正则表达式
synchronized同步代码块
死锁
WaitNotify
线程间共享数据 Synchrogazed方法
Thread.join()
synchronized同步的方法

正则表达式

///pattern 正则表达式
package com.lingzhuo.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegularExpressions_Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
// [^456] 代表4`5`6之外的任何字符 [a-r]代表a-r的任何一个字符 [a-e[g-z]]代表a-e,g-z的任何一个字母
// (并运算)
// [a-o&&[def]]代表def(交运算) [a-d&&[^bc]]代表 a`d(差运算)

// 验证邮箱 \w单词字符:[a-zA-Z_0-9] '+' 1次以上 '?'0次或1次 *大于等于0次 {n}正好n次 {n,}至少n次
// {n,m}出现n~m次
Pattern p3 = Pattern.compile("^\\w*@\\w+((.com)|(.cn)|(.net))");// Pattern.compile()编译
// 将给定的正则表达式编译到模式中。
Matcher m3 = p3.matcher("aa@so.com");// 通过调用模式的 matcher 方法从模式创建匹配器
boolean b3 = m3.matches();// matches 方法尝试将整个输入序列与该模式匹配。
System.out.println(b3);

// Pattern.compile(String regex) 将给定的正则表达式编译到模式中。
// pattern.matcher() 创建匹配给定输入与此模式的匹配器。
// mattern.matches() 尝试将整个区域与模式匹配。

// 验证手机号 13 15 17 18开头的手机号
Pattern p = Pattern.compile("^((13)|(15)|(17)|(18))\\d{9}$");
Matcher m = p.matcher("13041241002");
boolean b = m.matches();
System.out.println(b);
// 验证 18位 最后一位可能为X
Pattern p1 = Pattern.compile("^[1-9]{1}\\d{16}(\\d{1}|(X)|(x))$");
// [1-9]表示从1-9
Matcher m1 = p1.matcher("91130319921115451X");
boolean b1 = m1.matches();
System.out.println(b1);
// 验证密码8-16位的字母数字
// \p{Alnum}代表数字`字母
Pattern p2 = Pattern.compile("^\\p{Alnum}{8,16}$");
Matcher m2 = p2.matcher("nihao12356545447");
boolean b2 = m2.matches();
System.out.println(b2);

Pattern p4 = Pattern.compile("^((http://)|(https://)).+$");
// \\\\相当于\\ .\ 需要通过\转义
Matcher m4 = p4.matcher("http://www.baidu.com");
boolean b4 = m4.matches();
System.out.println(b4);

// 预定义字符类
// . 任何字符(与行结束符可能匹配也可能不匹配)
// \d 数字:[0-9]
// \D 非数字: [^0-9]
// \s 空白字符:[ \t\n\x0B\f\r]
// \S 非空白字符:[^\s]
// \w 单词字符:[a-zA-Z_0-9]
// \W 非单词字符:[^\w]

// POSIX 字符类(仅 US-ASCII)
// \p{Lower} 小写字母字符:[a-z]
// \p{Upper} 大写字母字符:[A-Z]
// \p{ASCII} 所有 ASCII:[\x00-\x7F]
// \p{Alpha} 字母字符:[\p{Lower}\p{Upper}]
// \p{Digit} 十进制数字:[0-9]
// \p{Alnum} 字母数字字符:[\p{Alpha}\p{Digit}]
// \p{Punct} 标点符号:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
// \p{Graph} 可见字符:[\p{Alnum}\p{Punct}]
// \p{Print} 可打印字符:[\p{Graph}\x20]
// \p{Blank} 空格或制表符:[ \t]
// \p{Cntrl} 控制字符:[\x00-\x1F\x7F]
// \p{XDigit} 十六进制数字:[0-9a-fA-F]
// \p{Space} 空白字符:[ \t\n\x0B\f\r]

// 边界匹配器
// ^ 行的开头
// $ 行的结尾
// \b 单词边界
// \B 非单词边界
// \A 输入的开头
// \G 上一个匹配的结尾
// \Z 输入的结尾,仅用于最后的结束符(如果有的话)
// \z 输入的结尾

// Greedy 数量词
// X? X,一次或一次也没有
// X* X,零次或多次
// X+ X,一次或多次
// X{n} X,恰好 n 次
// X{n,} X,至少 n 次
// X{n,m} X,至少 n 次,但是不超过 m 次

}

}

synchronized同步代码块

两个人同时从银行取钱

//synchronized同步代码块
package com.lingzhuo.test;

public class Money implements Runnable {
Integer i = 5025;
String s = "abc";

@Override
public void run() {
// TODO Auto-generated method stub

while (i >= 100) {
try {
Thread.sleep(100);// 休眠
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (s) {//s为锁 是个对象
if (i >= 100) {//if判断避免出现负数
i -= 100;
System.out.println(Thread.currentThread().toString() + "取走100" + "\t" + "卡內余额:" + i);
}

}

}
}

}
package com.lingzhuo.test;

public class TakeMoney_Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
Money m=new Money();
Thread thread1=new Thread(m);
Thread thread2=new Thread(m);
thread1.start();
thread2.start();
}

}

死锁

//死锁
package com.lingzhuo.test;

public class ThreadDeadLock implements Runnable {
private String s1;// 锁
private String s2;

public ThreadDeadLock(String o1, String o2) {// 构造两个锁
this.s1 = o1;
this.s2 = o2;
}

@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
System.out.println(name + "刚进run锁" + s1);
synchronized (s1) {
System.out.println(name + "现持有的锁" + s1);
work();//休眠2秒
System.out.println(name + "等待的锁" + s2);
synchronized (s2) {
System.out.println(name + "内部持有的锁" + s2);
work();
}
System.out.println(name + "释放锁" + s2);
System.out.println(name + "释放锁" + s1);
}
} // s1 start 持有的锁是s1 等待的锁是s2 s1休眠2秒
// s2 start 持有的锁是s2 等待的锁是s3 s2休眠2秒
// s3 start 持有的锁是s3 等待的锁是s1 s3休眠2秒
// 已锁死

private void work() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void main(String[] args) {
String s1 = "abc";
String s2 = "def";
String s3 = "fgh";
Thread t1 = new Thread(new ThreadDeadLock(s1, s2), "t1");
Thread t2 = new Thread(new ThreadDeadLock(s2, s3), "t2");
Thread t3 = new Thread(new ThreadDeadLock(s3, s1), "t3");
t1.start();
try {
t1.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
// try {
// t2.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
t3.start();
// try {
// t3.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}

}

WaitNotify

package com.lingzhuo.test;

public class WaitNotify_Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new Thread(new Tickets());
Thread t2=new Thread(new NotifyRunnable());
t1.start();
t2.start();


}

}
package com.lingzhuo.test;

public class Tickets implements Runnable {
Integer i = 100;
String s = "abc";

@Override
public void run() {
// TODO Auto-generated method stub
// while (i > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (s) {

try {
System.out.println("开始等待");
s.wait();//锁中的s被释放 其他线程可以使用
System.out.println("结束等待");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
System.out.println("程序运行完成");
// sell();

// }
}
private synchronized void sell(){
//同步方法 锁是Tickets的对象
if (i > 0) {
System.out.println(Thread.currentThread().toString()+"票的号码" + i);

}
i--;//放在外面

}

}
package com.lingzhuo.test;

public class NotifyRunnable implements Runnable {
String s="abc";
@Override
public void run() {

// TODO Auto-generated method stub
try {
Thread.sleep(2000);
System.out.println("唤醒前");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (s) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

s.notify();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("唤醒后");
// notify()直到当前线程放弃此对象上的锁定,才能继续执行被唤醒的线程 会先运行"唤醒后"再运行"结束等待"
}
}

}

线程间共享数据 Synchrogazed方法

package com.lingzhuo.test;

public class SellTicketTest {

public static void main(String[] args) {
// TODO Auto-generated method stub
//线程间共享数据
Tickets t=new Tickets();//
Thread thread1=new Thread(t);
Thread thread2=new Thread(t);
Thread thread3=new Thread(t);
Thread thread4=new Thread(t);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}

}
package com.lingzhuo.test;

public class SellTicket extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);// 1秒延迟
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().toString() + i);
// 返回当前调用线程的对象的String形式
}
}

}
package com.lingzhuo.test;

public class Tickets implements Runnable {
Integer i = 100;
String s = "abc";

@Override
public void run() {
// TODO Auto-generated method stub
while (i > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (s) {

// try {
// System.out.println("开始等待");
// s.wait();//锁中的s被释放 其他线程可以使用
// System.out.println("结束等待");
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
}
// System.out.println("程序运行完成");
sell();

}
}
private synchronized void sell(){
//同步方法 锁是Tickets的对象
if (i > 0) {
System.out.println(Thread.currentThread().toString()+"票的号码" + i);

}
i--;//放在外面

}

}

Thread.join()

//Thread.join()
package com.lingzhuo.test;

public class Thread_join_test {
public static void main(String[] args) {
Thread t = new Thread(new Tickets());
t.start();
try {
t.join();//Thread.join()等待该线程终止。
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("程序运行完成");
}

}
package com.lingzhuo.test;

public class Tickets implements Runnable {
Integer i = 100;
String s = "abc";

@Override
public void run() {
// TODO Auto-generated method stub
while (i > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// synchronized (s) {

// try {
// System.out.println("开始等待");
// s.wait();//锁中的s被释放 其他线程可以使用
// System.out.println("结束等待");
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
// System.out.println("程序运行完成");
sell();

}
}

private synchronized void sell() {
// 同步方法 锁是Tickets的对象
if (i > 0) {
System.out.println(Thread.currentThread().toString() + "票的号码" + i);

}
i--;// 放在外面

}

}

synchronized同步的方法

消费者消费商品与商家生产商品

package com.lingzhuo.test;

import javax.sql.rowset.spi.SyncFactory;
import javax.swing.plaf.synth.SynthCheckBoxMenuItemUI;

public class ProductFactory {
private boolean isHave = false;
//同步方法
public synchronized boolean creat() {
// if (isHave) {
// try {
//
// wait();
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
System.out.println("开始生产");
System.out.println("生产了一件产品");
isHave=true;
notify();
System.out.println("这是生产者的最后");
return isHave;


}
public synchronized boolean sumer(){

if(!isHave){
try {
System.out.println("等待商家生产");
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
System.out.println("商家生产完成");
System.out.println("开始消费");
System.out.println("消费完成");

isHave=false;
notify();
System.out.println("---------------");
return isHave;
}
//释放锁之后都是自己的wait()拿到了锁 两个if只会运行一个
}
package com.lingzhuo.test;

public class CreatThread implements Runnable {
private ProductFactory product;

public CreatThread(ProductFactory product) {
// TODO Auto-generated constructor stub
this.product = product;
}//重写构造器 为保证是同一个锁

@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
product.creat();
}
}

}

package com.lingzhuo.test;

public class ConsumerThread implements Runnable{
private ProductFactory product;
public ConsumerThread(ProductFactory product) {
// TODO Auto-generated constructor stub
this.product=product;
}//重写构造器 为保证是同一个锁

@Override
public void run() {
// TODO Auto-generated method stub
while(true){
product.sumer();//消费

}
}

}

package com.lingzhuo.test;

public class ConsumerCreatThread_Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
ProductFactory product=new ProductFactory();
Thread t2=new Thread(new CreatThread(product));//对象(锁)要一样
Thread t1=new Thread(new ConsumerThread(product));
t1.start();

t2.start();
}

}


JAVA学习第十天