使用java.util.concurrent.locks代替synchronized

来源:互联网 发布:sql networkdays 编辑:程序博客网 时间:2024/06/10 17:17

好, 首先看原始代码

package com.day12;public class ManyThreadsCom {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubGoods g = new Goods();FactoryGet fg = new FactoryGet(g);FactorySet fs = new FactorySet(g,"大豆");Thread t1 = new Thread(fg);Thread t2 = new Thread(fg);Thread t3 = new Thread(fs);Thread t4 = new Thread(fs);t1.start();t2.start();t3.start();t4.start();}}class Goods{private String name;private boolean flag = false;private int count=0;public synchronized void set(String name){while (flag) {//这如果用if的话, 是达不到线程同步的目的的.try {this.wait();//因为可能多个线程在这一步wait, 然后被notify的时候是不会再次判断flag的.}catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}count++;this.name = name;System.out.println(Thread.currentThread().getName() + " 生产了货物 " + name + ", 第几次: " + count);flag = true;//this.notify();this.notifyAll();//不能notify(), 必须是notifyAll(), 因为此时flag是ture,t1一判断flag,wait了, //如果唤醒了本方线程t2, 那么t2一判断flag,也wait了. 然后程序就不会再继续执行了, 因为所有的线程都wait了.}public synchronized void get(){while (!flag) {//这如果用if的话, 是达不到线程同步的目的的.理由同上try {this.wait();}catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(Thread.currentThread().getName() + " 购买了货物---- " + name + ", 第几次: " + count);flag=false;//this.notify();this.notifyAll();}}class FactoryGet implements Runnable{private Goods good = null;public FactoryGet(Goods good) {super();this.good = good;}@Overridepublic void run() {while (true) {// TODO Auto-generated method stubgood.get();}}}class FactorySet implements Runnable{private Goods good = null;private String name = null;public FactorySet(Goods good, String name) {super();this.good = good;this.name = name;}@Overridepublic void run() {while (true) {// TODO Auto-generated method stubgood.set(name);}}}


再来看使用java.util.coucurrent.locks实现的多线程同步锁.

package com.day12;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class LocksDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubGoods2 g = new Goods2();FactoryGet2 fg = new FactoryGet2(g);FactorySet2 fs = new FactorySet2(g,"大豆");Thread t1 = new Thread(fg);Thread t2 = new Thread(fg);Thread t3 = new Thread(fs);Thread t4 = new Thread(fs);t1.start();t2.start();t3.start();t4.start();}}class Goods2{private String name;private boolean flag = false;private int count=0;final private Lock lock = new ReentrantLock();final private Condition con1 = lock.newCondition();final private Condition con2 = lock.newCondition();public void set(String name){lock.lock();try {while (flag) { try {con1.await();}catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}count++;this.name = name;System.out.println(Thread.currentThread().getName() + " 生产了货物 "+ name + ", 第几次: " + count);flag = true;con2.signal();}finally {// TODO: final to dolock.unlock();}}public void get(){lock.lock();try {while (!flag) {try {con2.await();}catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.println(Thread.currentThread().getName() + " 购买了货物---- "+ name + ", 第几次: " + count);flag = false;con1.signal();}finally {// TODO: final to dolock.unlock();}}}class FactoryGet2 implements Runnable{private Goods2 good = null;public FactoryGet2(Goods2 good) {super();this.good = good;}@Overridepublic void run() {while (true) {// TODO Auto-generated method stubgood.get();}}}class FactorySet2 implements Runnable{private Goods2 good = null;private String name = null;public FactorySet2(Goods2 good, String name) {super();this.good = good;this.name = name;}@Overridepublic void run() {while (true) {// TODO Auto-generated method stubgood.set(name);}}}


原创粉丝点击