美文网首页
多线程综合案例

多线程综合案例

作者: 曾梦想仗剑天涯 | 来源:发表于2020-12-15 11:11 被阅读0次

    数字加减

    • 设计四个线程对象,两个线程执行减操作,两个线程执行加操作;
    package com.company;
    class AddThread implements Runnable {
        private Resource resource;
        public AddThread(Resource resource) {
            this.resource = resource;
        }
        @Override
        public synchronized void run() {
            for (int x = 0; x < 5; x++) {
                try {
                    this.resource.add();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class SubThread implements Runnable {
        private Resource resource;
        public SubThread(Resource resource) {
            this.resource = resource;
        }
        @Override
        public synchronized void run() {
            for (int x = 0; x < 5; x++) {
                try {
                    this.resource.sub();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class Resource {        //定义一个操作的类
        private int num = 0;        //定义加减操作的数据
        //flag = true 表示可以进行加法操作,不可以进行减法操作
        //flag = false 表示可以进行减法操作,不可以进行加法操作
        private boolean flag = true;
        public synchronized void add() throws InterruptedException {
            if (!this.flag) {
                super.wait();
            }
            Thread.sleep(500);
            this.num++;
            System.out.println("加法操作 - " + Thread.currentThread().getName() + " num = " + this.num);
            this.flag = false;
            super.notifyAll();
        }
        public synchronized void sub() throws InterruptedException {
            if (this.flag) {
                super.wait();
            }
            Thread.sleep(500);
            this.num--;
            System.out.println("减法操作 - " + Thread.currentThread().getName() + " num = " + this.num);
            this.flag = true;
            super.notifyAll();
        }
    }
    public class MathThread {
        public static void main(String[] args) {
            Resource resource = new Resource();
            AddThread at = new AddThread(resource);
            SubThread st = new SubThread(resource);
            new Thread(at, "加法线程 - A").start();
            new Thread(at, "加法线程 - B").start();
            new Thread(st, "减法线程 - X").start();
            new Thread(st, "减法线程 - Y").start();
        }
    }
    

    生产电脑

    • 设计一个生产电脑和搬运电脑类,要求生产出一个电脑就搬走一个电脑,如果没有新的电脑生产出来,则搬运工需要等待电脑产出,如果生产出的电脑没有搬走,则要等待电脑搬走之后再生产,并统计出生产的电脑数量;
    package com.company;
    class Computer {
        public static int count = 0;
        private String title;
        private double price;
        public Computer(String title, double price) {
            this.title = title;
            this.price = price;
            count++;
        }
        public String toString() {
            return "第" + count + "电脑品牌:" + this.title + "、电脑价格:" + this.price;
        }
    }
    class Produce implements Runnable {
        private Resources resources;
        public Produce(Resources resources) {
            this.resources = resources;
        }
        @Override
        public void run() {
            for (int x = 0; x < 5; x++) {
                try {
                    this.resources.create();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class Consumers implements Runnable {
        private Resources resources;
        public Consumers(Resources resources) {
            this.resources = resources;
        }
        @Override
        public void run() {
            for (int x = 0; x < 5; x++) {
                try {
                    this.resources.get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }    
    }
    class Resources {
        private Computer computer;
        public synchronized void create() throws Exception {
            if (this.computer != null) {
                super.wait();
            }
            Thread.sleep(100);
            this.computer = new Computer("XM", 999.0);
            System.out.println("生产电脑信息为:" + this.computer);
            super.notifyAll();
        }
        public synchronized void get() throws Exception {
            if (this.computer == null) {
                super.wait();
            }
            System.out.println("销售电脑信息:" + this.computer);
            Thread.sleep(10);
            this.computer = null;
            super.notifyAll();
        }
    }
    public class ComputerThread {
        public static void main(String[] args) {
            Resources resources = new Resources();
            new Thread(new Produce(resources)).start();
            new Thread(new Consumers(resources)).start();
        }
    }
    

    竞争抢答

    • 实现一个竞拍抢答程序:要求设置三个抢答者(三个线程),而后同时发出抢答指令,抢答成功者给出成功提示,抢答失败者给出失败提示;
    package com.company;
    import java.util.concurrent.Callable;
    import java.util.concurrent.FutureTask;
    class MyThread implements Callable<String> {
        private boolean flag = false;
        @Override
        public String call() throws Exception {
            if (!this.flag) {
                this.flag = true;
                return Thread.currentThread().getName() + "抢答成功";
            } else {
                return Thread.currentThread().getName() + "抢答失败";
            }
        }
    }
    public class AnswersThread {
        public static void main(String[] args) throws Exception {
            MyThread mt = new MyThread();
            FutureTask<String> taskA = new FutureTask<String>(mt);
            FutureTask<String> taskB = new FutureTask<String>(mt);
            FutureTask<String> taskC = new FutureTask<String>(mt);
            new Thread(taskA, "抢答者A").start();
            new Thread(taskB, "抢答者B").start();
            new Thread(taskC, "抢答者C").start();
            System.out.println(taskA.get());
            System.out.println(taskB.get());
            System.out.println(taskC.get());
        }
    }
    

    相关文章

      网友评论

          本文标题:多线程综合案例

          本文链接:https://www.haomeiwen.com/subject/wrkrgktx.html