来京学习第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(); } }
相关文章推荐
- 无限互联的日子——连载一
- [BZOJ1607] [Usaco2008 Dec]Patting Heads 轻拍牛头
- 天才的大三暑假修行之旅.表面着色器(总结)
- .NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)
- 成员函数之重载、覆盖、隐藏
- KMP + 求最小循环节 --- HUST 1010 - The Minimum Length
- OC语言第四课 Foundation框架一——iOS学习连载九
- OC语言第五课 Foundation框架二——iOS学习连载十
- NY 2 括号配对问题 【栈】
- ASPX和Razor
- pagerank收敛原因
- 数据库收缩:NOTRUNCATE与TRUNCATEONLY
- UIPickerView与UIDatePicker
- Foundation框架----字符串、数组连载三
- hdoj 1526 A Plug for UNIX 【floyd求传递闭包 + 二分图匹配】
- Dos 常用命令整理
- Android - ViewHolder优化ListView
- Swift遍历数组的三种方式
- 程序员如何爱护眼睛
- Python good tutorials