Java 实验五 多线程编程

都忘了这茬子事了,哈哈。

话不多说直接进入正题,先看实验任务:

1. 阅读下列程序,写出运行结果,然后用Runnable接口编写程序来实现同样功能。

class SimpleThread extends Thread{
    public SimpleThread(String str) {
        super(str);
    }

    public void run(){
        for(int i=0;i<10;i++){
            System.out.println(i+" "+getName());
            try {
                sleep((int)Math.random()*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("DONE!"+getName());
    }
}
public class TwoThreadsTest { 
       public static void main(String[] args) { 
               new SimpleThread("First").start(); 
               new SimpleThread("Second").start();
       } 
}

2. 创建两个线程,一个线程打印“中国”,另一个线程打印“加油”,输出如下类似效果:

中国加油加油中国加油中国中国加油加油中国…。

3. 假设有火车票1000张,创建10个线程模拟10个售票点,每个售票点100毫秒售出一张票。请模拟并打印出售票过程。注意使用synchronized确保同一张票只能卖出一次。输出格式如下:

第4售票点卖出第100张票 第2售票点卖出第101张票……

4. 假设某家银行每接受一次顾客的汇款,便可计算出当前汇款总额。若现有两个顾客,每人分3次、每次100元将钱汇入。试模拟该银行作业,输出格式如下:

顾客甲第1次存入100元,汇款总额100

顾客乙第1次存入100元,汇款总额200

顾客乙第2次存入100元,汇款总额300

……

代码块:

由于线程执行的情况不同,程序运行结果不唯一

1.先看给出代码的运行结果

 再看实现Runnable接口的代码:

public class Test2 {
    public static void main(String[] args) {
        new Thread(new SimpleThread2("First")).start();
        new Thread(new SimpleThread2("Second")).start();
    }
}
class SimpleThread2 implements Runnable{
    private String name;
    public SimpleThread2(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println(i+" "+name);
            try {
                Thread.sleep((int)(Math.random()*1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("DONE!"+name);
    }
}

 关于继承 Thread 与实现 Runnable 接口还是有区别的,想了解的可以去搜索一下哈。

2.

public class Test3 {
    public static void main(String[] args) {
        PrintThread thread1=new PrintThread("中国");
        PrintThread thread2=new PrintThread("加油");
        thread1.start();
        thread2.start();
    }
}
class PrintThread extends Thread{
    public PrintThread(String name) {
        super(name);
    }
    public void run(){
        for(int i=0;i<10;i++){
            System.out.print(getName());
            try {
                Thread.sleep((int) (Math.random() * 1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield(); // 释放CPU资源,让给其他线程执行
        }
    }
}

3. 

public class Test4 {
    public static void main(String[] args) {
        for(int i=1;i<=10;i++){
            new Thread(new SaleTicketThread(i,"售票点")).start();
        }
    }
}
class SaleTicketThread implements Runnable{
    private int id;
    private String name;
    private static int ticket=1;
    static Object obj=new Object();//锁对象  必须唯一

    public SaleTicketThread(int id,String name) {
        this.id=id;
        this.name = name;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (obj) {
                if (ticket<=1000) {
                    try {
                        Thread.sleep(100);
                        System.out.println("第"+id+name+"卖出第"+ticket+"张票");
                        ticket++;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    break;
                }
            }
        }
    }
}

4.

public class Test5 {
    public static void main(String[] args) {
        new Thread(new CountThread("顾客甲")).start();
        new Thread(new CountThread("顾客乙")).start();
    }
}
class CountThread implements Runnable {
    private String name;
    private static int count = 0;
    static Object obj=new Object();

    public CountThread(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for(int i=1;i<=3;i++){
            synchronized (obj) {
                count+=100;
                try {
                    System.out.println( name + "第" + i + "次存入100元,汇款总额"+count);
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

有问题的欢迎评论区留言 

再次声明 由于线程执行的情况不同,程序运行结果不唯一

还有一周考试,王某人也在此也祝愿各位好好复习别挂科

我不知道你们是不是这个状态,哈哈哈哈哈哈哈哈~ 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实验Java多线程 一、实验目的: 熟悉利用Thread类建立多线程方法。 熟悉利用Thread接口建立多线程方法。 二、实验内容: 1. 阅读下列程序,分析并上机检验其功能。 class DelayThread exends Thread{ private static int count=0; private int no; private int delay; public DelayThread(){ count++; no=count; } public void run(){ try{ for (int i=0;i<10;i++){ delay=(int)(Math.random()*5000); sleep(delay); System.out.println(“Thread ”+no+” with a delay ”+delay); } }catch(InterruptedException e){}}} public class MyThread{ public static void main(String args[]){ DelayThread thread1=new DelayThread(); DelayThread thread2=new DelayThread(); thread1.start(); thread2.start(); try{ Thread.sleep(1000);}catch(InterruptedException e){ System.out.println(“Thread wrong”);}}} 2.讲上列程序利用Runnable接口改写,并上机检验。 3.利用多线程编写一个模拟时钟(AWT程序、Runnable接口),有时/分/秒针 编写一个应用程序,创建三个线程分别显示各自的时间。 三、实验要求: 1. 通过实验掌握Thread 、Runnable使用方法; 2. 程序必须能够实现多线程; 3. 程序必须能够完成题目要求; 4. 写出实验报告。 四、实验步骤: 首先分析程序功能,再通过上机运行验证自己的分析,从而掌握通过Thread类建立多线程的方法。 通过将扩展Thread类建立多线程的方法改为利用Runnable接口的方法,掌握通过Runnable接口建立多线程的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惊骇世俗王某人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值