美文网首页
java 线程交替

java 线程交替

作者: zhangxuanchen | 来源:发表于2017-09-20 16:37 被阅读5次
    public class Num {
        int i=1;
        boolean flag = false; //两个线程,交替执行的一个标志
    }
    
    //打印奇数的线程
    public class PrintQi implements Runnable{
        Num num ;
        public PrintQi(Num num)
        {
            this.num = num;
        }
        public void run()
        {
            while(num.i<= 100)
            {   
                synchronized (num) {
                    if(num.flag)
                    {
                        try {
                            num.wait();
                        } catch (Exception e) {
                        }
                    }
                    else {
                        System.out.println("奇数"+num.i);
                        num.i++;
                        num.flag = true;
                        num.notify();
                    }
                }
            }
        }
    }
    
    //打印偶数的线程
    public class PrintOu implements Runnable{
        Num num;
        public PrintOu(Num num) {
            this.num = num;
        }
        public void run()
        {
            while(num.i<=100)
            {
                synchronized (num)/* 必须要用一把锁对象,这个对象是num*/ {
                    if(!num.flag)
                    {
                        try 
                        {
                            num.wait();  //操作wait()函数的必须和锁是同一个
                        } catch (Exception e) 
                        {}
                    }   
                    else {
                        System.out.println("oushu-----"+num.i);
                        num.i++;
                        num.flag = false;
                        num.notify();
                    }
                }
            }
        }
    }
    
    
    //主函数
    public class main {
        public static void main(String[] args) {
            Num num = new Num(); //声明一个资源
    
            PrintQi pQi = new PrintQi(num);
            PrintOu pOu = new PrintOu(num);
    
            Thread aThread = new Thread(pQi);
            Thread bThread = new Thread(pOu);
    
            aThread.start();
            bThread.start();
        }
    }
    

    相关文章

      网友评论

          本文标题:java 线程交替

          本文链接:https://www.haomeiwen.com/subject/mmposxtx.html