Java学习路线-16:多线程综合案例

第6 章 : 多线程综合案例

23 数字加减

4个线程,2个线程加,2个线程减
循环出现 加一个,减一个


// 资源
class Resource {
    private int count = 0;

    // 为false可以增加,加完了设置为true,
    // 为true可以减少,减完了设置为false
    private boolean flag = false;

    public synchronized void add() {
        if (this.flag == true) {

            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        this.count++;
        System.out.println(Thread.currentThread().getName() + " count=" + count);

        this.flag = true;
        super.notifyAll();
    }

    public synchronized void sub() {
        if (this.flag == false) {
            System.out.println(this.flag);
            try {
                super.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        this.count--;
        System.out.println(Thread.currentThread().getName() + " count=" + count);

        this.flag = false;
        super.notifyAll();
    }
}

// 加法线程
class AddThread implements Runnable {
    private Resource resource;

    public AddThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            this.resource.add();
        }
    }
}


// 减法线程
class SubThread implements Runnable {
    private Resource resource;

    public SubThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            this.resource.sub();
        }
    }
}


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

并没有出现一加一减的现象

24 生产电脑

生产一台搬运一台

消费者生产者模型

class Computer{
    private static int count;

    public Computer() {
        count++;
    }

    @Override
    public String toString() {
        return "电脑编号:" + count;
    }
}


class Resource{
    private Computer computer;

    public synchronized void make(){
        if(computer != null){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        this.computer = new Computer();
        System.out.println("生产:" + this.computer);
        notifyAll();
    }

    public synchronized void get(){
        if(computer == null){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("取走:" + this.computer);
        this.computer = null;
        notifyAll();
    }
}

class Producer implements Runnable{
    private Resource resource;

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            resource.make();
        }
    }
}


class Consumer implements Runnable{
    private Resource resource;

    public Consumer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            resource.get();
        }
    }
}

class Demo{
    public static void main(String[] args) {
        Resource resource = new Resource();
        new Thread(new Producer(resource)).start();
        new Thread(new Consumer(resource)).start();
    }
}

25 竞争抢答

3个抢答线程,同时发出抢答指令
成功和失败都给与提示

有数据返回,采用Callable方式

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 {
        synchronized (this) {
            // 如果没有人抢答成功则可以抢答
            if (this.flag == false) {
                this.flag = true;
                return Thread.currentThread().getName() + "抢答成功!";
            } else {
                return Thread.currentThread().getName() + "抢答失败!";
            }
        }
    }
}

class Demo {
    public static void main(String[] args) throws Exception {
        MyThread t = new MyThread();

        FutureTask<String> task1 = new FutureTask<String>(t);
        FutureTask<String> task2 = new FutureTask<String>(t);
        FutureTask<String> task3 = new FutureTask<String>(t);

        new Thread(task1, "抢答者A").start();
        new Thread(task2, "抢答者B").start();
        new Thread(task3, "抢答者C").start();

        System.out.println(task1.get());
        System.out.println(task2.get());
        System.out.println(task3.get());
    }
}
发布了1339 篇原创文章 · 获赞 317 · 访问量 113万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览