目录
1.卖电影票
package threadtest1;
public class TicketSale extends Thread {
static public int ticket = 1;//剩余电影票
@Override
public void run() {
while (true) {
synchronized (TicketSale.class) {
try {
sleep(3000);//领取时间
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (ticket > 1000) {//票卖完
break;
} else {//票未卖完
System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票");
ticket++;
}
}
}
}
}
package threadtest1;
public class ThreadDemo {
public static void main(String[] args) {
TicketSale ts1=new TicketSale();
TicketSale ts2=new TicketSale();
ts1.setName("窗口1");
ts2.setName("窗口2");
ts1.start();
ts2.start();
}
}
2.送礼品
package threadtest2;
public class GiftGive extends Thread {
static public int gift = 100;//剩余礼品
@Override
public void run() {
while (true) {
synchronized (this.getClass()) {
try {
sleep(3000);//领取时间
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (gift < 10) {//礼物未派送完
break;
} else {//礼物派送完
gift--;
System.out.println(Thread.currentThread().getName() + "送出了" + "一份礼品,还剩"+gift+"份");
}
}
}
}
}
package threadtest2;
public class ThreadDemo {
public static void main(String[] args) {
GiftGive ts1=new GiftGive();
GiftGive ts2=new GiftGive();
ts1.setName("小明");
ts2.setName("小红");
ts1.start();
ts2.start();
}
}
3.打印奇数数字
package threadtest3;
public class NumRun extends Thread {
static public int num = 1;//数
@Override
public void run() {
while (true) {
synchronized (this.getClass()) {
try {
sleep(30);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (num > 100) {//数字算完
break;
} else {//数字未算完
if (num % 2 == 1)//打印奇数
System.out.println(Thread.currentThread().getName() + "输出了奇数" + num);
num++;
}
}
}
}
}
package threadtest3;
public class ThreadDemo {
public static void main(String[] args) {
NumRun ts1=new NumRun();
NumRun ts2=new NumRun();
ts1.setName("线程1");
ts2.setName("线程2");
ts1.start();
ts2.start();
}
}
4.抢红包
package threadtest4;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;
public class MoneyGet extends Thread {
//红包剩余金额
static BigDecimal money = BigDecimal.valueOf(100.0);
//抢红包剩余名额
static int count = 3;
//抢到的最低金额
static final BigDecimal MIN = BigDecimal.valueOf(0.01);
@Override
public void run() {
synchronized (MoneyGet.class) {
if (count == 0) System.out.println(getName() + "没有抢到红包");//抢红包名额用完
else {
BigDecimal prize;
if (count == 1) {//最后一个名额抢走剩余金额
prize = money;
} else {//随机得到范围内金额
double bounds = money.subtract(BigDecimal.valueOf(count - 1).multiply(MIN)).doubleValue();
prize = BigDecimal.valueOf(new Random().nextDouble(bounds));
}
prize = prize.setScale(2, RoundingMode.HALF_UP);//四舍五入
System.out.println(getName() + "抢到了" + prize + "元");
money = money.subtract(prize);
count--;
}
}
}
}
package threadtest4;
public class ThreadDemo {
public static void main(String[] args) {
MoneyGet mg1=new MoneyGet();
MoneyGet mg2=new MoneyGet();
MoneyGet mg3=new MoneyGet();
MoneyGet mg4=new MoneyGet();
MoneyGet mg5=new MoneyGet();
mg1.setName("A");
mg2.setName("B");
mg3.setName("C");
mg4.setName("D");
mg5.setName("E");
mg1.start();
mg2.start();
mg3.start();
mg4.start();
mg5.start();
}
}
5.抽奖箱抽奖
package threadtest5;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class Box extends Thread{
static ArrayList<Integer> list;//所有奖项
public Box(ArrayList<Integer> list){//录入所有奖项
this.list=list;
}
@Override
public void run() {
while(true){
try {
sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (Box.class){
if(list.size()==0)break;//奖项抽完
else {//打乱奖项顺序后抽第一位奖项
Collections.shuffle(list);
int prize=list.remove(0);
System.out.println(getName()+"抽中了"+prize+"元的奖励");
}
}
}
}
}
package threadtest5;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
public class ThreadDemo {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
Collections.addAll(list,10,5,20,50,100,200,500);//录入所有奖项
Box b1=new Box(list);
Box b2=new Box(list);
b1.setName("抽奖箱1");
b2.setName("抽奖箱2");
b1.start();
b2.start();
}
}
6.抽奖箱抽奖(多线程统计并求最大值)
package threadtest6;
import java.util.ArrayList;
import java.util.Collections;
public class Box extends Thread {
static ArrayList<Integer> list;//所有奖项
public Box(ArrayList<Integer> list) {//录入所有奖项
this.list = list;
}
@Override
public void run() {
ArrayList<Integer> boxList = new ArrayList<>();//单一线程中抽到的奖项
while (true) {
try {
sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (Box.class) {
if (list.size() == 0) {//奖项抽完
System.out.println(getName() + "产生了" + boxList.size() + "个奖项" + boxList);
if (boxList.size() != 0)
System.out.println("最大奖项是" + Collections.max(boxList) + "元");
break;
} else {//打乱奖项顺序后抽第一位奖项
Collections.shuffle(list);
int prize = list.remove(0);
boxList.add(prize);
}
}
}
}
}
package threadtest6;
import java.util.ArrayList;
import java.util.Collections;
public class ThreadDemo {
public static void main(String[] args) {
ArrayList<Integer> list=new ArrayList<>();
Collections.addAll(list,10,5,20,50,100,200,500);//录入所有奖项
Box b1=new Box(list);
Box b2=new Box(list);
Box b3=new Box(list);
b1.setName("抽奖箱1");
b2.setName("抽奖箱2");
b3.setName("抽奖箱3");
b1.start();
b2.start();
b3.start();
}
}
7.抽奖箱抽奖(多线程的返回值比较)
package threadtest7;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;
public class Box implements Callable<Integer> {
static ArrayList<Integer> list;//所有奖项
public Box(ArrayList<Integer> list) {
this.list = list;
}//录入所有奖项
@Override
public Integer call() throws InterruptedException {
ArrayList<Integer> boxList=new ArrayList<>();//单一线程中抽到的奖项
while (true) {
Thread.sleep(10);
synchronized (Box.class) {
if (list.size() == 0) {//奖项抽完
System.out.println(Thread.currentThread().getName()+"产生了"+boxList.size()+"个奖项"+boxList);
break;
} else {//打乱奖项顺序后抽第一个奖项
Collections.shuffle(list);
int prize = list.remove(0);
boxList.add(prize);
}
}
}
if(boxList.size()==0)return null;//线程没有抽到奖返回null
else return Collections.max(boxList);//线程抽到奖返回其中最大奖项
}
}
package threadtest7;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class ThreadDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ArrayList<Integer> list=new ArrayList<>();
Collections.addAll(list,10,5,20,50,100,200,500);//录入所有奖项
Box box=new Box(list);
//FutureTask接收线程返回值
FutureTask<Integer> ft1=new FutureTask<>(box);
FutureTask<Integer> ft2=new FutureTask<>(box);
FutureTask<Integer> ft3=new FutureTask<>(box);
Thread t1=new Thread(ft1);
Thread t2=new Thread(ft2);
Thread t3=new Thread(ft3);
t1.setName("抽奖箱1");
t2.setName("抽奖箱2");
t3.setName("抽奖箱3");
t1.start();
t2.start();
t3.start();
//打印最高奖项
Integer max1=ft1.get();
Integer max2=ft2.get();
Integer max3=ft3.get();
System.out.println("最高奖项是"+Math.max(Math.max(max1,max2),max3)+"元");
}
}