第三周第一天(【正则表达式的应用】【卖票问题】【同一个账号两人同时取钱】【生产消费问题】【join】【死锁问题】)
来源:互联网 发布:cp什么意思网络用语 编辑:程序博客网 时间:2024/05/16 17:12
正则表达式的应用
//package com.day1_2015_7_27;//import java.util.Scanner;//public class Test1 {// public static void main(String[] args) {// // 定义要匹配E_mail地址的正则表达式// Scanner scanner = new Scanner(System.in);// String s= scanner.next();// String regex = "^\\w+@\\w+((.com)|(.cn)|(.net))$";// if (s.matches(regex)) {// System.out.println(s + "是一个合法的E-mail地址格式");// }else{// System.out.println(s+"不是一个合法的E-mail地址格式");// }//}//} // 身份证号18位//package com.day1_2015_7_27;//import java.util.Scanner;//public class Test1 {// public static void main(String[] args) {// Scanner scanner = new Scanner(System.in);// String s= scanner.next();// String regex = "^[1-9]{1}\\d{16}(([0-9]{1})|(X))$";// if (s.matches(regex)) {// System.out.println(s + "是一个合法的身份证格式");// }else{// System.out.println(s+"不是一个合法的身份证格式");// }//}//} //验证网址//package com.day1_2015_7_27;//import java.util.Scanner;//public class Test1 {//// public static void main(String[] args) {// Scanner scanner = new Scanner(System.in);// String s= scanner.next();// String regex = "^(http://|https://).+$";// if (s.matches(regex)) {// System.out.println(s + "是一个合法的网址格式");// }else{// System.out.println(s+"不是一个合法的网址格式");// }//}//} // 定义要匹配密码的正则表达式//package com.day1_2015_7_27;//import java.util.Scanner;//public class Test1 {// public static void main(String[] args) {// Scanner scanner = new Scanner(System.in);// String s= scanner.next();// String regex ="^[0-9a-zA-Z]{8,16}$" ;// if (s.matches(regex)) {// System.out.println(s+"是一个合法的密码格式");// }else{// System.out.println(s+"不是一个合法的密码格式");// }// }//}// // 定义要匹配手机号的正则表达式//package com.day1_2015_7_27;//import java.util.Scanner;//public class Test1 {// public static void main(String[] args) {// Scanner scanner = new Scanner(System.in);// String s= scanner.next();// String regex = "^((13)|(15)|(18)|(17))\\d{9}$";// if (s.matches(regex)) {// System.out.println(s + "是一个合法的手机号");// }else{// System.out.println(s +"这不是一个合法的手机号");// }// }//}
卖票问题
//***************************卖票***********************///package com.day1_2015_7_27;//public class SellTick extends Thread {// public void run(){// for (int i = 1; i <1001; i++) {// try {// Thread.sleep(1000);// } catch (InterruptedException e) {// e.printStackTrace();// }// System.out.println(Thread.currentThread().toString()+i);// }// }//}//*****************************************************///package com.day1_2015_7_27;//public class Test2 {// public static void main(String[] args) {// SellTick s1 = new SellTick();// SellTick s2 = new SellTick();// s1.start();// s2.start();// }//}/*******************卖票*********************************/package com.day1_2015_7_27;public class SellTick implements Runnable { int i=100; public void run() { while(i>0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sell(); i--; } }private synchronized void sell(){ //同步方法 他的锁是SellTick的对象 if(i>0){ System.out.println(Thread.currentThread()+"买的票号码"+i); }}}/**************************************************/package com.day1_2015_7_27;public class Test2 { public static void main(String[] args) { SellTick t1 = new SellTick(); Thread thread1 = new Thread(t1); Thread thread2 = new Thread(t1); Thread thread3 = new Thread(t1); Thread thread4 = new Thread(t1); thread1.start(); thread2.start(); thread3.start(); thread4.start(); }}
同一个账号两人同时取钱
/**********************同一个账号两人同时取钱*******************************///package com.day1_2015_7_27;//public class SellTick implements Runnable {// public int s=1000;// String m="abc";// public void run() {// for (int i = 1; i <8; i++) {// synchronized(m){// try {// Thread.sleep(10);// } catch (InterruptedException e) {// e.printStackTrace();// }// if(s>=100){// s-=100;// System.out.println(Thread.currentThread().toString()+"取出100");// }else{// System.out.println(Thread.currentThread().toString()+"余额不足");// }// }// }// }//}//*********************************************************************/package com.day1_2015_7_27;public class Test2 { public static void main(String[] args) { SellTick t1 = new SellTick(); Thread thread1 = new Thread(t1); Thread thread2 = new Thread(t1); thread1.start(); thread2.start(); }}
死锁问题
/********************死锁问题**************/package com.day1_2015_7_27;public class Sisuo implements Runnable{private String s1;private String s2;public Sisuo(String o1,String o2){ this.s1=o1; this.s2=o2; }public void run(){ String name = Thread.currentThread().getName(); System.out.println(name+"刚进run锁"+s1); synchronized(s1){ System.out.println(name+"现在持有的锁"+s1); work(); System.out.println(name+"等待的锁"+s2); synchronized (s2) { System.out.println(name+"内部持有的锁"+s2); work(); } System.out.println(name+"released lock on"+s2); } System.out.println(name+"released lock on"+s1); System.out.println(name+"finished execution.");}private void work(){ try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}///**************死锁问题*******************************************///package com.day1_2015_7_27;//public class Test4 {// public static void main(String[] args) {// Thread thread1 = new Thread(new Sisuo("aa","bb"));// Thread thread2 = new Thread(new Sisuo("bb","cc"));// Thread thread3 = new Thread(new Sisuo("cc","aa"));// thread1.start();// thread2.start();// thread3.start();// }//}
#
package com.day1_2015_7_27;public class NotifyRunnable implements Runnable{ private String s="abc"; public void run(){ 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 e1) { // TODO Auto-generated catch block e1.printStackTrace(); } s.notify(); } System.out.println("唤醒后"); } }//**************************************************************//package com.day1_2015_7_27;public class Test5 implements Runnable{ private String s="abc"; public void run(){ 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(); } } }}//************************************************************************/package com.day1_2015_7_27;public class Test6 {public static void main(String[] args) { NotifyRunnable t1= new NotifyRunnable(); Test5 t2 = new Test5(); Thread r1= new Thread(t1); Thread r2= new Thread(t2); r1.start(); r2.start();}}
join
/************join**********************/package com.day1_2015_7_27;public class Test4 { public static void main(String[] args) { SellTick t1 = new SellTick(); Thread thread1 = new Thread(t1); thread1.start(); try { thread1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("程序运行完成了"); }}
生产消费问题
//*******************生产消费问题************************//package com.day1_2015_7_27;public class ConsumerThread implements Runnable{ private Product product; public ConsumerThread(Product product){ this.product=product; } public void run(){ while(true){ try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } product.sumer();}}}//*******************生产消费问题************************//package com.day1_2015_7_27;public class CreatThread implements Runnable{ private Product product; public CreatThread(Product product){ this.product=product; }public void run(){ while(true){ try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } product.creat();}}}//*******************生产消费问题************************//package com.day1_2015_7_27;public class Product {private boolean isHave = false;public synchronized void creat(){ if(isHave){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("开始生产"); System.out.println("生产完成"); isHave= true; notify(); }public synchronized void sumer(){ System.out.println("消费前"); if(!isHave){ try { System.out.println("等待生产"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("消费完成"); isHave=false; notify();}}package com.day1_2015_7_27;public class Test7 { public static void main(String[] args) { Product product = new Product(); ConsumerThread consumerRun = new ConsumerThread(product); CreatThread creatRun = new CreatThread(product); Thread t1= new Thread(consumerRun); Thread t2= new Thread(creatRun); t2.start(); t1.start(); }}
0 0
- 第三周第一天(【正则表达式的应用】【卖票问题】【同一个账号两人同时取钱】【生产消费问题】【join】【死锁问题】)
- 生产消费中的死锁问题
- 多线程生产消费问题
- 线程---生产消费问题
- 生产消费问题
- java生产消费问题
- 多线程经典问题:馒头问题 生产和消费问题(线程的通信问题)
- 线程 --生产和消费问题
- Java模拟生产消费问题
- 生产消费问题整合版
- 多线程之生产消费问题
- Java多线程的单生产单消费和多生产多消费问题的解决
- 单生产单消费多生产多消费问题的解决及使用机制
- 多线程卖票,死锁问题和生产者消费者问题!
- 经典的生产与消费线程同步问题
- 生产与消费的wait和notify位置问题
- Java并发(八)生产消费问题&虚假唤醒
- 条件变量实现生产消费问题
- Android基础_页面布局_TableLayout(表格布局)
- UVA 442 Matrix Chain Multiplication
- 安卓手机来电亮屏流程分析
- JavaScript-2015.7.27
- HDU 2846 Repository (Trie·统计子串)
- 第三周第一天(【正则表达式的应用】【卖票问题】【同一个账号两人同时取钱】【生产消费问题】【join】【死锁问题】)
- 如此理解面向对象编程
- C++使用libcurl做HttpClient
- UML建模 步骤
- SAP项目实施阶段
- [BZOJ1664] [Usaco2006 Open]County Fair Events 参加节日庆祝
- 转载极客头条的ios面试题
- 【算法学习】【图像增强】【Retinex】White Patch Retinex 程序解读
- 程序员的思维修炼