本帖最后由 赵庆礼 于 2012-7-5 11:44 编辑
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;
public class ThreadTest {
/** * @param args */ private int j; private Lock lock = new ReentrantLock(); public static void main(String[] args) { // TODO Auto-generated method stub ThreadTest tt = new ThreadTest(); for(int i=0;i<2;i++) { new Thread(tt.new Adder()).start(); new Thread(tt.new Subtractor()).start(); } }
private class Subtractor implements Runnable {
@Override public void run() { // TODO Auto-generated method stub while(true) { /*synchronized (ThreadTest.this) { System.out.println("j--=" + j--); //这里抛异常了,锁能释放吗? }*/ lock.lock(); try { System.out.println("j--=" + j--); }finally { lock.unlock(); } } } } private class Adder implements Runnable {
@Override public void run() { // TODO Auto-generated method stub while(true) { /*synchronized (ThreadTest.this) { System.out.println("j++=" + j++); }*/ lock.lock(); try { System.out.println("j++=" + j++); }finally { lock.unlock(); } } } } }
看了毕老师的视频,Lock锁比synchronized 锁更方便准确,但是对于Lock替换synchronized 还是有点不解,请哪位帮我仔细讲解一下,谢谢 |