zoukankan      html  css  js  c++  java
  • java 10-04 死锁 notifyall()

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

    同步代码

    class TenProductCustorwait5{
    public static void main(String[] agrs) {
    Pool pool = new Pool();
    Producer p = new Producer(pool);

    p.setName("p1");
    Consumer c = new Consumer(pool);

    Consumer c1 = new Consumer(pool);

    c .setName("c ");

    c1 .setName("c1 ");


    p.start();
    c.start();

    c1.start();



    }
    class Pool{
    private int Max =1;
    private java.util.List<Integer> list =new java.util.LinkedList<Integer>();

    public void addLast(int i){
    //以当前对象Pool做锁,把当前线程放到锁的队列中去

    synchronized(this){

    try{
    while(list.size()>= Max){

    Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

    System.out.println(ct.getName()+".wait");
    this.wait();
    }

    list.add(new Integer(i));

    System.out.println(ct.getName()+":"+i);

    System.out.println(ct.getName()+".notify");
    this.notify();//通知和等待都是掉锁的方法

    }

    catch(Exception e){
    e.printStackTrace();
    }

    }
    }

    public int Remove(){
    //以当前对象Pool做锁,把当前线程放到锁的队列中去
    synchronized(this){

    try{
    while(list.size()==0){

    Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

    System.out.println(ct.getName()+".wait");

    this.wait();
    }

    int no =list.remove(0);

    System.out.println(ct.getName()+":"+i);

    System.out.println(ct.getName()+".notify");
    this.notify();
    return no;

    }
    catch(Exception e){

    e.printStackTrace();
    }
    }

    }
    }

    //在死锁的线程加上名称

    class Producer extends Thread{

    private Pool pool;
    static int i =1;
    public Producer( Pool pool){

    this.name=name;
    this.pool =pool;
    }
    public void run(){
    while(true){
    pool.addLast(i++);
    System.out.println("生产者"+i);
    }
    }
    }

    class Consumer extends Thread{

    private Pool pool;
    public Consumer(Pool pool){
    this.pool =pool;

    this.name=name;
    }
    public void run(){
    while(true){
    int no = pool.Remove();
    System.out.println("消费了"+no);
    }
    }
    }

    wait(int n)

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

    class TenProductCustorwait5{
    public static void main(String[] agrs) {
    Pool pool = new Pool();
    Producer p = new Producer(pool);

    p.setName("p1");
    Consumer c = new Consumer(pool);

    Consumer c1 = new Consumer(pool);

    c .setName("c ");

    c1 .setName("c1 ");


    p.start();
    c.start();

    c1.start();



    }
    class Pool{
    private int Max =1;
    private java.util.List<Integer> list =new java.util.LinkedList<Integer>();

    public void addLast(int i){
    //以当前对象Pool做锁,把当前线程放到锁的队列中去

    synchronized(this){

    try{
    while(list.size()>= Max){

    Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

    System.out.println(ct.getName()+".wait");

    //等待10毫秒 没人通知即撤 不等待了 或者用this.notifyAll();
    this.wait(10);
    }

    list.add(new Integer(i));

    System.out.println(ct.getName()+":"+i);

    System.out.println(ct.getName()+".notify");
    this.notify();//通知和等待都是掉锁的方法

    //this.notifyAll();

    }

    catch(Exception e){
    e.printStackTrace();
    }

    }
    }

    public int Remove(){
    //以当前对象Pool做锁,把当前线程放到锁的队列中去
    synchronized(this){

    try{
    while(list.size()==0){

    Thread ct=Thread.currentThread();//获取当前线程的名称 .getName()

    System.out.println(ct.getName()+".wait");

    this.wait(10);
    }

    int no =list.remove(0);

    System.out.println(ct.getName()+":"+i);

    System.out.println(ct.getName()+".notify");
    this.notify();

    //this.notifyAll();
    return no;

    }
    catch(Exception e){

    e.printStackTrace();
    }
    }

    }
    }

    //在死锁的线程加上名称

    class Producer extends Thread{

    private Pool pool;
    static int i =1;
    public Producer( Pool pool){

    this.name=name;
    this.pool =pool;
    }
    public void run(){
    while(true){
    pool.addLast(i++);
    System.out.println("生产者"+i);
    }
    }
    }

    class Consumer extends Thread{

    private Pool pool;
    public Consumer(Pool pool){
    this.pool =pool;

    this.name=name;
    }
    public void run(){
    while(true){
    int no = pool.Remove();
    System.out.println("消费了"+no);
    }
    }
    }

  • 相关阅读:
    【译】用 Chart.js 做漂亮的响应式表单
    【译】快速高效学习Java编程在线资源Top 20
    【译】理解Spring MVC Model Attribute 和 Session Attribute
    Github 恶搞教程(一起『玩坏』自己的 Github 吧)
    Effective Java 读书笔记(一):使用静态工厂方法代替构造器
    JavaScript 中 onload 事件绑定多个方法的优化建议
    【译】常见 Java 异常解释(恶搞版)
    Java 重写 equals 与 hashCode 的注意事项
    【译】Java语言速览:StackOverflow
    【译】StackOverflow——Java 中的 finally 代码块是否总会被执行?
  • 原文地址:https://www.cnblogs.com/simly/p/10833232.html
Copyright © 2011-2022 走看看