Java-多线程实验
Posted 康小庄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java-多线程实验相关的知识,希望对你有一定的参考价值。
1.模拟一个简单的银行系统,使用两个不同的线程向同一个账户存钱。账户的初始余额是1000元,两个线程每次存储100元,分别各存储1000元,不允许出现错误数据。
BankCard
public class BankCard
// 余额
private int balance;
public BankCard(int balance)
super();
this.balance = balance;
public BankCard()
public void setBalance(int balance)
this.balance = balance;
public int getBalance()
return balance;
SaveMoney1
public class SaveMoney1 implements Runnable
private BankCard card;
public SaveMoney1(BankCard card)
super();
this.card = card;
@Override
public void run()
card.setBalance(card.getBalance() + 100);
System.out.println(Thread.currentThread().getName() + "存了100元,余额是:" + card.getBalance());
SaveMoney2
public class SaveMoney2 implements Runnable
private BankCard card;
public SaveMoney2(BankCard card)
super();
this.card = card;
@Override
public void run()
card.setBalance(card.getBalance() + 1000);
System.out.println(Thread.currentThread().getName() + "存了1000元,余额是:" + card.getBalance());
BankMain
public class BankMain
public static void main(String[] args)
// 1.创建银行卡对象
BankCard card = new BankCard();
// 2.存钱功能
SaveMoney1 save1 = new SaveMoney1(card);
SaveMoney2 save2 = new SaveMoney2(card);
Thread a = new Thread(save1, "小康");
Thread b = new Thread(save2, "小庄");
a.start();
try
Thread.sleep(5);
catch (InterruptedException e)
e.printStackTrace();
b.start();
2.某房产公司大促销,所有购房者可以参加一次抽奖,抽奖箱中总共有10个奖品,分别是:“苹果 手机”,“华为手机”,“三洋踏板摩托”,“迪拜7日游”,“苹果笔记本”,“联想笔记本”,“小米空气净化器”,“格力空调”,“海尔冰箱”,"海信电视"所有抽奖者分成两组进行抽奖,请创建两个线程,名称分别为“第一组”和“第二组”,随机从抽奖箱中完成抽奖程序运行效果如下图:要求轮抽取,平均分配抽奖次数,每组5次,使用Lock锁实现。
ThreadMain
public class ThreadMain
public static void main(String[] args)
ArrayList<String> list = new ArrayList<String>();
list.add("苹果");
list.add("手机");
list.add("华为手机");
list.add("三洋踏板摩托");
list.add("迪拜7日游");
list.add("苹果笔记本");
list.add("联想笔记本");
list.add("小米空气净化器");
list.add("格力空调");
list.add("海尔冰箱");
list.add("海信电视");
// 创建锁对象
Lock lock = new ReentrantLock();
// 创建线程A,开启线程
new Thread(new Runnable()
@Override
public void run()
while (!list.isEmpty())
Collections.shuffle(list);
String award = list.get(0);
list.remove(award);
System.out.println("第一组...抽出了一个:" + award);
lock.lock();
// 线程A和线程B交换执行关键所在
try
Thread.sleep(2000);
catch (InterruptedException e)
e.printStackTrace();
finally
lock.unlock();
, "A").start();
// 创建线程B
new Thread(new Runnable()
@Override
public void run()
while (!list.isEmpty())
Collections.shuffle(list);
String award = list.get(0);
list.remove(award);
System.out.println("第二组...抽出了一个:" + award);
lock.lock();
// 线程A和线程B交换执行关键所在
try
Thread.sleep(1000);
catch (InterruptedException e)
e.printStackTrace();
finally
lock.unlock();
, "B").start();
3.编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束。数字和字母之间用空格隔开,要求两个线程轮流交替打印
public class PrintMain
public static void main(String[] args)
// 创建锁对象
final Object object = new Object();
// 创建线程A,开启线程
new Thread(new Runnable()
@Override
public void run()
for (int i = 1; i <= 52; i++)
synchronized (object)
try
System.out.println(i);
// 进入等待状态,并释放锁
object.wait();// 无线等待状态
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
, "A").start();
// 创建线程B
new Thread(new Runnable()
@Override
public void run()
int count = 0;
while (count < 52)
for (char i = 'A'; i <= 'Z'; i++)
synchronized (object)
System.out.println(i);
// 唤醒线程A
object.notify();
count++;
// 线程A和线程B交换执行关键所在
try
Thread.sleep(10);
catch (InterruptedException e)
e.printStackTrace();
, "B").start();
以上是关于Java-多线程实验的主要内容,如果未能解决你的问题,请参考以下文章