Java多线程之线程间协作 notify与wait的使用

(转载请注明出处:http://blog.csdn.net/buptgshengod

1.背景

       Java多线程操作运用很广,特别是在android程序方面。线程异步协作是多线程操作的难点也是关键,也是找工作面试经常考到的地方。下面分享一下我的使用心得。

介绍几个关键字

synchronized:线程锁,使得系统只执行当前线程。

notifyAll():唤醒其它被锁住的线程

wait():挂起线程

ExecutorService exec=Executors.newCachedThreadPool();:创建线程池

exec.execute( new Runnable() ):将线程放到线程池中管理

2.代码部分

(1)

public class Thread2 {
     public void m4t1() {
          synchronized(this) {
               int i = 5;
               while(i>0) {
                    System.out.println("1");
                    i--;

                    try {
                         Thread.sleep(500);

                    } catch (InterruptedException ie) {
                    }
               }
          }
     }
     public void m4t2() {
        // synchronized(this){
         int i = 5;
          while( i > 0) {
               System.out.println("2");
               i--;
               try {
                    Thread.sleep(500);

               } catch (InterruptedException ie) {
               }
          }

     }
     public static void main(String[] args) {
          final Thread2 myt2 = new Thread2();
          Thread t1 = new Thread(  new Runnable() {  public void run() {  myt2.m4t1();  }  }, "t1"  );
          Thread t2 = new Thread(  new Runnable() {  public void run() { myt2.m4t2();   }  }, "t2"  );
          t1.start();
          t2.start();

     }
}

我们发现,两个线程其中一个上了锁,另一个没有上锁。运行结果如下。说明了虽然一个线程上了锁,但是还是能被让其它未上锁线程访问。

(2)

当我们给两个方法都加上锁

public class Thread2 {
     public void m4t1() {
          synchronized(this) {
               int i = 5;
               while(i>0) {
                    System.out.println("1");
                    i--;

                    try {
                         Thread.sleep(500);

                    } catch (InterruptedException ie) {
                    }
               }
          }
     }
     public void m4t2() {
        synchronized(this){
         int i = 5;
          while( i > 0) {
               System.out.println("2");
               i--;
               try {
                    Thread.sleep(500);

               } catch (InterruptedException ie) {
               }
          }
        }

     }
     public static void main(String[] args) {
          final Thread2 myt2 = new Thread2();
          Thread t1 = new Thread(  new Runnable() {  public void run() {  myt2.m4t1();  }  }, "t1"  );
          Thread t2 = new Thread(  new Runnable() {  public void run() { myt2.m4t2();   }  }, "t2"  );
          t1.start();
          t2.start();

     }
}
运行结果变成了

先执行完第一个锁中的内容后执行第二个锁中的内容。


(3)

这时候我们使用notifyAll()和wait()

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Thread2 {
     public void m4t1() {
          synchronized(this) {
               int i = 5;
               while(i>0) {
                    System.out.println("1");
                    i--;

                    try {
                         Thread.sleep(500);
                         notifyAll();
                         wait();
                    } catch (InterruptedException ie) {
                    }
               }
          }
     }
     public void m4t2() {
        synchronized(this){
         int i = 5;
          while( i > 0) {
               System.out.println("2");
               i--;
               try {
                    Thread.sleep(500);
                    notifyAll();
                    wait();
               } catch (InterruptedException ie) {
               }
          }
        }

     }
     public static void main(String[] args) {
          final Thread2 myt2 = new Thread2();
          Thread t1 = new Thread(  new Runnable() {  public void run() {  myt2.m4t1();  }  }, "t1"  );
          Thread t2 = new Thread(  new Runnable() {  public void run() { myt2.m4t2();   }  }, "t2"  );
          t1.start();
          t2.start();

     }
}

通过不断的唤醒再挂起,两个线程又交替运行。


(4)

如果想让代码更完善,可以将两个线程放到线程池

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Thread2 {
     public void m4t1() {
          synchronized(this) {
               int i = 5;
               while(i>0) {
                    System.out.println("1");
                    i--;

                    try {
                         Thread.sleep(500);
                         notifyAll();
                         wait();
                    } catch (InterruptedException ie) {
                    }
               }
          }
     }
     public void m4t2() {
        synchronized(this){
         int i = 5;
          while( i > 0) {
               System.out.println("2");
               i--;
               try {
                    Thread.sleep(500);
                    notifyAll();
                    wait();
               } catch (InterruptedException ie) {
               }
          }
        }

     }
     public static void main(String[] args) {
          final Thread2 myt2 = new Thread2();
         ExecutorService exec=Executors.newCachedThreadPool();
         exec.execute( new Runnable() {  public void run() {  myt2.m4t1();  }  });
         exec.execute( new Runnable() {  public void run() {  myt2.m4t2();  }  });
     }
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值