目录
模拟多个线程实现取钱操作:
1.创建Account类
2.创建自定义线程类实现取款操作
3.创建多个线程实现取款,观察数据的变化
Account类
public class Account {
private int deposit;//存款
public Account() {
}
public Account(int deposit) {
this.deposit = deposit;
}
public int getDeposit() {
return deposit;
}
public void setDeposit(int deposit) {
this.deposit = deposit;
}
public void withdraw(int num) {//取款
synchronized (this) {
if (this.deposit < num) {
System.out.println("存款不够");
System.exit(0);
} else {
//获取存款
int before = this.deposit;
//模拟取钱
int after = before - num;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
//修改存款
this.deposit = after;
System.out.println(Thread.currentThread().getName() + "取走" + num + "元,余额为:" + after);
}
}
}
}
AccountThread类
public class AccountThread extends Thread{
private Account account;
public AccountThread(Account account) {
this.account = account;
}
@Override
public void run() {
while(true) {
account.withdraw(100);
}
}
}
Test类
public class Test {
public static void main(String[] args) {
//初始化
Account account = new Account(1000);
Thread thread1 = new AccountThread(account);
thread1.setName("取款人1");
thread1.start();
Thread thread2 = new AccountThread(account);
thread2.setName("取款人2");
thread2.start();
}
}
运行结果:
取款人1取走100元,余额为:900
取款人1取走100元,余额为:800
取款人1取走100元,余额为:700
取款人1取走100元,余额为:600
取款人1取走100元,余额为:500
取款人1取走100元,余额为:400
取款人1取走100元,余额为:300
取款人2取走100元,余额为:200
取款人2取走100元,余额为:100
取款人1取走100元,余额为:0
存款不够
模拟多个线程实现售票操作:
SellTicket类
public class SellTicket {
private int ticket;
public SellTicket() {
}
public SellTicket(int ticket) {
this.ticket = ticket;
}
public int getTicket() {
return ticket;
}
public void setTicket(int ticket) {
this.ticket = ticket;
}
public void sellTicket() {
synchronized (this) {
if (ticket == 0) {
System.out.println("没票了");
System.exit(0);
} else {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
//一次卖一张票
System.out.println(Thread.currentThread().getName() + "卖出一张票,余票为:" + ticket--);
}
}
}
}
TicketThread类
public class TicketThread extends Thread{
private SellTicket sellTicket;
public TicketThread(SellTicket sellTicket) {
this.sellTicket = sellTicket;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
sellTicket.sellTicket();
}
}
}
Test类
public class Test {
public static void main(String[] args) {
SellTicket sellTicket = new SellTicket(20);
Thread thread1= new TicketThread(sellTicket);
Thread thread2= new TicketThread(sellTicket);
Thread thread3= new TicketThread(sellTicket);
thread1.start();
thread2.start();
thread3.start();
}
}
运行结果:
Thread-0卖出一张票,余票为:20
Thread-0卖出一张票,余票为:19
Thread-0卖出一张票,余票为:18
Thread-2卖出一张票,余票为:17
Thread-2卖出一张票,余票为:16
Thread-2卖出一张票,余票为:15
Thread-2卖出一张票,余票为:14
Thread-2卖出一张票,余票为:13
Thread-2卖出一张票,余票为:12
Thread-2卖出一张票,余票为:11
Thread-2卖出一张票,余票为:10
Thread-2卖出一张票,余票为:9
Thread-2卖出一张票,余票为:8
Thread-1卖出一张票,余票为:7
Thread-1卖出一张票,余票为:6
Thread-1卖出一张票,余票为:5
Thread-1卖出一张票,余票为:4
Thread-1卖出一张票,余票为:3
Thread-1卖出一张票,余票为:2
Thread-1卖出一张票,余票为:1
没票了
模拟多个线程实现强抢红包操作:
RedPacket类
import java.util.Random;
public class RedPacket implements Runnable{
/**
* 抢红包:金额:money, 数量: count
* 每轮次抢的金额 ( money元*100 )分
* 金额随机数(范围)1-money*100-(count-1)*MIN
* 最少钱数:MIN
*/
private int money;//红包金额
private int count;//红包数量
private final int MIN = 1;//抢到的最少金额,1分
public RedPacket() {
}
public RedPacket(int money, int count) {
this.money = money*100;
this.count = count;
}
@Override
public void run() {
synchronized (this) {
if (count == 0) {
System.out.println(Thread.currentThread().getName()+"手速过慢,红包已经没了");
System.exit(0);
} else {
int num = 0;
if (count == 1) {
num = this.money;//最后一次的红包为剩余的钱数
}else{
Random random = new Random();
num = random.nextInt(this.money-(this.count-1)*MIN) + MIN; //每轮随机的钱数
}
System.out.println(Thread.currentThread().getName() + "抢到了" + num/100.0 + "元");
this.money -= num;
this.count--;
}
}
}
}
Test类
public class Test {
public static void main(String[] args) {
RedPacket redPacket = new RedPacket(10,5);
while(true){
new Thread(redPacket).start();
}
}
}
运行结果:
Thread-0抢到了3.12元
Thread-8抢到了3.4元
Thread-28抢到了1.82元
Thread-7抢到了0.39元
Thread-20抢到了1.27元
Thread-4手速过慢,红包已经没了
生产者-消费者模式:
Stock类:
public class Stock {
private int count;//最大容量10
public synchronized void push(){
if(count<10){
count++;
this.notifyAll();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+"正在生产"+count);
}else{
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println("仓库已满,停止生产");
}
}
public synchronized void pop(){
if(count>0){
count--;
this.notifyAll();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println(Thread.currentThread().getName()+"正在消费,库存还有"+count);
}else{
try {
this.wait();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
System.out.println("仓库已空,继续生产");
}
}
}
Produce类:
public class Produce extends Thread{
private Stock stock;
public Produce(Stock stock) {
this.stock = stock;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
stock.push();
}
}
}
Consumer类:
public class Consumer extends Thread{
private Stock stock;
public Consumer(Stock stock) {
this.stock = stock;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
stock.pop();
}
}
}
Test类:
public class Test1 {
public static void main(String[] args) {
Stock stock = new Stock();
Produce thread1 = new Produce(stock);
Consumer thread2 = new Consumer(stock);
thread1.start();
thread2.start();
}
}
运行结果:
Thread-0正在生产1
Thread-0正在生产2
Thread-0正在生产3
Thread-0正在生产4
Thread-0正在生产5
Thread-0正在生产6
Thread-0正在生产7
Thread-0正在生产8
Thread-0正在生产9
Thread-1正在消费,库存还有8
Thread-0正在生产9
Thread-1正在消费,库存还有8
Thread-1正在消费,库存还有7
Thread-1正在消费,库存还有6
Thread-1正在消费,库存还有5
Thread-1正在消费,库存还有4
Thread-1正在消费,库存还有3
Thread-1正在消费,库存还有2
Thread-1正在消费,库存还有1
Thread-1正在消费,库存还有0
生产者-消费者模式(阻塞队列):
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Test1 {
public static void main(String[] args) {
BlockingQueue queue = new ArrayBlockingQueue(10);
Thread produce = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
queue.put(i);
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+"正在生产"+i);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
});
produce.start();
Thread consumer = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
try {
Object value = queue.take();
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+"正在消费"+value);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
});
consumer.start();
}
}
运行结果:
Thread-0正在生产0
Thread-1正在消费0
Thread-0正在生产1
Thread-1正在消费1
Thread-0正在生产2
Thread-0正在生产3
Thread-1正在消费2
Thread-0正在生产4
Thread-1正在消费3
Thread-0正在生产5
Thread-0正在生产6
Thread-1正在消费4
Thread-0正在生产7
Thread-1正在消费5
Thread-0正在生产8
Thread-0正在生产9
Thread-1正在消费6
Thread-1正在消费7
Thread-1正在消费8
Thread-1正在消费9