线程小案例

Posted alida

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线程小案例相关的知识,希望对你有一定的参考价值。

一、龟兔赛跑

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;
    }
}

 

以上是关于线程小案例的主要内容,如果未能解决你的问题,请参考以下文章

线程小案例

线程小案例

微信小程序代码片段

关于JVM案例分析

Android 多线程下载,断点续传,线程池

newCacheThreadPool()newFixedThreadPool()newScheduledThreadPool()newSingleThreadExecutor()自定义线程池(代码片段