/* 卖票程序 同步代码块形式 */ class Ticket implements Runnable { //接口没有抛出异常,所以run()方法内只能捕捉异常 private int num = 100; Object obj = new Object(); public void run() { while(true) { try{ Thread.sleep(100);//---测试多线程调用时,数据num出现负数 }catch(InterruptedException e) { System.out.println("线程出错了"); } synchronized(obj){ if(num>0){ System.out.println(Thread.currentThread().getName() + "----- sale ----" + num--); } } } } } class TicketDemo { public static void main(String[] args) { Ticket t = new Ticket();//---只有一个Ticket对象 Thread t1 = new Thread(t); // Thread t2 = new Thread(new Ticket()); // Thread t3 = new Thread(new Ticket()); // Thread t4 = new Thread(new Ticket()); //---4个Ticket对象需要使用static修饰num Thread t2 = new Thread(t); Thread t3 = new Thread(t); Thread t4 = new Thread(t); t1.start(); t2.start(); t3.start(); t4.start(); }
/* 多线程下的单例设计模式 */ //---饿汉式 class SingleHg { private static final SingleHg s = new SingleHg(); private SingleHg(){} public static SingleHg getInstance(){ return s; } } //---懒汉式 class SingleLz { private static SingleLz s = null; private SingleLz(){} public static SingleLz getInstance(){ if(s==null) s = new SingleLz(); return s; } } //---加同步锁的懒汉式,面试常考,一般实际用饿汉式 class Single { private static Single s = null; private Single(){} public static /*synchronized*/ Single getInstance(){ //---使用同步函数会降低效率,每次都要获取锁 if(s==null) synchronized(Single.class){ if(s==null) s = new Single(); } return s; } }
多个生产者消费者,需要用while()循环判断标记;
class Duck { private int id; Duck(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Duck [id=" + id + "]"; } } class DuckBasket { int index = 0; Duck[] ducks = new Duck[5]; public synchronized void push(Duck duck) { while(index==ducks.length) try { System.out.println(Thread.currentThread().getName()+":篮子满了,歇会~~~~~~~~~!"); this.wait(); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } ducks[index] = duck; System.out.println(Thread.currentThread().getName()+"生产了"+duck); index++; notify(); } public synchronized Duck pop() { while(index==0) try { System.out.println(Thread.currentThread().getName()+":空了,快生产烤鸭!"); wait(); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } index--; //---打印输出用到了共享数据,要放到同步代码中 System.out.println(Thread.currentThread().getName()+"消费了"+ducks[index]); notify(); return ducks[index]; } } class Producer implements Runnable { DuckBasket db; public Producer(DuckBasket db) { super(); this.db = db; } @Override public void run() { for(int i=0; i<20; i++) { Duck duck = new Duck(i); db.push(duck); } } } class Consumer implements Runnable { DuckBasket db; public Consumer(DuckBasket db) { super(); this.db = db; } @Override public void run() { for(int i=0; i<20; i++) { db.pop(); } } } public class ProducerConsumerDemo { /** * @param args */ public static void main(String[] args) { DuckBasket db = new DuckBasket(); Producer pro1 = new Producer(db); Consumer con1 = new Consumer(db); Thread t1 = new Thread(pro1,"生产者----1"); Thread t2 = new Thread(con1,"消费者1"); Thread t3 = new Thread(pro1,"生产者----2"); Thread t4 = new Thread(con1,"消费者2"); t1.start(); t2.start(); t3.start(); t4.start(); } }
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class Duck { private int id; Duck(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Duck [id=" + id + "]"; } } class DuckBasket { int index = 0; Duck[] ducks = new Duck[5]; Lock lock = new ReentrantLock(); Condition proL = lock.newCondition(); Condition conL = lock.newCondition(); public void push(Duck duck) { lock.lock(); try{ while(index==ducks.length) try { System.out.println(Thread.currentThread().getName()+":篮子满了,歇会~~~~~~~~~!"); proL.await(); } catch (InterruptedException e) { e.printStackTrace(); } ducks[index] = duck; System.out.println(Thread.currentThread().getName()+"做了"+duck); index++; conL.signal(); }finally{ lock.unlock(); } } public Duck pop() { lock.lock(); try { while(index==0) try { System.out.println(Thread.currentThread().getName()+":空了,快做烤鸭!"); conL.await(); } catch (InterruptedException e) { e.printStackTrace(); } index--; //---打印输出用到了共享数据,要放到同步代码中 System.out.println(Thread.currentThread().getName()+"消费了"+ducks[index]); proL.signal(); return ducks[index]; } finally{ lock.unlock(); } } } class Producer implements Runnable { DuckBasket db; public Producer(DuckBasket db) { super(); this.db = db; } @Override public void run() { for(int i=0; i<20; i++) { Duck duck = new Duck(i); db.push(duck); } } } class Consumer implements Runnable { DuckBasket db; public Consumer(DuckBasket db) { super(); this.db = db; } @Override public void run() { for(int i=0; i<20; i++) { db.pop(); } } } public class ProConLock { /** * @param args */ public static void main(String[] args) { DuckBasket db = new DuckBasket(); Producer pro1 = new Producer(db); Consumer con1 = new Consumer(db); Thread t1 = new Thread(pro1,"生产者----1"); Thread t2 = new Thread(con1,"消费者1"); Thread t3 = new Thread(pro1,"生产者----2"); Thread t4 = new Thread(con1,"消费者2"); t1.start(); t2.start(); t3.start(); t4.start(); }