一、实验目的:
了解和使用多线程
二、实验环境:
配置java环境的PC
三、实验内容:
1、设有一个银行账户,里面有2000元钱。该账户归tom和jack两个人共同所有。每个人每 次可以取100元钱。编写一个类BankAccount表示银行账户,void withdraw(int count)方法表 示取钱,int getbalance()方法用来获取银行账户余额。创建两个线程分别表示tom和jack两 个人。要求输出取钱的过程,以及每次取钱后的账户余额。 为了更好的模拟多线程的交错运行效果,可以每次取钱之前让线程暂停20毫秒。
运行代码:
package SIXONE;
public class SIXONE implements Runnable{
int money = 2000;
int getMoney() {
money = money -100;
return money;
}
public void run() {
while(this.money>100) {
synchronized (this) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" withdraw 100 balance left "+getMoney());
}
}
public static void main (String []args) {
SIXONE ba = new SIXONE();
Thread t1 = new Thread(ba);
t1.setName("Tom");
Thread t2 = new Thread(ba);
t2.setName("Jack");
t1.start();
t2.start();
}
}
运行截图:
2、计算0~999的和,要求使用多线程的方法,第一个线程计算前500个数的和,第二个线 程计算后500个数的和。然后将两个计算结果相加。第一个线程名为thread1,第二个线程名 为thread2。注意:在计算最终结果之前,必须确保两个线程都已经计算完成。
运行代码:
package SIXTWO;
public class SIXTWO {
public static void main(String[] args) {
int sum;
Thread1 t1s=new Thread1();
Thread2 t2s=new Thread2();
Thread t1=new Thread(t1s);
Thread t2=new Thread(t2s);
t1.setName("thread1");
t2.setName("thread2");
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sum=t1s.get()+t2s.get();
System.out.println("LAST:"+sum);
}
}
class Thread1 implements Runnable{
private int sum1=0;
public void run(){
for(int i=0;i<500;i++)
sum1+=i;
System.out.println("thread1END LAST:"+sum1 );
}
public int get(){
return sum1;
}
}
class Thread2 implements Runnable{
private int sum2=0;
public void run(){
for(int i=500;i<1000;i++)
sum2+=i;
System.out.println("thread2END LAST:"+sum2 );
}
public int get(){
return sum2;
}
}
运行截图:
3、编程模拟售票系统,模拟多个窗口(至少4个)同时出售100张车票的情况;用实现Runnable接口的方法实现多线程。
运行代码:
package SIXTREE;
public class SellTicktDemo implements Runnable
{
//定义票的总数
private int total = 100;
//定义票的编号
private int no = total+1;
//定义一个线程同步对象
private Object obj = new Object();
@Override
public void run() {
while(true){
//同步锁
synchronized(this.obj){
if(this.total > 0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
String msg = Thread.currentThread().getName()+" 售出第 "+(this.no -this.total) +" 张票";
System.out.println(msg);
this.total--;
}else{
System.out.println("票已售完,请下次再来!");
System.exit(0);
}
}
}
}
}
package SIXTREE;
public class SellTicktDemoTest {
public static void main(String[] args) {
//得到对象
SellTicktDemo std = new SellTicktDemo();
//把对象放入线程中
Thread t1 = new Thread(std,"售票窗口1");
Thread t2 = new Thread(std,"售票窗口2");
Thread t3 = new Thread(std,"售票窗口3");
Thread t4 = new Thread(std,"售票窗口4");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行截图:
4、使用多线程同步和互斥方式解决5个哲学家问题。
设置一个’Chopstick’类代表筷子
一个‘Philosopher’类代表哲学家
运行代码:
筷子类:
package SIXFORTH;
public class Chopstick {
public boolean[] isUsing=new boolean[5];
public synchronized void takeChopsticks(int index){
while(isUsing[index]||isUsing[(index+1)%5]){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
isUsing[index]=true;
isUsing[(index+1)%5]=true;
System.out.println("哲学家"+index+"拿起筷子");
}
public synchronized void putChopsticks(int index){
isUsing[index]=false;
isUsing[(index+1)%5]=false;
System.out.println("哲学家"+index+"放下筷子");
notify();
}
}
哲学家类:
package SIXFORTH;
public class Philosopher implements Runnable{
private int index;
public static Chopstick chop=new Chopstick();
public Philosopher(int index) {
this.index = index;
}
public synchronized void thinking(){
System.out.println("哲学家"+Thread.currentThread().getName()+"在思考");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized void eating(){
System.out.println("哲学家"+index+"在吃饭");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
// TODO Auto-generated method stub
while(true){
thinking();
chop.takeChopsticks(index);
eating();
chop.putChopsticks(index);
}
}
}
主类:
package SIXFORTH;
public class TEST {
public static void main(String[] args) {
Philosopher p1=new Philosopher(0);
Philosopher p2=new Philosopher(1);
Philosopher p3=new Philosopher(2);
Philosopher p4=new Philosopher(3);
Philosopher p5=new Philosopher(4);
new Thread(p1,"0").start();
new Thread(p2,"1").start();
new Thread(p3,"2").start();
new Thread(p4,"3").start();
new Thread(p5,"4").start();
}
}
运行截图:
结果符合题意要求
(1)多次重复本文仅供参考学习,全文复制粘贴者后果自负,不提供详细源码文件,欢迎留言讨论;
(2)学会独立思考、辨析,该学好的课认真学,该逃的课就逃,逃课不是为了去耍,而是精修于自己感兴趣的技术技能。
(3)推荐歌曲:温柔–五月天