正则表达式
字符串开头加入^ 结尾加入$
1.判断一个字符串是否是邮箱
String rule = "\\w+@\\w+(\\.\\w{2,3})*+\\.\\w{2,3}";
Scanner scanner = new Scanner(System.in);
String item = scanner.nextLine();
if(item.matches(rule)){
System.out.println("符合标准");
}else{
System.out.println("不符合标准");
}
运行结果:
2.判断是否为手机号
Pattern p = Pattern.compile("^((13)|(15)|(17)|(18))+\\d{9}$");
Scanner scanner = new Scanner(System.in);
String item = scanner.next();
Matcher m = p.matcher(item);
System.out.println(m.matches());
运行结果:
3.判断是否为身份证号
Pattern p = Pattern.compile("^\\d{17}+(\\d{1}|(x))$");
Scanner scanner = new Scanner(System.in);
String item = scanner.nextLine();
Matcher m = p.matcher(item);
System.out.println(m.matches());
运行结果:
4.判断密码是否符合标准
Pattern p = Pattern.compile("^[\\w&(^[_])]{8,16}$");
Matcher m = p.matcher("123sad333ss");
System.out.println(m.matches());
运行结果:true
线程
创建线程
public class Thread0 extends Thread{
@Override
public void run() {
for (int i = 0; i <= 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread0.currentThread().toString()+i);
}
super.run();
}
}
Thread0 t0 = new Thread0();
Thread0 t1 = new Thread0();
t0.start();
t1.start();
运行结果:
线程间共享数据
实现runnable接口
public class ThreadGongXiang implements Runnable{
int i = 100;
String b = "d";
@Override
public void run() {
// TODO Auto-generated method stub
while(i>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (b) {
if(i>0){
System.out.println(Thread.currentThread().toString()+i);
i--;
}
}
}
}
}
ThreadGongXiang t = new ThreadGongXiang();
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();
运行结果:
Synchronized(lock){
线程锁,保证了在线程运行时只有一个线程运行,不会被别的线程干扰
}
public class QuQian implements Runnable {
int i = 500;
int k = i;
@Override
public void run() {
// TODO Auto-generated method stub
while(i/100>0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(""){
if(i/100>0){
System.out.println("取了第"+(k/100-i/100+1)+"个100元");
i = i-100;
}else{
System.out.println("余额不足");
}
}
}
}
}
public class QuQianTest {
public static void main(String[] args) {
QuQian q = new QuQian();
Thread t1 = new Thread(q);
Thread t2 = new Thread(q);
t1.start();
t2.start();
}
}
运行结果:
synchronized在方法中的用法:
public class SynTest0 implements Runnable{
int i = 50;
@Override
public void run() {
// TODO Auto-generated method stub
while(i>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i>0){
sell();
}
}
}
public synchronized void sell(){
System.out.println(Thread.currentThread().toString()+i);
i--;
}
}
public class SynTest1 {
public static void main(String[] args) {
SynTest0 s0 = new SynTest0();
Thread t1 = new Thread(s0);
Thread t2 = new Thread(s0);
Thread t3 = new Thread(s0);
t1.start();
t2.start();
t3.start();
}
}
运行结果:
join方法:
import ThreadTest.ThreadGongXiang;
public class Test {
private Thread n1;
private Thread n2;
public static void main(String[] args) {
Thread t = new Thread(new ThreadGongXiang());
t.start();
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("运行完成");
}
}
运行结果:
唤醒
wait()可以让线程挂起
挂起后可以用notify()方法唤醒wait()
注意:notify()唤醒wait()后必须等notify()运行完成后才能开始运行wait()
这是因为这些语句中用到了线程锁,抢锁的时候是随机的,可能后需要发生的事件抢先站到锁.
public class NotifyTest implements Runnable{
private String s = "abc";
NotifyTest(){}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("唤醒前");
try {
Thread.sleep(2000);
} 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();
System.out.println("结束唤醒");
}
}
}
public class WaitTxt implements Runnable {
private String s = "abc";
public WaitTxt(){}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("开始等待");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(s){
try {
s.wait();
System.out.println("结束等待");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class NotifyAndWait {
public static void main(String[] args) {
Thread thread1 = new Thread(new NotifyTest());
Thread thread2 = new Thread(new WaitTxt());
thread2.start();
thread1.start();
}
}
运行结果:
死锁
死锁的发生是因为线程通过线程锁需要相应的钥匙,没有相应的钥匙就不能进入线程锁,而某线程需要的钥匙正好在另一个线程中,而那个线程需要的钥匙在某线程中,这样就会发生死锁
public class DeadLockTest implements Runnable{
private String s1;
private String s2;
public DeadLockTest(String a1,String a2){
this.s1 = a1;
this.s2 = a2;
}
@Override
public void run() {
// TODO Auto-generated method stub
String name = Thread.currentThread().getName();
System.out.println(name+"进入锁"+s1);
synchronized (s1) {
System.out.println(name+"持有锁"+s1);
waitLock();
System.out.println(name+"需要锁"+s2);
synchronized (s2) {
System.out.println(name+"内部持有锁"+s2);
waitLock();
}
System.out.println(name+"释放锁"+s2);
}
System.out.println(name+"释放锁"+s1);
System.out.println("结束");
}
public void waitLock(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class SynTest {
public static void main(String[] args) throws InterruptedException {
String a1 = "abc";
String a2 = "def";
String a3 = "ghi";
DeadLockTest d0 = new DeadLockTest(a1,a2);
DeadLockTest d1 = new DeadLockTest(a2,a3);
DeadLockTest d2 = new DeadLockTest(a3,a1);
Thread t1 = new Thread(d0);
Thread t2 = new Thread(d1);
Thread t3 = new Thread(d2);
t1.start();
Thread.sleep(1000);
t2.start();
Thread.sleep(1000);
t3.start();
}
}
运行结果:
生产与消费
写一个程序用到多线程,要求有一个生产者一个消费者,生产者每生产一个商品,消费者就要去消费,如此循环.
public class Product {
public synchronized void creat(){
System.out.println("开始生产2");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("生产了一件商品3");
notify();
try {
wait();
System.out.println("等待生产6");
Thread.sleep(500);
System.out.println("随时可以生产7");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void cost(){
try {
System.out.println("等待消费1");
wait();
System.out.println("开始消费4");
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("消费完成5");
notify();
}
}
public class Consumer implements Runnable{
Product product = new Product();
Consumer(Product product){
this.product = product;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
product.cost();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Creater implements Runnable{
Product product = new Product();
Creater(Product product){
this.product = product;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
product.creat();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Product product = new Product();
Consumer c1 = new Consumer(product);
Creater c2 = new Creater(product);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
t1.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
}
}
运行结果: