Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法

前面讲解了关于多线程的使用方法,这篇文章则是进行实战,做几道测试题。

感兴趣的情况下可以看一下Java多线程

多线程练习1 (卖电影票) 


一共有1000张电影票,可以在两个窗口领取,假设每次领取的时间为3000毫秒要求:请用多线程模拟卖票过程并打印剩余电影票的数量

线程代码:

package Exercise.Exam1;

public class Mythread extends Thread{
    static int count = 1;

    @Override
    public void run() {
        while (true){
            synchronized (Mythread.class){
                if (count<=1000){
                    try {
                        Thread.sleep(3000);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName()+"正在卖第"+count+"张票");
                    count++;
                }else
                    break;
            }
        }
    }
}

测试类:

package Exercise.Exam1;

public class Test {
    public static void main(String[] args) {
        Mythread mythread1 = new Mythread();
        Mythread mythread2 = new Mythread();

        mythread1.setName("窗口一");
        mythread2.setName("窗口二");

        mythread1.start();
        mythread2.start();
    }
}

运行结果:

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第1张图片

多线程练习2 (送礼品) 


有100份礼品,两人同时发送,当剩下的礼品小于10份的时候则不再送出。
利用多线程模拟该过程并将线程的名字和礼物的剩余数量打印出来. 

线程代码:

package Exercise.Exam2;

public class MyThread extends Thread{
    public MyThread(String name) {
        super(name);
    }
    static int count =1;
    @Override
    public void run() {
        while (true) {
            synchronized (MyThread.class){
                if(count==91){
                    break;
                }
                else {

                    System.out.println(getName()+"送出的第"+count+"份礼物");
                    int sum = 100-count;
                    System.out.println("礼物还剩下" +sum +"份礼物");
                    count++;
                    if (sum==10){
                        System.out.println("最后的十份礼物");
                    }
                }
            }
        }
    }
}

测试类:

package Exercise.Exam2;

public class Test {
    public static void main(String[] args) {
        MyThread thread = new MyThread("马");
        MyThread thread1 = new MyThread("安");

        thread.start();
        thread1.start();

    }
}

运行结果:

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第2张图片

多线程练习3 (打印奇数数字) 


同时开启两个线程,共同获取1-100之间的所有数字要求:将输出所有的奇数。

线程代码

package Exercise.Exam3;

public class Mythread extends Thread{
    private static Object object  = new Object();
    static int num =1;
    @Override
    public void run() {
        while (true){
            synchronized (object){
                object.notifyAll();
                if (num<=100){
                    int ji = num%2;
                    if (ji!=0){
                        System.out.println(num);

                    }
                    num++;
                }else
                    break;
            }
        }
    }

}

测试类

package Exercise.Exam3;

public class Test {
    public static void main(String[] args) {

        Mythread mythread = new Mythread();
        Mythread mythread2 = new Mythread();

        mythread.start();
        mythread2.start();
    }
}

运行结果

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第3张图片

多线程练习4(抢红包)


抢红包也用到了多线程。
假设:100块,分成了3个包,现在有5个人去抢
其中,红包是共享数据。
5个人是5条线程。
打印结果如下:
XXX抢到了XXX元
XXX抢到了XXX元
XXX抢到了XXX元
XXX没抢到
XXX没抢到

线程代码:

package Exercise.Exam4;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Mythread implements Runnable {
    static double sum = 100.00;
    static double num = 0.00;
    static int count = 1;
    @Override
    public void run() {
        while (true){
            synchronized (Mythread.class){
                if(count<3) {
                    double random = Math.random() * sum;
                    BigDecimal a = new BigDecimal(random);
                    BigDecimal bigDecimal = a.setScale(2, RoundingMode.FLOOR);
                    double money = bigDecimal.doubleValue();
                    System.out.println(Thread.currentThread().getName() + "获得了" + money + "元");
                    num = num+money;
                    sum = 100.00-num;
                }
                else if(count==3){
                    double m = 100.00;
                    double three = m-num;
                    BigDecimal a = new BigDecimal(three);
                    BigDecimal bigDecimal = a.setScale(2, RoundingMode.FLOOR);
                    System.out.println(Thread.currentThread().getName()+"获得了" + bigDecimal + "元");
                }
                else if (count>=4&&count<=5) {
                    System.out.println(Thread.currentThread().getName()+"抱歉,您没有抢到红包");
                }
                else {
                    break;
                }

                count++;
                Thread.currentThread().interrupt();
                break;

            }
        }
    }
}

测试类:

package Exercise.Exam4;


public class Testr {
    public static void main(String[] args)  {
        Mythread mythread = new Mythread();
        Thread thread1 = new Thread(mythread);
        Thread thread2 = new Thread(mythread);
        Thread thread3 = new Thread(mythread);
        Thread thread4 = new Thread(mythread);
        Thread thread5 = new Thread(mythread);

        thread1.setName("马");
        thread2.setName("安");
        thread3.setName("安");
        thread4.setName("好");
        thread5.setName("帅");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();

    }
}

 运行结果:

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第4张图片

多线程练习5 (抽奖箱抽奖)


有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池中的奖项为(10,5,20350,100,200,500,800,2,80,300,700;创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”“抽奖箱2"随机从抽奖池中获取奖项元素并打印在控制台上,格式如下:
每次抽出一个奖项就打印一个(随机)抽奖箱1 又产生了一个10 元大奖
抽奖箱1又产生了一个100 元大奖
抽奖箱1 又产生了一个200 元大奖
抽奖箱1又产生了一个800 元大奖抽奖箱2又产生了一个 700 元大奖

线程代码:

package Exercise.Exam5;


import java.util.ArrayList;
import java.util.Collections;

public class Mythread implements Runnable {
    ArrayList list;

    public Mythread(ArrayList list) {
        this.list = list;
    }
    @Override
    public void run() {
        while (true){
            synchronized (Mythread.class){
                if (list.size() ==0){
                    System.out.println("奖项已经被抽完了>>>>>");
                    break;
                }
                else{
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    System.out.println(Thread.currentThread().getName()+"出了"+prize+"元大奖");
                }
            }
        }
    }
}

测试类:

package Exercise.Exam5;

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        ArrayList list  = new ArrayList<>();
        Collections.addAll(list,10,20,3,100,40,55,110,60,500,1000);
        Mythread mythread = new Mythread(list);
        Thread thread =new Thread(mythread);
        Thread thread2 =new Thread(mythread);
        thread.setName("抽奖箱1");
        thread2.setName("抽奖箱2");

        thread.start();
        thread2.start();
    }
}

运行结果:

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第5张图片 

多线程练习6 (多线程统计并求最大值)


在上一题基础上继续完成如下需求
每次抽的过程中,不打印,抽完时一次性打印(随机)在此次抽奖过程中,抽奖箱1总共产生了6个奖项
分别为: 10,20,100,500,2,300最高奖项为300元,总计额为932元在此次抽奖过程中,抽奖箱2总共产生了6个奖项。
分别为:5,50,200,800,80,700最高奖项为800元,总计额为1835元

线程代码:

package Exercise.Exam6;


import java.util.ArrayList;
import java.util.Collections;

public class Mythread implements Runnable {

    ArrayList list;
    public Mythread(ArrayList list) {
        this.list = list;
    }
    @Override
    public void run() {
        ArrayList list1 = new ArrayList<>();
        int sum1=0;
        int Max = 0;
        while (true){
            synchronized (Mythread.class){
                if (list.size() ==0){
                        System.out.println(Thread.currentThread().getName()+list1+"共有"+list1.size()+"项"+"共"+sum1+"最大值为:"+Max);
                    break;
                }
                else{
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    if(prize>Max){
                        Max=prize;
                    }
                    list1.add(prize);
                    sum1 =sum1+prize;
                   }
                }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }

        }
    }


测试类:

package Exercise.Exam6;

import java.util.ArrayList;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        ArrayList list  = new ArrayList<>();
        Collections.addAll(list,10,20,3,100,40,55,110,60,500,1000);
        Mythread mythread = new Mythread(list);
        Thread thread =new Thread(mythread);
        Thread thread2 =new Thread(mythread);
        thread.setName("抽奖箱1");
        thread2.setName("抽奖箱2");

        thread.start();
        thread2.start();
    }
}

运行结果:

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第6张图片 

多线程练习7(多线程之间的比较)


在上一题基础上继续完成如下需求:
在此次抽奖过程中,抽奖箱1总共产生了6个奖项,分别为:10,20,100,500,2,300
最高奖项为300元,总计额为932元在此次抽奖过程中,抽奖箱2总共产生了6个奖项,分别为:5,50,200,800,80,700最高奖项为800元,总计额为1835元在此次抽奖过程中,抽奖箱2中产生了最大奖项,该奖项金额为800元
以上打印效果只是数据模拟,实际代码运行的效果会有差异

线程代码:

package Exercise.Exam7;

import Exercise.Exam6.Mythread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

public class MyThread implements Callable {
    ArrayList list;

    public MyThread(ArrayList list) {
        this.list = list;
    }

    @Override
    public Integer call() throws Exception {
        ArrayList list1 = new ArrayList<>();
        int sum1=0;
        int Max = 0;
        while (true){
            synchronized (Mythread.class){
                if (list.size() ==0){
                    System.out.println(Thread.currentThread().getName()+list1+"共有"+list1.size()+"项"+"共"+sum1+"最大值为:"+Max);
                    break;
                }
                else{
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    if(prize>Max){
                        Max=prize;
                    }
                    list1.add(prize);
                    sum1 =sum1+prize;
                }
            }
                Thread.sleep(10);
        } if (list1.size()==0){
            return null;
        }
        else
            return Collections.max(list1);
    }
}

测试类:

package Exercise.Exam7;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.FutureTask;

public class Test {
    public static void main(String[] args) throws Exception {
        ArrayList list  = new ArrayList<>();
        Collections.addAll(list,10,20,3,100,40,55,110,60,500,1000);
        MyThread mythread = new MyThread(list);

        FutureTask futureTask = new FutureTask<>(mythread);
        FutureTask futureTask2 = new FutureTask<>(mythread);

        Thread thread = new Thread(futureTask);
        Thread thread2 = new Thread(futureTask2
        );

        thread.start();
        thread2.start();

        Integer integer = futureTask.get();
        Integer integer1 = futureTask2.get();
        if (integer1>integer){
            System.out.println("2最大奖项"+integer1);
        }
        else
            System.out.println("2最大奖项"+integer);
    }
}

运行结果:

Java——关于实现多线程的测试小题,帮助我们更好的理解多线程的使用方法_第7张图片

你可能感兴趣的:(Java面试,JAVA,开发语言,Java,后端,多线程)