synchronized:解决死锁的问题[轉貼]
pv 操作研究了一下,才发现原来 java
多线程的互斥与同步
临界资源问题
前面所提到的线程都是独立的,而且异步执行,也就是说每个线程都包含了运行时所需要的数据或方法,而不需要外部的资源或方法,也不必关心其它线程的状态或行为。但是经常有一些同时运行的线程需要共享数据,此时就需考虑其他线程的状态和行为,否则就不能保证程序的运行结果的正确性。例说明了此问题。
例
class stack{
int idx=0; //
堆栈指针的初始值为
0
char[ ] data = new char[6]; //
堆栈有
6
public void push(char c){ //
data[idx] = c; //
idx + +; //
}
public char pop(){ //
idx - -; //
return data[idx]; //
}
}
A 和 B 在同时使用 Stack 的同一个实例对象, A 正在往堆栈里 push 一个数据, B 则要从堆栈中 pop 一个数据。如果由于线程 A 和 B 在对 Stack
1)
data = | p | q | | | | | idx=2
2) A 执行 push 中的第一个语句,将 r
data = | p | q | r | | | | idx=2
3) A 还未执行 idx++ 语句, A 的执行被 B 中断, B 执行 pop 方法,返回 q
data = | p | q | r | | | | idx=1
4 〕 A 继续执行 push
data = | p | q | r | | , | | idx=2
r
互斥锁
Java 语言中,引入了对象互斥锁的概念,来保证共享数据操作的完整性。每个对象都对应于一个可称为 " 互斥锁 " 的标记,这个标记用来保证在任一时刻,只能有一个线程访问该对象。 关键字 synchronized 来与对象的互斥锁联系。当某个对象用 synchronized
public void push(char c){
synchronized(this){ //this
表示
Stack
data[idx]=c;
idx++;
}
}
public char pop(){
synchronized(this){ //this
表示
Stack
idx--;
return data[idx];
}
}
synchronized
public synchronized void push(char c){
…
}
synchronized 用在类声明中,则表明该类中的所有方法都是 synchronized
多线程的同步
, 即多线程之间的同步问题 , 下面我们将通过多线程同步的模型 : 生产者 -
我们把系统中使用某类资源的线程称为消费者,产生或释放同类资源的线程称为生产者。
Java
例
class SyncStack{ //
private int index = 0; //
堆栈指针初始值为
0
private char []buffer = new char[6]; //
堆栈有
6
public synchronized void push(char c){ //
while(index = = buffer.length){ //
try{
this.wait(); //
}catch(InterruptedException e){}
}
this.notify(); //
buffer[index] = c; //
index++; //
}
public synchronized char pop(){ //
while(index ==0){ //
try{
this.wait(); //
}catch(InterruptedException e){}
}
this.notify(); //
index- -; //
return buffer[index]; //
}
}
class Producer implements Runnable{ //
SyncStack theStack;
//
public Producer(SyncStack s){
theStack = s;
}
public void run(){
char c;
for(int i=0; i<20; i++){
c =(char)(Math.random()*26+'A');
//
随机产生
20
theStack.push(c); //
System.out.println("Produced: "+c); //
try{
Thread.sleep((int)(Math.random()*1000));
/*
每产生一个字符线程就睡眠
*/
}catch(InterruptedException e){}
}
}
}
class Consumer implements Runnable{ //
SyncStack theStack;
//
public Consumer(SyncStack s){
theStack = s;
}
public void run(){
char c;
for(int i=0;i<20;i++){
c = theStack.pop(); //
System.out.println("Consumed: "+c);
//
try{
Thread.sleep((int)(Math.random()*1000));
/*
每读取一个字符线程就睡眠
*/
}catch(InterruptedException e){}
}
}
}
public class SyncTest{
public static void main(String args[]){
SyncStack stack = new SyncStack();
//
Runnable source=new Producer(stack);
Runnable sink = new Consumer(stack);
Thread t1 = new Thread(source); //
Thread t2 = new Thread(sink); //
t1.start(); //
t2.start(); //
}
}
Producer 是生产者模型,其中的 run() 方法中定义了生产者线程所做的操作,循环调用 push() 方法 , 将生产的 20 个字母送入堆栈中,每次执行完 push 操作后,调用 sleep() 方法睡眠一段随机时间,以给其他线程执行的机会。类 Consumer 是消费者模型,循环调用 pop() 方法 , 从堆栈中取出一个数据,一共取 20 次,每次执行完 pop 操作后,调用 sleep()
程序执行结果
Produced:V
Consumed:V
Produced:E
Consumed:E
Produced:P
Produced:L
...
Consumed:L
Consumed:P
wait() 和 notify() 方法来实现线程的同步,在同步中还会用到 notifyAll() 方法,一般来说,每个共享对象的互斥锁存在两个队列,一个是锁等待队列,另一个是锁申请队列,锁申请队列中的第一个线程可以对该共享对象进行操作,而锁等待队列中的线程在某些情况下将移入到锁申请队列。下面比较一下 wait() 、 notify() 和 notifyAll()
(1) wait,nofity,notifyAll 必须在已经持有锁的情况下执行 , 所以它们只能出现在 synchronized 作用的范围内,也就是出现在用 synchronized
(2) wait 的作用 : 释放已持有的锁 , 进入等待队列 .
(3) notify 的作用 : 唤醒 wait 队列中的第一个线程并把它移入锁申请队列 .
(4) notifyAll 的作用 : 唤醒 wait 队列中的所有的线程并把它们移入锁申请队列 .
注意 :
1 ) suspend() 和 resume()
JDK1.2 中不再使用 suspend() 和 resume(), 其相应功能由 wait() 和 notify()
2) stop()
JDK1.2 中不再使用 stop(), 而是通过标志位来使程序正常执行完毕。例 6.6
例
public class Xyz implements Runnable {
private boolean timeToQuit=false; //
public void run() {
while(!timeToQuit) {//
…
}
}
public void stopRunning() {
timeToQuit=true;} //
}
public class ControlThread {
private Runnable r=new Xyz();
private Thread t=new Thread(r);
public void startThread() {
t.start();
}
public void stopThread() {
r.stopRunning(); }
//
通过调用
stopRunning
}
















