zoukankan      html  css  js  c++  java
  • Java多线程编程题

    1. 分别设计一个Account类,和两个任务类 Company和Bank: (1)Account类拥有账户余额属性和余额增减操作;(2)Company任务类能增加Account对象中的余额;(3)Bank任务类能减少(或使用)Account对象中的余额(比如用于基金投资)。要求使用同一account对象来分别创建两个Company线程对象和两个Bank线程对象,通过两次测试来完成以下两任务(实验报告中 截两个图对应以下两个任务):

     1) 出现Account对象余额不一致问题(同步问题);

     2)使用同步代码块或同步方法或Lock对象来解决同步问题。

    package com.join;
    
    class Bank implements Runnable {
    
        private Account account;
    
        Bank(Account account) {
            this.account = account;
        }
    
        @Override
        public void run() {
            for (int i = 0; i != 5; ++i) {
                account.decreaseBalance(10);
            }
        }
    }
    
    class Company implements Runnable {
    
        private Account account;
    
        Company(Account account) {
            this.account = account;
        }
    
        @Override
        public void run() {
            for (int i = 0; i != 5; ++i) {
                account.increaseBalance(10);
            }
        }
    }
    
    public class Account {
    
        private double balance;
    
        private Account(double balance) {
            this.balance = balance;
        }
    
        void increaseBalance(double increment) {
            double b = balance;
            b = balance + increment;
            balance = b;
        }
    
        void decreaseBalance(double decrement) {
            double b = balance;
            b = balance - decrement;
            balance = b;
        }
    
        private synchronized double getBalance() {
            return balance;
        }
    
        public static void main(String[] args) {
            Account account = new Account(100);
            Company company = new Company(account);
            Bank bank = new Bank(account);
            Thread t1 = new Thread(company);
            Thread t2 = new Thread(company);
            Thread t3 = new Thread(bank);
            Thread t4 = new Thread(bank);
            // 或者给方法加锁
            try {
                t1.start();
                t1.join();
                t2.start();
                t2.join();
                t3.start();
                t3.join();
                t4.start();
                t4.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("最终余额为:" + account.getBalance());
        }
    }

    2. 设计一个生产电脑和搬运电脑类,要求生产一台电脑就搬走一台电脑,如果没有新的电脑生产出来,则搬运工要等待新电脑产出;如果生产出的电脑没有搬走,则要等待电脑搬走之后在生产,并统计出生产的电脑数量。

    package com.my.test;
    
    class Producer extends Thread {
    
        int count = 0;
        boolean hasNewComputer = false;
    
        public Producer(String name) {
            super(name);
        }
    
        private synchronized void produceComputer() throws InterruptedException {
            while (hasNewComputer) {
                wait();
            }
            Thread.sleep(3000);
            hasNewComputer = true;
            System.out.println(Thread.currentThread().getName() + ":生产第" + ++count + "台电脑");
            notify();
        }
    
        public synchronized void consumeComputer(int count) throws InterruptedException {
            while (!hasNewComputer) {
                wait();
            }
            Thread.sleep(3000);
            hasNewComputer = false;
            System.out.println(Thread.currentThread().getName() + ":第" + count + "台电脑被消费");
            notify();
        }
    
    
        @Override
        public void run() {
            while (true) {
                try {
                    produceComputer();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class Consumer extends Thread {
    
        int count = 0;
        Producer producer;
    
        public Consumer(String name, Producer producer) {
            super(name);
            this.producer = producer;
        }
    
        @Override
        public void run() {
            while (true) {
                try {
                    producer.consumeComputer(++count);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Producer producer = new Producer("producer");
            Consumer consumer = new Consumer("consumer", producer);
            producer.start();
            consumer.start();
        }
    }
  • 相关阅读:
    闭包Closures
    jsp中的四种对象作用域
    JFrame小练习1
    swift函数的用法,及其嵌套实例
    图的储存
    洛谷P3366 【模板】最小生成树(Kruskal && Prim)
    洛谷P3371 【模板】单源最短路径(弱化版)(SPFA解法)
    POJ 1577 Falling Leaves
    POJ 3784 Running Median
    洛谷P3374 【模板】树状数组 1
  • 原文地址:https://www.cnblogs.com/xkxf/p/10080051.html
Copyright © 2011-2022 走看看