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();
        }
    
    }

       运行示意部分截图:

       

  • 相关阅读:
    PHP 单态设计模式
    五中常见的PHP设计模式
    PHP如何定义类及其成员属性与操作
    thinkphp 中MVC思想
    1.4 算法
    1.3 迭代器
    1.2 容器-container
    1.1 STL 概述
    2.3顺序容器-deque
    2.2 顺序容器-list
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5248058.html
Copyright © 2011-2022 走看看