线程和正则表达式
发布时间:2020-12-14 01:10:29 所属栏目:百科 来源:网络整理
导读:正则表达式 正则表达式通常用于判断语句中,用来检查某一字符串是否满足某一格式。 接下来用几个常用例子来说明正则表达式的具体用法。 package com.lingzhuo.Test; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ZhengZe
正则表达式
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();
}
}
线程的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();
}
}
运行结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |