zoukankan      html  css  js  c++  java
  • java多线程编程(2)交替输出数字和字母

    mark一下,不停的看看notify和wait的没有理解

    class Printer
    {
        int index=0;
        //输出奇数
        public synchronized void printA(int a)
        {
            while(index%2==0)
            {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            index++;
            System.out.println(a);
            notify();
            
        }
        public  synchronized void printB(char b)
        {
            while(index%2!=0)
            {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            index++;
            System.out.println(b);
            notify();
            
        }
        
    }
    public class 多线程1 {
        public static void main(String args[])
        {
            Printer p=new Printer();
            Thread t1=new Thread(new A(p));
            Thread t2=new Thread(new B(p));
            t1.start();
            t2.start();
            
            
            
            
        }
        
    
    }
    
    class A implements Runnable
    {
        Printer p=null;
        public A(Printer p)
        {
            this.p=p;
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            for(int i=1;i<=26;i++)
            {
            p.printA(i);
            }
            
            
        }
        
        
    }
    class B implements Runnable
    {
        Printer p=null;
        public B(Printer p)
        {
            this.p=p;
        }
    
        public void run() {
            // TODO Auto-generated method stub
            for(char c='a';c<='z';c++)
            {
            p.printB(c);
            }
            
            
        }
        
        
    }
  • 相关阅读:
    drf规范——请求与响应
    序列化器——Serializer
    drf 安装与配置
    CBV源码——View,APIView
    django restful framework —— Drf 规范一
    Vue——五
    Vue——四
    今日复习
    冒泡排序
    考试总结
  • 原文地址:https://www.cnblogs.com/hansongjiang/p/3854171.html
Copyright © 2011-2022 走看看