多线程小练习

模拟两个人AB通过一个账户A在柜台取钱和B在ATM机取钱,程序分析:钱的数量要设置成一个静态的变量。两个人要取的同一个对象值。

1.创建一个银行实体类

public class Bank {

    static int money = 1000;

    //柜台取钱的方法
    public void Counter(int money) {
        Bank.money -= money;
        System.out.println("A取走了" + money + "还剩下" + (Bank.money));
    }

    //ATM机取钱的方法
    public void ATM(int money) {
        Bank.money -= money;
        System.out.println("B取走了" + money + "还剩下" + (Bank.money));
    }
}

2.创建一个人A

public class PersonA extends Thread{
    //创建银行对象
    Bank bank;

    public PersonA(Bank bank){
        this.bank = bank;
    }

    @Override
    public void run() {
        while (Bank.money>=30){
            bank.Counter(30);//每次取100块钱
            try {
                Thread.sleep(100);//取完休息0.1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

3.创建另一个人B

public class PersonB extends Thread{
    //创建银行对象
    Bank bank;

    public PersonB(Bank bank){
        this.bank = bank;
    }

    @Override
    public void run() {
        while (Bank.money>=30){
            bank.ATM(30);//每次取200块钱
            try {
                Thread.sleep(100);//趣玩休息0.1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

4.创建一个主函数

public class MainClass {

    public static void main(String[] args) {
    //创建银行
        Bank bank = new Bank();
        PersonA pa = new PersonA(bank);
        PersonB pb = new PersonB(bank);

        pa.start();
        pb.start();
    }
}

实现有三个人同时去银行取款机取钱,这三个同时操作。设银行共有10000元,第一人取1500,第二人取2000,第 三人取3000,结算银行剩余多少钱?

1.创建一个银行

public class Bank implements Runnable {
 
	int totalMoney = 10000;
	int money;// 要取出的钱
 
	public void getMoney(int money) {
		this.money = money;
	}


	@Override
	public void run() {
		String name = Thread.currentThread().getName();
		System.out.println(name + "取出:" + money);
		int remainMoney = totalMoney - money;
		totalMoney = remainMoney;
		System.out.println("银行剩余:" + remainMoney);
	}
}

结果:
在这里插入图片描述

2.编写主函数

public class TestThread {
	public static void main(String[] args) {
		Bank bank = new Bank();
 
		bank.getMoney(1500);
		Thread person1 = new Thread(bank);
		person1.setName("==person1==");
		person1.start();
		try {
			//合并线程
			person1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
 
		bank.getMoney(2000);
		Thread person2 = new Thread(bank);
		person2.setName("==person2==");
		person2.start();
		try {
			person2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		bank.getMoney(3000);
		Thread person3 = new Thread(bank);
		person3.setName("==person3==");
		person3.start();
		try {
			person3.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

结果:
在这里插入图片描述

多线程模拟龟兔赛跑:

规则:龟兔同时起步,每10毫秒秒跑1米,终点为100米,兔子跑步的能力强,乌龟跑步的能力弱 途中: 1.兔子跑到10米的时候,谦让乌龟一下,接着跑
2.兔子跑到50米的时候,再让龟1毫秒,接着跑
3.兔子跑到80米的时候,睡了50毫秒,接着跑
分析: 兔子跑步的能力强,乌龟跑步的能力弱(优先级的设置)
1.兔子跑到10米的时候,谦让乌龟一下,接着跑(yield方法)
2.兔子跑到50米的时候,再让龟1毫秒,接着跑(sleep方法)
3.兔子跑到80米的时候,睡了50毫秒,接着跑(sleep方法)
4.乌龟全程没有停留

1.创建乌龟类

/**
 * 乌龟类
 */
public class Tortoise implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i <=100; i++) {

            try {
                //每隔10毫秒跑一次,全程一直跑
                Thread.sleep(10);
                System.out.println(Thread.currentThread().getName()+"跑了"+i+"米");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

2.创建兔子类

/**
 * 兔子类-->兔子在中途休息了三次
 */
public class Rabbit implements Runnable {


    @Override
    public void run() {
        try {
            for (int i = 0; i <= 100; i++) {

                Thread.sleep(10);
                System.out.println(Thread.currentThread().getName() + "跑了" + i + "米");
                if (i == 10) {
                    System.out.println("===============兔子跑到10米的时候,谦让乌龟一下,接着跑=============");
                    //让步进入就绪状态
                    Thread.yield();
                }
                //兔子跑到五十米的时候,再让乌龟1毫秒,接着跑
                if (i == 50) {
                    System.out.println("==============兔子跑到五十米的时候,再让乌龟1毫秒,接着跑=============");
                    Thread.sleep(1);
                }
                //兔子跑到80米的时候,睡了50毫秒,接着跑
                if (i == 80) {
                    System.out.println("==============兔子跑到80米的时候,睡了50毫秒,接着跑===================");
                    Thread.sleep(50);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.编写测试类

/**
 * 测试类——>>用于创建“乌龟”和“兔子”线程并启动
 */
public class Test {
    public static void main(String[] args) {
        Thread thread = new Thread(new Rabbit(), "兔子");
        //设置级别
        thread.setPriority(10);
        //启动线程
        thread.start();


        Thread thread1 = new Thread(new Tortoise(), "乌龟");
        //设置级别
        thread1.setPriority(1);
        //启动线程
        thread1.start();

    }

}

结果:
在这里插入图片描述
在这里插入图片描述

编写多线程应用程序,模拟多个人通过一个山洞的模拟。这个山洞每次只能通过一个人,每个人通过山洞的时间为5秒,随机 生成10个人,同时准备过此山洞,显示一下每次通过山洞人的姓名

public class Person implements Runnable {

    private int person = 10;

    @Override
    public void run() {

        while (true) {
            //调用passPerson方法
            passPerson();
            if (person < 0) {
                break;
            }
        }

    }


    public synchronized void passPerson() {
        if (person > 0) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Random random = new Random();
            System.out.println(Thread.currentThread().getName() + "执行" + random.toString() + "过山洞了" + person--);
        }
    }

}

class test {
    public static void main(String[] args) {

        Person person = new Person();
        Thread thread = new Thread(person, "线程一");
        Thread thread1 = new Thread(person, "线程二");
        Thread thread2 = new Thread(person, "线程三");
        thread.start();
        thread1.start();
        thread2.start();


    }
}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值