A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 小爷邱烙   /  2014-11-27 19:34  /  3324 人查看  /  79 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Testa implements Runnable{
  2.         private boolean flag;
  3.         Testa(boolean flag){
  4.                 this.flag = flag;
  5.         }
  6.         public void run(){
  7.                 if(flag){
  8.                         while(true){
  9.                                 synchronized (MyLock.locka) {
  10.                                         System.out.println("if locka");
  11.                                         synchronized (MyLock.lockb) {
  12.                                                 System.out.println("if lockb");
  13.                                         }
  14.                                 }
  15.                         }
  16.                 }else{
  17.                         while(true){
  18.                                 synchronized (MyLock.lockb) {
  19.                                         System.out.println("else lockb");
  20.                                         synchronized (MyLock.locka) {
  21.                                                 System.out.println("else locka");
  22.                                         }
  23.                                 }
  24.                         }
  25.                 }
  26.         }
  27. }
  28. class MyLock{
  29.         static Object locka = new Object();
  30.         static Object lockb = new Object();
  31. }
  32. class DeadLockTest{
  33.         public static void main(String[] args) {
  34.                 new Thread(new Testa(true)).start();
  35.                 new Thread(new Testa(false)).start();
  36.         }
  37. }
复制代码


回复 使用道具 举报
  1. class Res{
  2.         String name;
  3.         String sex;
  4. }
  5. class Input implements Runnable{
  6.         private Res r;
  7.         Input(Res r){
  8.                 this.r = r;
  9.         }
  10.         public void run(){
  11.                 int x = 0;
  12.                 while(true){
  13.                         synchronized (r) {//r对象唯一,也可以用class对象
  14.                                 if(x==0){
  15.                                         r.name = "mike";
  16.                                         r.sex = "man";
  17.                                 }else{
  18.                                         r.name = "丽丽";
  19.                                         r.sex = "女";
  20.                                 }
  21.                                 x = (x+1)%2;
  22.                         }
  23.                 }
  24.         }
  25. }
  26. class Output implements Runnable{
  27.         private Res r;
  28.         Output(Res r){
  29.                 this.r = r;
  30.         }
  31.         public void run(){
  32.                 while(true){
  33.                         synchronized (r) {
  34.                                 System.out.println(r.name+"..."+r.sex);
  35.                         }
  36.                 }
  37.         }
  38. }
  39. class InputOutputDemo{
  40.         public static void main(String[] args) {
  41.                 Res r = new Res();
  42.                 Input in = new Input(r);
  43.                 Output out = new Output(r);
  44.                 Thread t1 = new Thread(in);
  45.                 Thread t2 = new Thread(out);
  46.                 t1.start();
  47.                 t2.start();
  48.         }
  49. }
复制代码


回复 使用道具 举报
  1. class Resource{
  2.         private String name;
  3.         private int count = 1;
  4.         private boolean flag = false;
  5.         public synchronized void set(String name){//生产和消费线程有相同的锁,生产时不能消费,消费时不能生产
  6.                 if(flag){
  7.                         try {
  8.                                 this.wait();
  9.                         } catch (InterruptedException e) {
  10.                                 e.printStackTrace();
  11.                         }
  12.                 }
  13.                 this.name = name+"--"+count++;
  14.                 System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
  15.                 flag = true;
  16.                 this.notify();
  17.         }
  18.         public synchronized void get(){
  19.                 if(!flag){
  20.                         try {
  21.                                 this.wait();
  22.                         } catch (InterruptedException e) {
  23.                                 e.printStackTrace();
  24.                         }
  25.                 }
  26.                 System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
  27.                 flag = false;
  28.                 this.notify();
  29.         }
  30. }
  31. class Producer implements Runnable{//生产者
  32.         private Resource res;
  33.         Producer(Resource res){
  34.                 this.res = res;
  35.         }
  36.         public void run(){
  37.                 while(true){
  38.                         res.set("商品");
  39.                 }
  40.         }
  41. }
  42. class Consumer implements Runnable{
  43.         private Resource res;
  44.         Consumer(Resource res){
  45.                 this.res = res;
  46.         }
  47.         public void run(){
  48.                 while(true){
  49.                         res.get();
  50.                 }
  51.         }
  52. }
  53. class ProducerConsumerDemo{
  54.         public static void main(String[] args) {
  55.                 Resource res = new Resource();
  56.                 new Thread(new Producer(res)).start();
  57.                 new Thread(new Consumer(res)).start();
  58.         }
  59. }
复制代码


回复 使用道具 举报
  1. import java.util.concurrent.locks.*;
  2. class ProducerConsumerDemo2{
  3.         public static void main(String[] args) {
  4.                 Resource r = new Resource();
  5.                 Producer pro = new Producer(r);
  6.                 Consumer con = new Consumer(r);
  7.                 Thread t1 = new Thread(pro);
  8.                 Thread t2 = new Thread(pro);
  9.                 Thread t3 = new Thread(con);
  10.                 Thread t4 = new Thread(con);
  11.                 t1.start();
  12.                 t2.start();
  13.                 t3.start();
  14.                 t4.start();
  15.         }
  16. }
  17. class Resource{
  18.         private String name;
  19.         private int count = 1;
  20.         private boolean flag = false;
  21.         private Lock lock = new ReentrantLock();
  22.         private Condition condition_pro = lock.newCondition();
  23.         private Condition condition_con = lock.newCondition();
  24.         public void set(String name)throws InterruptedException{
  25.                 lock.lock();
  26.                 try{
  27.                         while(flag)
  28.                                 condition_pro.await();
  29.                         this.name = name+"--"+count++;
  30.                         System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
  31.                         flag = true;
  32.                         condition_con.signal();
  33.                 }finally{
  34.                         lock.unlock();
  35.                 }
  36.         }
  37.         public void get()throws InterruptedException{
  38.                 lock.lock();
  39.                 try{
  40.                         while(!flag)
  41.                                 condition_con.await();
  42.                         System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
  43.                         flag = false;
  44.                         condition_pro.signal();
  45.                 }finally{
  46.                         lock.unlock();
  47.                 }
  48.         }
  49. }
  50. class Producer implements Runnable{
  51.         private Resource res;
  52.         Producer(Resource res){
  53.                 this.res = res;
  54.         }
  55.         public void run(){
  56.                 while(true){
  57.                         try{
  58.                                 res.set("商品");
  59.                         }catch(InterruptedException e){
  60.                                 e.printStackTrace();
  61.                         }
  62.                 }
  63.         }
  64. }
  65. class Consumer implements Runnable{
  66.         private Resource res;
  67.         Consumer(Resource res){
  68.                 this.res = res;
  69.         }
  70.         public void run(){
  71.                 while(true){
  72.                         try{
  73.                                 res.get();
  74.                         }catch(InterruptedException e){
  75.                                 e.printStackTrace();
  76.                         }
  77.                 }
  78.         }
  79. }
复制代码


回复 使用道具 举报
  1. class StopThread implements Runnable{
  2.         private boolean flag = true;
  3.         public synchronized void run(){
  4.                 while(flag){
  5.                         try{
  6.                                 wait();
  7.                         }catch(InterruptedException e){
  8.                                 System.out.println(Thread.currentThread().getName()+"...Exception");
  9.                                 changeFlag();
  10.                         }
  11.                         System.out.println(Thread.currentThread().getName()+"...run");
  12.                 }
  13.         }
  14.         public void changeFlag(){
  15.                 flag = false;
  16.         }
  17. }
  18. class StopThreadDemo{
  19.         public static void main(String[] args) {
  20.                 StopThread st = new StopThread();
  21.                 Thread t1 = new Thread(st);
  22.                 Thread t2 = new Thread(st);
  23.                 t1.start();
  24.                 t2.start();
  25.                 int num = 0;
  26.                 while(true){
  27.                         if(num++==60){
  28.                                 t1.interrupt();
  29.                                 t2.interrupt();
  30.                                 break;
  31.                         }
  32.                         System.out.println(Thread.currentThread().getName()+"..."+num);
  33.                 }
  34.                 System.out.println("over");
  35.         }
  36. }
复制代码


回复 使用道具 举报
  1. new Thread(){
  2.         public void run(){
  3.                 for(int x=0;x<100;x++){
  4.                         System.out.println(Thread.currentThread().getName()+"..."+x);
  5.                 }
  6.         }
  7. }.start();
复制代码


回复 使用道具 举报
  1.         /*
  2.          *练习一:模拟一个trim方法,去除字符串两端的空格
  3.          *思路:1、判断第一个位置是否是空格,依次直到不是空格为止。判断最后一个是否空格,依次直到不是空格为止
  4.          *    2、开头和结尾都不是空格时,根据角标获取子串
  5.          */
  6.         public static String myTrim(String str){
  7.                 int start = 0,end = str.length()-1;
  8.                 while(start<=end && str.charAt(start)==' '){
  9.                         start++;
  10.                 }
  11.                 while(start<=end && str.charAt(end)==' '){
  12.                         end--;
  13.                 }
  14.                 return str.substring(start, end+1);
  15.         }
复制代码


回复 使用道具 举报
  1. /*
  2. *练习二:将字符串指定的部分反转
  3. *思路:将字符串变成数组,将数组反转,将数组变成字符串
  4. */
  5.         public static String reverseString(String s,int start,int end){
  6.                 char[] chs = s.toCharArray();
  7.                 reverse(chs,start,end);
  8.                 return new String(chs);
  9.         }
  10.         private static void reverse(char[] arr,int x,int y){
  11.                 for(int start=x,end=y-1;start<end;start++,end--){//y-1,包含头不包含尾,约定俗成
  12.                         swap(arr,start,end);                         //java定义成这样方便传length得到length-1
  13.                 }
  14.         }
  15.         private static void swap(char[] arr,int x,int y){
  16.                 char temp = arr[x];
  17.                 arr[x] = arr[y];
  18.                 arr[y] = temp;
  19.         }
  20.         public static String reverseString(String s){
  21.                 return reverseString(s,0,s.length());
  22.         }
复制代码


回复 使用道具 举报
  1. /*
  2. *练习三:获取一个字符串中另一个字符串出现的次数
  3. *思路:定义一个计数器,获取子串第一次出现的位置,从第一次出现位置后剩余的字符串中继续获取子串出现的位置,每获取一次,
  4. *    就计数一次,当获取不到时,计数完成
  5. */
  6.         public static int getSubCount(String str,String key){
  7.                 int count = 0;
  8.                 int index = 0;
  9.                 while((index=str.indexOf(key))!=-1){
  10.                         str = str.substring(index+key.length());
  11.                         count++;
  12.                 }
  13.                 return count;
  14.         }
复制代码


回复 使用道具 举报
  1. /*
  2. *方式二:方式一在内存中产生太多字符串
  3. */
  4.         public static int getSubCount(String str,String key){
  5.                 int count = 0;
  6.                 int index = 0;
  7.                 while((index=str.indexOf(key, index))!=-1){
  8.                         index = index + key.length();
  9.                         count++;
  10.                 }
  11.                 return count;
  12.         }
复制代码


回复 使用道具 举报
  1. /*
  2. *练习四:取两个字符串中最大相同子串
  3. *思路:获取两个字符串中较短的那个,获取一个字符串从长到短的子串,判断子串是否在长串中
  4. */
  5.         public static String getMaxSubString(String s1,String s2){
  6.                 String max = (s1.length()>s2.length())?s1:s2;
  7.                 String min = (max==s1)?s2:s1;
  8.                 for(int x=0;x<min.length();x++){
  9.                         for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++){
  10.                                 String temp = s2.substring(y,z);
  11.                                 if(max.contains(temp)){
  12.                                         return temp;
  13.                                 }
  14.                         }
  15.                 }
  16.                 return "";
  17.         }
复制代码


回复 使用道具 举报
  1.         Collection 接口 java.util包  JDK1.2出现
  2.                 |——List 接口
  3.                         |——ArrayList
  4.                         |——LinkedList
  5.                         |——Vector
  6.                 |——Set 接口
  7.                         |——HashSet
  8.                         |——TreeSet
  9.         每一个容器对数据的存储方式都不同,存储方式称为数据结构
复制代码


回复 使用道具 举报
  1.         boolean add(E e)//集合对象在堆内存中,和数组一样,存储的是元素在堆内存中的地址
  2.         boolean addAll(Collection c)//把指定集合中的元素添加到此集合中
  3.         void clear()//清空集合
  4.         boolean remove(Object o)//移除指定元素,只移除第一次出现的
  5.         boolean removeAll(Collection c)//移除交集
  6.         boolean contains(Object o)//判断是否包含该元素
  7.         boolean containsAll(Collection c)//必须包含所有元素才返回true
  8.         boolean equals(Object o)//集合与对象是否相同
  9.         boolean isEmpty()//集合是否为空
  10.         int size()//集合中元素数
  11.         boolean retainAll(Collection c)//取交集,仅保留两个集合中都有的元素,属于改
  12.         Object[] toArray()//返回所有元素的数组
  13.         <T> T[] toArray(T[] a)//按泛型返回数组,必须所有元素都是泛型指定类型
  14.         String toString()//Collection的toString方法重写了Object的toString方法,
  15.                                         //不再打印哈希值,而以一对[]中括号为边界,打印集合中元素
  16.         Collection的操作没有取出,需要一个方法
  17.          Iterator<T> iterator()//返回集合的迭代器,由迭代器做取出
复制代码


回复 使用道具 举报
  1.         Iterator<E> it = al.iterator();//返回一个Iterator的子类对象,至于是什么对象,已经被封装了,这里是多态
  2.         while(it.hasNext()){
  3.                 System.out.println(it.next());
  4.         }
复制代码


回复 使用道具 举报
  1. for(Iterator it=al.iterator();it.hasNext();){
  2.         System.out.println(it.next());
  3. }
复制代码


回复 使用道具 举报
  1. void add(int index,E e)
  2. boolean addAll(int index,Collection c)//在指定位置添加
  3. E get(int index)//获取
  4. int indexOf(Object o)
  5. int lastIndexOf(Object o)//判断位置
  6. ListIterator listIterator()//List特有迭代器
  7. ListIterator listIterator(int index)//从指定位置开始
  8. E remove(int index)
  9. E set(int index,E e)//替换
  10. List subList(int fromIndex,int toIndex)//包含头,不包含尾
复制代码


回复 使用道具 举报
  1. for(int x=0;x<al.size();x++){
  2.         System.out.println(al.get(x));
  3. }
复制代码


回复 使用道具 举报
  1. void add(E e)//在被迭代的最后一个元素后添加
  2. int nextIndex()//获取下一个元素的角标
  3. void set(E e)//修改被迭代的最后一个元素
  4. boolean hasPrevious()//逆向迭代,同next
  5. E previous()
  6. int previousIndex()
  7. 这些都是ListIterator特有方法,因为只有List有角标
复制代码


回复 使用道具 举报
  1. void addFirst(E e)
  2. void addLast(E e)
  3. E getFirst()
  4. E getLast()
  5. E removeFirst()
  6. E removeLast()
  7. get系列方法:获取元素
  8. remove系列方法:获取元素,并且删除元素
  9. 一种由remove方法实现的获取,LinkedList特有方法
  10. while(!link.isEmpty()){
  11.         System.out.println(list.removeFirst())
  12. }
  13. 当LinkedList中为空时,remove,get抛出NoSuchElementException
复制代码


回复 使用道具 举报
  1. E pollFirst()//相当于remove
  2. E pollLast()
  3. E peekFirst()//相当于get
  4. E peekLast()
  5. boolean offerFirst(E e)//相当于add
  6. boolean offerLast(E e)
  7. 这一系列方法当List为空时,返回null,不抛出异常
复制代码


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马