zoukankan      html  css  js  c++  java
  • 第9章 多线程

       1、

    /*
    需求:设计4个线程对象,两个线程执行减操作,两个线程执行加操作。
    类似于多个生产者和多个消费者的例子
     */
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    class Resource {
        private int num;
        private boolean flag = false;
        
        private Lock lock = new ReentrantLock(); 
        private Condition condition_add = lock.newCondition();
        private Condition condition_sub = lock.newCondition();
        
        public void add() throws InterruptedException {
            lock.lock();
            try {
                while(flag) {
                    condition_add.await();
                }
                this.num++;
                System.out.println(Thread.currentThread().getName() + 
                        "...执行加操作之后... num = " + num);
                flag = true;
                condition_sub.signal();
            } finally {
                lock.unlock();
            }
        }
        
        public void subtract() throws InterruptedException {
            lock.lock();
            try {
                while(!flag) {
                    condition_sub.await();
                }
                this.num--;
                System.out.println(Thread.currentThread().getName() + 
                        "........执行减操作之后... num = " + num);
                flag = false;
                condition_add.signal();
            } finally {
                lock.unlock();
            }
        }
    }
    class Addition implements Runnable {
        private Resource res;
        
        Addition(Resource res) {
            this.res = res;
        }
        
        public void run() {
            while(true) {
                try {
                    res.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class Subtraction implements Runnable {
        private Resource res;
        
        Subtraction(Resource res) {
            this.res = res;
        }
        
        public void run() {
            while(true) {
                try {
                    res.subtract();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public class Practise1 {
    
        public static void main(String[] args) {
            Resource res = new Resource();
            Addition add = new Addition(res);
            Subtraction sub = new Subtraction(res);
            
            Thread t1 = new Thread(add, "加操作——线程1");
            Thread t2 = new Thread(add, "加操作——线程2");
            
            Thread t3 = new Thread(sub, "减操作——线程1");
            Thread t4 = new Thread(sub, "减操作——线程2");
            
            t1.start();
            t2.start();
            t3.start();
            t4.start();
        }
    
    }

       运行部分示意图:

       

       2、

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    class Computer {
        private String name;
        private int num = 1;
        private boolean flag = false;
        private static int count = 0;
        
        private Lock lock = new ReentrantLock();
        //private Condition condition_producer = lock.newCondition();
        //private Condition condition_carrier = lock.newCondition();
        private Condition condition = lock.newCondition();
        
        public void setComputer(String name) throws InterruptedException {
            lock.lock();
            try {
                while(flag) {
                    condition.await();
                }
                this.name = name + "-型号:" + num++;
                System.out.println(Thread.currentThread().getName() + "生产电脑——>" + 
                        this.name + ",共生产了" + count++ + "台电脑");
                flag = true;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }
        
        public void getComputer() throws InterruptedException {
            lock.lock();
            try {
                while(!flag) {
                    condition.await();
                }
                System.out.println(Thread.currentThread().getName() + "搬运电脑——>" + this.name);
                flag = false;
                condition.signal();
            } finally {
                lock.unlock();
            }
        }
        
    }
    class Producer implements Runnable {
        private Computer com;
        
        Producer(Computer com) {
            this.com = com;
        }
        
        public void run() {
            while(true) {
                try {
                    com.setComputer("苹果");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class Carrier implements Runnable {
        private Computer com;
        
        Carrier(Computer com) {
            this.com = com;
        }
        
        public void run() {
            while(true) {
                try {
                    com.getComputer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public class Practise2 {
    
        public static void main(String[] args) {
            Computer computer = new Computer();
            
            Producer producer = new Producer(computer);
            Carrier carrier = new Carrier(computer);
            
            Thread t1 = new Thread(producer, "生产1号线");
            Thread t2 = new Thread(carrier, "1号搬运工人");
            
            t1.start();
            t2.start();
        }
    
    }

       运行示意部分截图:

       

  • 相关阅读:
    wsl中的git问题
    接口测试框架实战(三)| JSON 请求与响应断言
    接口测试框架实战(二)| 接口请求断言
    面试大厂测试开发之前,你需要做好哪些准备?
    接口测试框架实战(一) | Requests 与接口请求构造
    实战 | UI 自动化测试框架设计与 PageObject 改造
    用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告
    外包测试的职业发展利弊分析与建议
    做软件测试,到底能不能去外包?
    移动自动化测试入门,你必须了解的背景知识和工具特性
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5248058.html
Copyright © 2011-2022 走看看