码迷,mamicode.com
首页 > 编程语言 > 详细

线程小案例

时间:2020-05-31 11:05:30      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:color   sys   mon   getname   rri   乌龟   zed   rup   div   

一、龟兔赛跑

package cn.xej.thread;

public class Rice implements Runnable{
    private int rSpeed = 5; //兔子速度
    private int gSpeed = 2; //乌龟速度
    private int rSum = 0;   //兔子跑了多少米
    private int gSum = 0;   //乌龟跑了多少米
    boolean flag = true;

    @Override
    public void run() {
        while (flag){
            if(Thread.currentThread().getName().equals("兔子")){
                if(rSum >= 100){
                    System.out.println("兔子是胜利者");
                    flag = false;
                }
                rSum = rSum + rSpeed;
                System.out.println("我是: "+Thread.currentThread().getName()+"跑了: "+rSum);
            }
            if(Thread.currentThread().getName().equals("乌龟")){
                if(gSum >= 100){
                    System.out.println("乌龟是胜利者");
                    flag = false;
                }
                gSum = gSum + gSpeed;
                System.out.println("我是: "+Thread.currentThread().getName()+"跑了: "+gSum);
            }
        }
    }

    public static void main(String[] args) {
        Rice rice = new Rice();
        new Thread(rice,"兔子").start();
        new Thread(rice,"乌龟").start();

    }
}

 

二、账户取钱问题(线程安全加锁)

package cn.xej.thread.money;

public class SaveMoneyDemo {
public static void main(String[] args) {
Bank bank = new Bank();
Customer customer1 = new Customer(bank,150,"draw");
Customer customer2 = new Customer(bank,20,"save");
Customer customer3 = new Customer(bank,200,"draw");
new Thread(customer1,"张三").start();
new Thread(customer2,"王五").start();
new Thread(customer3,"李四").start();

}
}

class Customer implements Runnable{
Bank bank;
int money;
String type;
static final String SAVE = "save";
static final String DRAW = "draw";

public Customer(Bank bank, int money,String type) {
this.bank = bank;
this.money = money;
this.type = type;
}

@Override
public void run() {
synchronized (bank) {
if (SAVE.equals(type)) {

bank.saveMoney(money);
System.out.println("我是:" + Thread.currentThread().getName() + "卡里剩余:" + bank.count);
}
if (DRAW.equals(type)) {
if (bank.count - money <= 0) {
System.out.println("卡里钱不够");
return;
}


bank.drawMoney(money);
System.out.println("我是:" + Thread.currentThread().getName() + "卡里剩余:" + bank.count);
}
}
}
}

class Bank{
int count = 300;

public void saveMoney(int money){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
count += money;
//System.out.println("卡里剩余: "+count);
}

public void drawMoney(int money){
if(count-money<=0){
System.out.println("余额不足");
return;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
count -= money;
//System.out.println("卡里剩余: "+count);
}
}

 

三、影院买票问题

package cn.xej.thread;

import java.util.ArrayList;
import java.util.List;

public class SafeCinema {
    public static void main(String[] args) {
        List<Integer> available = new ArrayList<Integer>();
        available.add(1);
        available.add(2);
        available.add(3);
        available.add(6);
        available.add(7);
        available.add(4);

        List<Integer> seat1 = new ArrayList<Integer>();
        seat1.add(1);
        seat1.add(2);
        seat1.add(6);

        List<Integer> seat2 = new ArrayList<Integer>();
        seat2.add(4);
        seat2.add(7);
        seat2.add(8);


        Cinema cinema = new Cinema(available,"娃哈哈");
        Customer customer1 = new Customer(seat1,"张三",cinema);
        Customer customer2 = new Customer(seat2,"李四",cinema);
        new Thread(customer1,"张三").start();
        new Thread(customer2,"李四").start();

    }

}

class Customer implements Runnable{
    List<Integer> seat; // 选的位置
    String name; //名字
    Cinema cinema;

    public Customer(List<Integer> seat, String name, Cinema cinema) {
        this.seat = seat;
        this.name = name;
        this.cinema = cinema;
    }

    @Override
    public void run() {
        if(cinema.available.size()<=0){
            return;
        }
        synchronized (cinema) {
            boolean flag = cinema.buyTickets(seat);
            if(flag){
                System.out.println("我是: " + Thread.currentThread().getName() + " 位置足够,电影院票剩余 " + cinema.available);
            }else{
                System.out.println("位置不够,电影院票剩余 " + cinema.available);
                return;
            }

        }
    }
}

// 电影院
class Cinema{
    List<Integer> available; //可用的位置
    String name; //名称

    public Cinema(List<Integer> available, String name) {
        this.available = available;
        this.name = name;
    }

    // 购票
    public boolean buyTickets(List<Integer> seat){
        System.out.println("可用位置为: "+available);
        List<Integer> copy = new ArrayList<Integer>();
        copy.addAll(available);

        //相减
        copy.removeAll(seat);
        //判断大小
        if(available.size()-copy.size()!=seat.size()){
            return false;
        }
        //成功
        available = copy;
        return true;
    }
}

 

线程小案例

标签:color   sys   mon   getname   rri   乌龟   zed   rup   div   

原文地址:https://www.cnblogs.com/Alida/p/12995212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!