zoukankan      html  css  js  c++  java
  • Java第二十六天,多线程等待换新机制(严格执行化)

    代码:

    1.老板类:

    package com.lanyue.day26;
    
    public class bossRunnable implements Runnable {
    
        public myLock lock;
    
        public void setLock(myLock lock){
    
            this.lock = lock;
        }
    
        @Override
        public void run() {
    
            while(true){
    
                synchronized (lock){
    
                    try {
                        Thread.sleep(1500);
                    } catch (InterruptedException e) {
                        System.out.println("系统故障");
                    }
    
                    if(lock.flag()){
    
                        System.out.println("老板:小刘给我倒杯水去");
                        System.out.println("老板:小张给我整理一下资料");
    
                        lock.notifyAll();
                    }
                }
            }
        }
    }
    

    2.员工一类:

    package com.lanyue.day26;
    
    public class customerOneRunnable implements Runnable {
    
        private myLock lock;
    
        @Override
        public void run() {
    
            while (true){
    
                synchronized (lock){
    
                    if(lock.flag % 2 == 0){
    
                        System.out.println("小刘:老板,我有什么可以帮助你的吗?");
                        lock.flagTime++;
    
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            System.out.println("系统出现故障");
                        }
    
                        System.out.println("小刘:好的老板。");
                        lock.flag++;
                    }
                }
            }
        }
    
        public void setLock(myLock lock){
    
            this.lock = lock;
        }
    }
    

    3.员工二类:

    package com.lanyue.day26;
    
    public class customerTwoRunnable implements Runnable {
    
        public myLock lock;
    
        public void setLock(myLock lock){
    
            this.lock = lock;
        }
    
        @Override
        public void run() {
    
            while(true){
    
                synchronized(lock){
    
                    if(lock.flag % 2 == 0){
    
                        System.out.println("小张:老板,请问有什么吩咐");
                        lock.flagTime++;
    
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            System.out.println("系统出现故障");
                        }
    
                        System.out.println("小张:已经准备好了");
                        lock.flag++;
                    }
                }
            }
        }
    
    
    }
    

    4.控制锁类:

    package com.lanyue.day26;
    
    public class myLock {
    
        public String name;
        public int flag = 0;
        public int flagTime = 0;
    
        public void setName(String name){
    
            this.name = name;
        }
    
        public boolean flag(){
    
            if(flagTime == 2){
    
                flagTime = 0;
                return true;
    
            }else{
    
                return false;
            }
        }
    }
    

    执行代码:

    package com.lanyue.day26;
    
    public class TestDemo {
    
        public static void main(String[] args) {
    
            bossRunnable boss = new bossRunnable();
            customerOneRunnable cusOne = new customerOneRunnable();
            customerTwoRunnable cusTwo = new customerTwoRunnable();
    
            myLock lock = new myLock();
            lock.setName("同步锁");
            boss.setLock(lock);
            cusOne.setLock(lock);
            cusTwo.setLock(lock);
    
            Thread one = new Thread(boss);
            Thread two = new Thread(cusOne);
            Thread three = new Thread(cusTwo);
    
            one.start();
            two.start();
            three.start();
        }
    }
    
  • 相关阅读:
    SQL语言的组成
    存储过程中使用事务
    sql语法:inner join on, left join on, right join on详细使用方法
    Sql Server服务 远程过程调用失败
    UML学习之初步总结
    UML学习之用例图
    使用redis
    msserver的update or insert语句
    c#操作注册表的意外
    托管代码编写mssql存储过程
  • 原文地址:https://www.cnblogs.com/viplanyue/p/12700523.html
Copyright © 2011-2022 走看看