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

© 狼王 高级黑马   /  2013-10-30 15:18  /  1201 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

十七。 线程
    17-1.线程的同步
    1.同步的概念: 当多个线程同时使用一个对象时,由于线程本身运行的不确定性,可能会造成操作的不完整性,故而引入同步
    2.Java中同步的方式有两种, Synchronized 和 Lock
    3.当一个线程进入一个对象的同步方法后,它会把该对象锁住,其它的线程不能再使用该对象(包括对象的任何方法,属性),直到该线程释放掉锁,其它线程才有机会使用该对象
    4.一个线程释放同步锁的条件:
    a. 正常运行完(退出synchronized块)
    b. 使用wait()方法
    5.同步中的方法: wait(), notify()/notifyAll(),用于同步中的线程通讯
    Wait(): 释放持有的同步锁,本身进入锁等待状态,在线程中因为多个线程"同时"运作,可能导致运作的条件不满足,当条件不满足时,线程本身就需要进入等待状态(释放掉锁),等其它的线程改变了条件,它才能有机会继续执行
    NotifyAll(): 唤醒锁等待的线程,当一个持有线程锁的对象调用该方法后,其它处于锁等待的线程虽然被唤醒,但其本身不会立刻释放掉锁,需要等运行结束后(退出synchronized块)才释放掉,其它线程才有机会执行
    17-2.线程中的方法
    6.sleep() ,当前线程休眠一个设定的时间,时间到后进入线程就绪队列,等待执行
    7.join(),该方法使调用该方法的线程在此之前执行完毕,也就是等待调用该方法的线程执行完毕后再往下继续执行。注意该方法也要捕获异常。
    实例:
  1. <p>class A implements runable {
  2.     int i;
  3.     public void run() {
  4.         try {
  5.                thread.sleep(5000);
  6.                i = 10;
  7.         } catch (InterruptedException e) { }
  8.     }
  9. }
  10. public class Test {
  11.       public static void main(string args[]) {
  12.           try {
  13.                 A a = new A();
  14.                Thread t = new
  15.                Thread(a);
  16.                t.start();
  17.                int j = a.i;
  18.          } catch (Exception e) {    }</p><p>     }</p><p>}</p>
复制代码
Which statement al line 17 will ensure that j=10 at line 19?
    A.a.wait();
    B.t.wait();
    C.t.join();
    D.t.yield();
    E.t.notify();
    F.a.notify();
    G.t.interrupt();
    8.yield(),与sleep()类似,只是不能由用户指定暂停多长时间,所以一个线程线程执行yield()方法后,也可能立刻执行(jvm还是分配给它执行),yield()方法只能让同优先级的线程有执行的机会。
    举几个例子:
    例1 线程同步
  1. <p>class SyncStack{ //同步堆栈类
  2.       private int index = 0; //堆栈指针初始值为0
  3.       private char []buffer = new char[6]; //堆栈有6个字符的空间
  4.       public synchronized void push(char c){ //加上互斥锁
  5.               while(index = = buffer.length){ //堆栈已满,不能压栈
  6.              try{
  7.                        this.wait(); //等待,直到有数据出栈
  8.              }catch(InterruptedException e){}   
  9.       }
  10.      this.notify(); //通知其它线程把数据出栈
  11.      buffer[index] = c; //数据入栈
  12.      index++;
  13.      //指针向上移动
  14. }
  15. public synchronized char pop(){ //加上互斥锁
  16.         while(index ==0){
  17.         //堆栈无数据,不能出栈
  18.         try{
  19.                this.wait();
  20.                //等待其它线程把数据入栈
  21.         }catch(InterruptedException e){}</p><p>    }
  22.     this.notify();
  23.     //通知其它线程入栈
  24.     index- -; //指针向下移动
  25.      return buffer[index]; //数据出栈
  26. }   
  27. }
  28. class Producer implements Runnable{ //生产者类
  29.        SyncStack theStack;   
  30.        //生产者类生成的字母都保存到同步堆栈中
  31.         public Producer(SyncStack s){
  32.                theStack = s;   
  33.         }
  34.         public void run(){
  35.               char c;
  36.               for(int i=0; i<20; i++){
  37.                      c =(char)(Math.random()*26+'A');  //随机产生20个字符
  38.                     theStack.push(c);
  39.              //把字符入栈
  40.              System.out.println("Produced: "+c); //打印字符
  41.              try{   
  42.                      Thread.sleep((int)(Math.random()*1000)); /*每产生一个字符线程就睡眠*/
  43.              }catch(InterruptedException e){}
  44.         }
  45.     }
  46.     }
  47. class Consumer implements Runnable{ //消费者类
  48.         SyncStack theStack;  //消费者类获得的字符都来自同步堆栈
  49.         public Consumer(SyncStack s){
  50.                  theStack = s;
  51.         }
  52.         public void run(){
  53.         char c;
  54.          for(int i=0;i<20;i++){
  55.                 c = theStack.pop();
  56.                //从堆栈中读取字符
  57.                 System.out.println("Consumed: "+c);  //打印字符
  58.                try{   
  59.                      Thread.sleep((int)(Math.random()*1000));
  60.                }catch(InterruptedException e){}

  61.               }
  62.        }
  63.     }
  64. public class SyncTest{
  65.       public static void main(String args[]){
  66.             SyncStack stack = new SyncStack();
  67.             //下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象
  68.             Runnable source=new Producer(stack);
  69.             Runnable sink = new Consumer(stack);
  70.             Thread t1 = new Thread(source); //线程实例化
  71.             Thread t2 = new Thread(sink); //线程实例化
  72.              t1.start();
  73.              t2.start(); //线程启动
  74.      }</p><p>}</p>
复制代码
  1. <p>  //例2.下面的代码在绝大部分时间内都运行得很正常,请问在什么情况下会出现问题?问题的根源在哪里?
  2.     </p><p>import java.util.LinkedList;
  3.     public class Stack {
  4.          LinkedList list = new LinkedList();
  5.         public synchronized void push(Object x) {
  6.               synchronized (list) {
  7.                     list.addLast(x);
  8.                     notify();
  9.                }
  10.         }
  11. public synchronized Object pop() throws Exception {
  12.         synchronized (list) {
  13.              if (list.size() <= 0) {
  14.                        wait();
  15.              }
  16.             return list.removeLast();
  17.         }
  18.     }</p><p> }
  19.     </p>
复制代码
  1. <p>对例2的分析【网友】:
  2.     当一个线程执行下面方法:</p><p>public synchronized void push(Object x) {
  3.     synchronized(list) {
  4.           list.addLast( x );
  5.           notify();
  6.      }</p><p>}</p>
复制代码
这个时候他获得2个锁,一个是Stack对象的锁,还有list对象的锁,而notify,释放的是stack对象的锁,没有释放list对象的锁,所以只要当pop方法中检测到list的大小为0,则执行pop的线程会一直控制list的锁,使得push没法执行。 之所以大部分时间程序运行成功,是因为push总比pop快,list没有为0.
    //例3.可能死锁的线程
  1. public class SyncTest {
  2.     public static void main(String[] args) {
  3.            final StringBuffer s1 = new StringBuffer();
  4.            final StringBuffer s2 = new StringBuffer();
  5.            new Thread() {
  6.                public void run() {
  7.                    synchronized (s1) {
  8.                         s2.append("A");
  9.                         synchronized (s2) {
  10.                                s2.append("B");
  11.                                System.out.print(s1);
  12.                                System.out.print(s2);
  13.                          }
  14.                         }
  15.                     }
  16.                }.start();
  17.               new Thread() {
  18.                public void run() {
  19.                        append("C");
  20.                        synchronized (s1) {
  21.                              s1.append("D");
  22.                              System.out.print(s2);
  23.                              System.out.print(s1);
  24.                        }
  25.                    }
  26.                }
  27.            }.start();
  28.         }
  29.     }
复制代码
对例3的分析【网友】
    如果没有出现死锁,那么输出的结果必然是:"ABABCD"; 如果没有输出此结果;那么死锁
    原因:T1启动在前, T2启动在后;且T1的第一步操作为:synchronized(s1)
    1. 由于T1执行过慢---> T2要执行第一步:synchronized(s2)--->寻找s1,被T1锁住等待--->T1寻找说说s2,被T2锁住等待 ; 出现死锁
    2. T1执行过快-->s1,s2都被锁住--->T2执行,等待-->T1执行完:"AB" -->T2执行:"ABCD"
   
您需要登录后才可以回帖 登录 | 加入黑马