//懒汉式 class Single{ private static Single s = null; private Single(){} public static Single getInstance(){ if(s==null){ synchronized (Single.class){ if (s==null){ return new Single(); } } } return s; } }
synchronized模式实现:在多消费者多生产者模式的情况下,只能使用notifyAll()的方式,使用notify()的方式有可能死锁。
package com.bixiangdong.thread; class Resource{ private String name; private int count=1; private boolean flag=false; public synchronized void set(String name){ while (flag){ try { this.wait(); }catch (InterruptedException e){ } } this.name=name; count++; System.out.println(Thread.currentThread().getName()+"----生产者----"+this.name+count); flag=true; notifyAll();//这里使用notifyAll是因为如果 生产者有多个的话,notify唤醒的可能总是生产者的线程,然后因为flag=flase就总是等待,可能会发生死锁的情况 } public synchronized void get(){ while (!flag){ try { this.wait(); }catch (InterruptedException e){ } } System.out.println(Thread.currentThread().getName()+"----消费者----"+this.name+count); flag=false; notifyAll(); } } class Productor implements Runnable{ private Resource r; public Productor(Resource r){ this.r=r; } @Override public void run() { while (true){ r.set("牛奶"); } } } class Customer implements Runnable{ private Resource r; public Customer(Resource r){ this.r=r; } @Override public void run() { while (true){ r.get(); } } } public class Demo03 { public static void main(String[] args) { Resource resource = new Resource(); Productor productor = new Productor(resource); Customer customer = new Customer(resource); new Thread(productor).start(); new Thread(customer).start(); } }
lock实现方式:JDK1.5支持了lock方式,可以实现一个lock有多个condition,可以实现生产者通知消费者,更加精准,生产者只用唤醒一个消费者就行,消费者也是如此
package com.bixiangdong.thread; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class Resource2{ private String name; private int count=1; private boolean flag=false; //创建锁 Lock lock = new ReentrantLock(); //通过已有的锁创建两组监视器 Condition pro_con = lock.newCondition(); Condition cus_con = lock.newCondition(); public void set(String name){ //设置锁 lock.lock(); while (flag){ try { pro_con.await(); }catch (InterruptedException e){ } } this.name=name; count++; System.out.println(Thread.currentThread().getName()+"----生产者----"+this.name+count); flag=true; //notifyAll();//这里使用notifyAll是因为如果 生产者有多个的话,notify唤醒的可能总是生产者的线程,然后因为flag=flase就总是等待,可能会发生死锁的情况 cus_con.signal();//只需要唤醒一个消费者就ok //释放锁 lock.unlock(); } public void get(){ lock.lock(); while (!flag){ try { cus_con.await(); }catch (InterruptedException e){ } } System.out.println(Thread.currentThread().getName()+"----消费者----"+this.name+count); flag=false; //notifyAll(); pro_con.signal(); lock.unlock(); } } class Productor2 implements Runnable{ private Resource2 r; public Productor2(Resource2 r){ this.r=r; } @Override public void run() { while (true){ r.set("牛奶"); } } } class Customer2 implements Runnable{ private Resource2 r; public Customer2(Resource2 r){ this.r=r; } @Override public void run() { while (true){ r.get(); } } } public class Demo04 { public static void main(String[] args) { Resource2 resource = new Resource2(); Productor2 productor = new Productor2(resource); Customer2 customer = new Customer2(resource); new Thread(productor).start(); new Thread(productor).start(); new Thread(productor).start(); new Thread(customer).start(); new Thread(customer).start(); new Thread(customer).start(); } }
package com.bixiangdong.thread; class A1 implements Runnable{ public boolean flag = true; @Override public void run() { while (flag){ System.out.println("一直运行"+Thread.currentThread().getName()); } } } public class Demo05 { public static void main(String[] args) { A1 a1 = new A1(); new Thread(a1).start(); new Thread(a1).start(); int num=0; for (;;){ num++; if (num==100000){ a1.flag=false; break; // System.exit(0); } System.out.println("-----------"); } } }