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

       运行示意部分截图:

       

  • 相关阅读:
    js与jquery实时监听输入框值的oninput与onpropertychange方法
    jQuery实现的浮动层div浏览器居中显示效果
    jquery代码规范让代码越来越好看
    asp.net截屏功能实现截取web页面
    拦截asp.net输出流并进行处理的方法
    asp.net调用系统设置字体文本框的方法
    asp.net网站防恶意刷新的Cookies与Session解决方法
    Attribute在.net编程中的应用(一)
    java学习——构造类之3!+5!=126
    java学习——构造类
  • 原文地址:https://www.cnblogs.com/yerenyuan/p/5248058.html
Copyright © 2011-2022 走看看