线程安全与并发经典问题

生产者与消费者线程并发经典问题

方式一:

 private static List<Productss> productsses = new ArrayList<>();

    private static Random random = new Random();

    public static void main(String[] args) {


        // 生产者
        new Thread(() -> {
            synchronized (productsses) {
                while (true) {
                    while (productsses.size() >= 5) {
                        try {
                            System.out.println("生产者在等待...");
                            productsses.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

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

                    Productss p = new Productss();
                    productsses.add(p);
                    System.out.println(Thread.currentThread().getName() + "生产者生产了" + p);
                    productsses.notifyAll();
                }
            }
        }).start();


        for (int i = 0; i < 10; i++) {
            // 多个消费者并发
            new Thread(() -> {
                synchronized (productsses) {
                    while (true) {
                        while (productsses.size() == 0) {
                            try {
                                System.out.println(Thread.currentThread().getName() + "在等待...");
                                productsses.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        try {
                            Thread.sleep(random.nextInt(500));
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        Productss p = productsses.remove(0);
                        System.out.println(Thread.currentThread().getName() + "消费者消费了" + p);
                        productsses.notifyAll();
                    }
                }
            }).start();
        }
    }

方式二:

package org.westos.thread.consumer;

import java.util.Random;

/**
 * *************************************
 * Copyright (c) 2018 feiyan.com
 * All rights reserved.
 * *************************************
 * *************************************
 *
 * @Author: think
 * @Project: JavaSE
 * @Date: Created in 2018/8/12 16:37
 * @Since: JDK 1.8
 * @Version: 1.0
 * @Modified By:
 * @Description:
 */
public class ConsumerAndProduct {

    // 标记是否生产产品 true : 是      false : 否
    public static boolean flag = false;

    public static final Object OBJ = new Object();

    private static Product product = null;

    static class Producter extends Thread {

        @Override
        public void run() {
            synchronized (OBJ) {
                while (true) {
                    if (!flag) {

                        Fruit[] fruits = Fruit.values();
                        Random random = new Random();

                        product = new Product(fruits[random.nextInt(4)].toString(),
                                3.5*random.nextInt(4));

                        flag = true;
                        System.out.println("生产者生产了--" + product);
                        OBJ.notify();

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


    static class Consumer extends Thread {
        @Override
        public void run() {
            synchronized (OBJ) {
                while (true) {
                    if (!flag) {
                        try {
                            OBJ.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("消费者消费了--" + product);
                        flag = false;
                        OBJ.notify();

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

    }

    public static void main(String[] args) {
        Producter producter = new Producter();
        Consumer consumer = new Consumer();

        producter.start();
        consumer.start();

    }
}

enum Fruit{
    Apple, Banana, Peak, Grape
}

class Product {
    private String name;
    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return name + " : " + price;
    }
}

线程并发卖票经典问题

package org.westos.thread;
/**
 * *************************************
 * Copyright (c) 2018 feiyan.com
 * All rights reserved.
 * *************************************
 * *************************************
 *
 * @Author: think
 * @Project: JavaSE
 * @Date: Created in 2018/8/12 11:38
 * @Since: JDK 1.8
 * @Version: 1.0
 * @Modified By:
 * @Description:${description}
 */

class SaleStick extends Thread {

    public static int stick = 50;
    public static final Object OBJ = new Object();

    public SaleStick(String name){
        super(name);
    }

    @Override
    public void run() {
        synchronized (OBJ) {

            while (true) {
                if (stick > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖出了"
                            + stick + "号票.");
                    stick--;

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

                } else {
                    System.out.println("卖完了。。。");
                    break;
                }
            }
        }

    }

}

public class ThreadSecurityTest {
    public static void main(String[] args) {
        SaleStick t1 = new SaleStick("1号窗口");
        SaleStick t2 = new SaleStick("2号窗口");
        SaleStick t3 = new SaleStick("3号窗口");

        t1.start();
        t2.start();
        t3.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

phial03

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值