java线程同步(synchronized,wait,notify,notifyAll)

synchronized:

包括synchronized方法和synchronized块。

synchronized方法:

synchronized限制static方法的时候,
锁对象为这个类的class变量,相当于XXXClass.class.
synchronized限制非static方法的时候,锁对象为这个类的实例(相当于this).

而synchronized块:自己自行指定锁对象。

wait、notify、notifyAll必须在同步方法或块中,否则会抛出异常

Object.wait:

要执行obj.wait,当前线程必须拥有obj的监视器(monitor)

执行后会导致当前线程进入阻塞等待,直到其它线程调用这个obj的notify或notifyAll

注意只是使当前线程处于等待,但对同步方法/块的占有却释放了,所以其它线程便可以随后访问这个同步方法/块了

Object.notify,Object.notifyAll:

要执行obj.notify、obj.notifyAll,当前线程必须拥有obj的监视器(monitor)

obj.notify会随机唤醒一个等待在obj上的线程,而obj.notifyAll却会唤醒所有等待在obj上的线程

wait会释放占有的锁,notify和notifyAll不会释放占用的锁.如果notify方法后面的代码还有很多,需要这些代码执行完后才会释放锁

sleep:

sleep就是正在执行的线程主动让出cpucpu去执行其他线程,在sleep指定的时间过后,cpu才会回到这个线程上继续往下执行,如果当前线程进入了同步锁,sleep方法并不会释放锁,即使当前线程使用sleep方法让出了cpu,但其他被同步锁挡住了的线程也无法得到执行


实战一下吧:

public class ThreadA {
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();//主线程中启动另外一个线程
System.out.println("b is start....");
//括号里的b是什么意思,应该很好理解吧
synchronized(b) {
try {
System.out.println("Waiting for b to complete...");
b.wait();//synchronized(b)和下面的 synchronized(this)是同一个对象锁
System.out.println("ThreadB is Completed. Now back to main thread");
}catch (InterruptedException e){}
}
System.out.println("Total is :" + b.total);
}
}


class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {//主线程调用wait后这里才能进来(wait释放阻塞)
System.out.println("ThreadB is running..");
for (int i=0; i<=100; i++ ) {
total += i;
}
System.out.println("total is " + total);
notify();//通知主线程释放阻塞
}
}
}


运行结果: 
b is start.... 
Waiting for b to complete... 
ThreadB is running.. 
total is 5050 
ThreadB is Completed. Now back to main thread 
Total is :5050 


再看一个例子:

  1. public class MultiThread {  
  2.     /** 
  3.      * @param args 
  4.      */  
  5.     public static void main(String[] args) {  
  6.         new Thread(new Thread1()).start();  
  7.         try {  
  8.             Thread.sleep(10);  
  9.         } catch (InterruptedException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.         new Thread(new Thread2()).start();  
  13.     }  
  14.   
  15.     private static class Thread1 implements Runnable {  
  16.   
  17.         @Override  
  18.         public void run() {  
  19.             // 由于这里的Thread1和下面的Thread2内部run方法要用同一对象作为监视器,我们这里不能用this,因为在Thread2里面的this和这个Thread1的this不是同一个对象。我们用MultiThread.class这个字节码对象,当前虚拟机里引用这个变量时,指向的都是同一个对象。  
  20.             synchronized (MultiThread.class) {  
  21.   
  22.                 System.out.println("enter thread1...");  
  23.                 System.out.println("thread1 is waiting");  
  24.                 try {  
  25.                     // 释放锁有两种方式,第一种方式是程序自然离开监视器的范围,也就是离开了synchronized关键字管辖的代码范围,另一种方式就是在synchronized关键字管辖的代码内部调用监视器对象的wait方法。这里,使用wait方法释放锁。  
  26.                     MultiThread.class.wait();  
  27.                 } catch (InterruptedException e) {  
  28.                     e.printStackTrace();  
  29.                 }  
  30.   
  31.                 System.out.println("thread1 is going on...");  
  32.                 System.out.println("thread1 is being over!");  
  33.             }  
  34.         }  
  35.   
  36.     }  
  37.   
  38.     private static class Thread2 implements Runnable {  
  39.   
  40.         @Override  
  41.         public void run() {  
  42.             synchronized (MultiThread.class) {  
  43.   
  44.                 System.out.println("enter thread2...");  
  45.                 System.out.println("thread2 notify other thread can release wait status..");  
  46.                 // 由于notify方法并不释放锁,  
  47.                 // 即使thread2调用下面的sleep方法休息了10毫秒,但thread1仍然不会执行,因为thread2没有释放锁,所以Thread1无法得不到锁。  
  48.                 //notify并不释放锁,只是告诉调用过wait方法的线程可以去参与获得锁的竞争了,但不是马上得到锁,因为锁还在别人手里,别人还没释放。如果notify方法后面的代码还有很多,需要这些代码执行完后才会释放锁  
  49.                 MultiThread.class.notify();  
  50.                 System.out.println("thread2 is sleeping ten millisecond...");  
  51.                 try {  
  52.                     Thread.sleep(10);  
  53.                 } catch (InterruptedException e) {  
  54.                     e.printStackTrace();  
  55.                 }  
  56.                 System.out.println("thread2 is going on...");  
  57.                 System.out.println("thread2 is being over!");  
  58.             }  
  59.         }  
  60.   
  61.     }  
  62.     /** 
  63.      * 运行结果: 
  64.      *  enter thread1... 
  65.         thread1 is waiting 
  66.         enter thread2... 
  67.         thread2 notify other thread can release wait status.. 
  68.         thread2 is sleeping ten millisecond... 
  69.         thread2 is going on... 
  70.         thread2 is being over! 
  71.         thread1 is going on... 
  72.         thread1 is being over! 
  73.      */  
  74. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值