您的位置:首页 > 其它

来京学习第15天(前2天是周末)

2015-07-28 08:07 288 查看

今天学习的主要内容

正则表达式

import java.util.Scanner;

import javax.swing.plaf.synth.SynthSpinnerUI;

public class TestZhengZeExpression {
public static void main(String[] args) {
String regex1="^((13)|(15)|(17)|(18))\\d{9}$";//手机号
String regex2="^\\w+@\\w+(\\.\\w+)+$";//邮箱
String regex3="^\\w{8,16}$";//密码
String regex4="^\\d{17}X|[0-9]$";//身份证号
String regex5="^((https://)|(ftp://)|(usesnet://)|(http://))\\w+(\\.\\w+)+$";//网址

Scanner scan=new Scanner(System.in);
String s=scan.next();
if(s.matches(regex2)){
if(s.endsWith(".com")||s.endsWith(".net")||s.endsWith(".cn")){
System.out.println(s+" is a valid Email!");
}
}else if(s.matches(regex1)){
System.out.println(s+"is a valid mobile phone number!");
}else if(s.matches(regex3)){
System.out.println(s+"is a valid password!");
}else if(s.matches(regex4)){
System.out.println(s+"is a valid shenfen id!");
}else if(s.matches(regex5)){
System.out.println(s+"is a valid url!");
}else
{
System.out.println(s+"is not a valid input!");
}

}

}

### 线程的创建
```java
public class Tick implements Runnable{
private int i=100;
public void run(){

while(i>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(""){
if(i>0){
System.out.println(Thread.currentThread().toString()+i);
i--;
}
}
}
}
}
public class TestThread1 {
public static void main(String[] args) {
/*  MyTestThread1 thread1=new MyTestThread1("线程1");
MyTestThread1 thread2=new MyTestThread1("线程2");
thread1.start();
thread2.start();*/
Tick t=new Tick();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}

<div class="se-preview-section-delimiter"></div>


线程的同步块 和 join练习

public class CashThread implements Runnable{
private int count;
public CashThread(int count){
this.count=count;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
cash();
if(count<100){
break;//结束线程
}
}
}
public synchronized void cash(){
if(count>=100){
count-=100;
System.out.print(Thread.currentThread().getName()+"取钱100成功");
System.out.println("账号余额为: "+count);
}else{
System.out.print(Thread.currentThread().getName()+"取钱不成功");
System.out.println("账号余额为:"+count);
}
}

}
public class TestCash {
public static void main(String[] args) {
CashThread c1=new CashThread(1000); //账户额 1000
Thread t1=new Thread(c1);
t1.start();
try {
t1.join();//必须等待t1完成主线程才能继续
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("程序运行完成");
/*  Thread t1=new Thread(c1);
Thread t2=new Thread(c1);
Thread t3=new Thread(c1);
Thread t4=new Thread(c1);
t1.start();
t2.start();
t3.start();
t4.start();*/
/*      Thread t1=new Thread(new DeadLockClass("aa","bb"));
t1.start();
Thread t2=new Thread(new DeadLockClass("bb","cc"));
t2.start();
Thread t3=new Thread(new DeadLockClass("cc","aa"));
t3.start();*/
}
}

<div class="se-preview-section-delimiter"></div>


线程死锁

public class DeadLockClass implements Runnable{
private String s1;
private String s2;
public DeadLockClass(String s1,String s2){
this.s1=s1;
this.s2=s2;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){

synchronized(s1){
System.out.print(Thread.currentThread().getName());
System.out.print("持有"+s1);
System.out.println("  等待"+s2);
work1();
synchronized(s2){
System.out.println("持有"+s2);
work2();
}
System.out.
cd07
println("释放"+s2);
}
System.out.println("释放"+s1);
}
}
public void work1(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void work2(){
System.out.println("成功进入");
}
}

main(){
/*      Thread t1=new Thread(new DeadLockClass("aa","bb"));
t1.start();
Thread t2=new Thread(new DeadLockClass("bb","cc"));
t2.start();
Thread t3=new Thread(new DeadLockClass("cc","aa"));
t3.start();*/
}

<div class="se-preview-section-delimiter"></div>


线程间通信

public class CustomerThread implements Runnable{
private Product product;
public CustomerThread(Product product){
this.product=product;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
product.cust();}
}

}
public class ProductThread implements Runnable{
private Product product;
public ProductThread(Product product){
this.product=product;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
product.prod();}
}

}
public class Product {
public Boolean have=false; //有产品
public synchronized void prod(){
if(have){
try {
wait();//等待消费
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("开始生产一个产品");
have=true;
notify();
}
}
public synchronized void cust(){
if(!have){
try {
System.out.println("等待生产");
wait();//等待生产
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("消费一个产品");
have=false;
notify();
}
}
}
//线程之间的通信,线程要共用一个对象,共同访问对象的方法,但是方法一定要synchronized
//竞争一把锁,即是那个公有的对象
public class TestThreadComm {
public static void main(String[] args) {
Product p=new Product();
ProductThread t1=new ProductThread(p);
CustomerThread t2=new CustomerThread(p);
Thread t3=new Thread(t2);
Thread t4=new Thread(t1);
t3.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t4.start();
}
}

<div class="se-preview-section-delimiter"></div>


线程的wait 和 Notify方法

注意:一般将notify()方法放在最后一句,因为唤醒其他线程,并不意味着让出持有权

public class NotifyClass implements Runnable{
String s="abc";

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

synchronized(s){
System.out.println("唤醒前");
s.notify();
System.out.println("唤醒了");
}
}

}
public class WaitClass implements Runnable{
String s="abc";

@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(s){
try {
System.out.println("等待中");
s.wait();
System.out.println("等待结束");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}
public class TestWait {
public static void main(String[] args) {
WaitClass r1=new WaitClass();
NotifyClass r2=new NotifyClass();
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
t1.start();
t2.start();
}
}


线程的wait 和 Notify方法
注意:一般将notify()方法放在最后一句,因为唤醒其他线程,并不意味着让出持有权
```java
public class NotifyClass implements Runnable{ String s="abc"; @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized(s){ System.out.println("唤醒前"); s.notify(); System.out.println("唤醒了"); } } } public class WaitClass implements Runnable{ String s="abc"; @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized(s){ try { System.out.println("等待中"); s.wait(); System.out.println("等待结束"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public class TestWait { public static void main(String[] args) { WaitClass r1=new WaitClass(); NotifyClass r2=new NotifyClass(); Thread t1=new Thread(r1); Thread t2=new Thread(r2); t1.start(); t2.start(); } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: