欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Java 多线程练习2 (抽奖比较Runnable写法)

Java 多线程练习2 (抽奖比较Runnable写法)

2024/10/25 9:23:37 来源:https://blog.csdn.net/Aishangyuwen/article/details/141202223  浏览:    关键词:Java 多线程练习2 (抽奖比较Runnable写法)

        MultiProcessingExercise2

package MultiProcessingExercise120240814;import java.util.ArrayList;
import java.util.Collections;public class MultiProcessingExercise1 {public static void main(String[] args) {// 需求:// 在此次抽奖过程中,抽奖箱1总共产生了6个奖项,分别为:10,20,100,500,2,300// 最高奖项为300元,总计额为932元// 在此次抽奖过程中,抽奖箱2总共产生了6个奖项,分别为:5,50,200,800,80,700// 最高奖项为800元,总计额为1835元// 在此次抽奖过程中,抽奖箱2中产生了最大奖项,该奖项金额为800元ArrayList<Integer> pool = new ArrayList<>();for (int i = 0; i < 1000; i++) {pool.add(0);}Collections.addAll(pool, 10,5,20,50,100,200,500,800,2,80,300,700);Lottery lottery = new Lottery(pool);Thread thread1 = new Thread(lottery, "抽奖箱1");Thread thread2 = new Thread(lottery, "抽奖箱2");thread1.start();thread2.start();}
}

        Lottery

package MultiProcessingExercise120240814;import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.locks.ReentrantLock;public class Lottery implements Runnable{// 创建奖池private ArrayList<Integer> pool;// 构造方法初始化奖池public Lottery(ArrayList<Integer> pool) {this.pool = pool;}// 创建两个抽奖箱便于统计两个抽奖箱的奖项private ArrayList<Integer> list1 = new ArrayList<>();private ArrayList<Integer> list2 = new ArrayList<>();// 创建锁private final ReentrantLock lock = new ReentrantLock();@Overridepublic void run() {while (true) {lock.lock();try {if (pool.isEmpty()) {// 此时已经奖池已经空了,所以说抽奖完成,可以输出了int maxAward1 = Collections.max(list1);int maxAward2 = Collections.max(list2);if ("抽奖箱1".equals(Thread.currentThread().getName())) {System.out.println(Thread.currentThread().getName() + "中的最大奖是" + maxAward1);System.out.println(Thread.currentThread().getName() + ": " + list1);} else {System.out.println(Thread.currentThread().getName() + "中的最大奖是" + maxAward2);System.out.println(Thread.currentThread().getName() + ": " + list2);}if (maxAward1 > maxAward2) {System.out.println("抽奖箱1有更大的奖项————" + maxAward1);} else {System.out.println("抽奖箱2有更大的奖项————" + maxAward2);}break;} else {// 此时奖池还没空,继续抽奖try {Thread.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}Collections.shuffle(pool);int prize = pool.removeFirst();if (prize != 0) {if (Thread.currentThread().getName().equals("抽奖箱1")) {list1.add(prize);} else {list2.add(prize);}}}} finally {lock.unlock();}}}
}//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.concurrent.locks.ReentrantLock;
//
//public class Lottery implements Runnable {
//    private ArrayList<Integer> pool;
//    private ArrayList<Integer> list1 = new ArrayList<>();
//    private ArrayList<Integer> list2 = new ArrayList<>();
//    private final ReentrantLock lock = new ReentrantLock();
//
//    public Lottery(ArrayList<Integer> pool) {
//        this.pool = pool;
//    }
//
//    @Override
//    public void run() {
//        while (true) {
//            lock.lock();
//            try {
//                if (pool.isEmpty()) {
//                    break;
//                }
//                Collections.shuffle(pool);
//                int prize = pool.removeFirst();
//                if (Thread.currentThread().getName().equals("抽奖箱1")) {
//                    list1.add(prize);
//                } else {
//                    list2.add(prize);
//                }
//            } finally {
//                lock.unlock();
//            }
//            try {
//                Thread.sleep(10);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//
//        lock.lock();
//        try {
//            int maxAward1 = list1.isEmpty() ? 0 : Collections.max(list1);
//            int maxAward2 = list2.isEmpty() ? 0 : Collections.max(list2);
//            if ("抽奖箱1".equals(Thread.currentThread().getName())) {
//                System.out.println(Thread.currentThread().getName() + "中的最大奖是" + maxAward1);
//                System.out.println(Thread.currentThread().getName() + ": " + list1);
//            } else {
//                System.out.println(Thread.currentThread().getName() + "中的最大奖是" + maxAward2);
//                System.out.println(Thread.currentThread().getName() + ": " + list2);
//            }
//            if (maxAward1 > maxAward2) {
//                System.out.println("抽奖箱1有更大的奖项————" + maxAward1);
//            } else {
//                System.out.println("抽奖箱2有更大的奖项————" + maxAward2);
//            }
//        } finally {
//            lock.unlock();
//        }
//    }
//}

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com