java第二阶段学习day4


一个生产者 和 一个消费者

 

public class ProduceandCustomer2 {

 public static void main(String[] args) {
  Produce2 p = new Produce2();
  Customer2 c = new Customer2();
  Thread t1 = new Thread(p);
  Thread t2 = new Thread(c);
  t1.setName("生产者");
  t2.setName("消费者");
  t1.start();
  t2.start();
 }
}

class BaoZi {
 public static boolean flag;    //false 为没有包子  true 为有包子
 public static Object obj = new Object();
}

class Produce2 implements Runnable{

 @Override
 public void run() {
  synchronized (BaoZi.obj) {
   while(true){
   if(BaoZi.flag){
    //有包子时
    try {
     BaoZi.obj.wait();//生产者等待
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   // 没包子的时候
   BaoZi.flag = true; //生产者生产
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName() + "生产了一个包子");
   BaoZi.obj.notify();//唤醒 生产者 
  } 
  }
  } 
 
}
//消费者
class Customer2 implements Runnable{

 @Override
 public void run() {
  synchronized (BaoZi.obj) {
   while(true){
   if(!BaoZi.flag){
    //无包子时
    try {
     BaoZi.obj.wait();//消费者等待
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
   //有包子时
   BaoZi.flag = false; //消费者消费包子
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName() + "消费了一个包子");
   BaoZi.obj.notify();//唤醒 消费者
   }
  }
  }
}

----------------------------

静态 同步方法的 锁


public class StaticThread {

 public static void main(String[] args) {
  
 }
 
}
class MyThre implements Runnable{

 private static int sum = 0;
 public  boolean flag = true;
 @Override
  public void run() {
  if(flag){
   
  for(int i = 0;i<3;i++){
   //锁 MyThre.class 为当前类 的字节码
    synchronized (MyThre.class) {
    try {
     Thread.sleep(10);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   sum +=100;
   System.out.println(Thread.currentThread().getName() + ":" + sum);
   flag = false;
   }
  }
  }else{
   for(int i = 0;i<3;i++){
    try {
     Thread.sleep(8);
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   setrun();
   }
  }
 }
 //静态同步方法 锁为 MyThre.class
 static public void setrun(){
  
  sum +=100;
  System.out.println(Thread.currentThread().getName() + "同步方法:" + sum);
  
 }
 
-------------------------------------------

Lock 的使用 和用法


public class Lock1 {
 public static void main(String[] Baozirgs) {
  Pr p1 = new Pr();
  Cus c1 = new Cus();
  p1.setName("生产者1");
  c1.setName("消费者1");
  
  Pr p2 = new Pr();
  Cus c2 = new Cus();
  p2.setName("生产者2");
  c2.setName("消费者2");
  
  p1.start();
  c1.start();
  p2.start();
  c2.start();
 }
}
 class Baozi {
 public static int id ;
 public static List<Baozi> list = new ArrayList<Baozi>();
 //Lock 对象的创建
 public static Lock lock = new ReentrantLock();
 //生产者 和消费者 的监视
 public static Condition pr = lock.newCondition();
 public static Condition cus = lock.newCondition();
}
class Pr extends Thread{
 @Override
 public void run() {
  while(true){
   Baozi.lock.lock();
   while(Baozi.list.size() == 6){
    
     try {
      //等待
      Baozi.pr.await();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }; 
   }
   Baozi baozi = new Baozi();

     baozi.id = Baozi.id++;
   Baozi.list.add(baozi);
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName()
     + "生产了id为:" + baozi.id
     + "剩余包子:" + Baozi.list.size());
   //唤醒消费者
   Baozi.cus.signal();
   //释放锁
   Baozi.lock.unlock();
  }
 }
}


class Cus extends Thread{
 @Override
 public void run() {
  while(true){
   Baozi.lock.lock();
   while(Baozi.list.size() == 0){
     try {
      //消费者 等待
      Baozi.cus.await();
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
   }
   int index =(int)Math.random()*Baozi.list.size();
   Baozi baozi2 = Baozi.list.remove(index);
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(Thread.currentThread().getName()
     + "消费了包子id:" + baozi2.id
     + "现有包子" + Baozi.list.size());
  //唤醒 生产者
   Baozi.pr.signal();
   //释放锁
   Baozi.lock.unlock();
   
  }
 }

-----------------------------------------

线程池 --> 因为线程的创建 需要时间 而线程池 就是为了解决线程创建需要花费
很多 创建的时间 而导致程序运行效率慢 的这个问题

      线程池的创建 和使用

     //创建一个线程池对象
    //线程池里有3个线程
       ExecutorService service = Executors.newFixedThreadPool(3);

   service.execute(new Runnable() {

    @Override
       public void run(){
             for(int i = 0;i<100;i++){
            system.out.println(i);
}
}
});

          service.shudown();//销毁完成 任务的线程
          service = null; //将完成任务的线程 占用的内存 回收释放

转载于:https://my.oschina.net/u/2542711/blog/595525

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值