不使用Thread或ExecutorService的Java并发

问题描述 投票:0回答:1

有一项任务交给我去做,我已尽力但无法完成。任务是编写一个 Java 程序来生产煎饼,同时随机选择用户来吃生产的煎饼。这两件事必须同时进行,也就是说,在煎饼制作的过程中,用户会检查是否有可用的煎饼制作出来,然后吃掉。每个用户最多可以吃 5 个煎饼,煎饼的生产者预计在给定时间内最多生产 12 个煎饼。整个事情将在 20 秒内发生,然后程序应在 20 秒后终止并打印出报告。指示还指出我应该使这两件事同时运行,但不要使用 Thread 或 ExecutorService。我真的需要一些关于如何实现它的帮助。这是我尝试过的方法,并且在 ProducePancake 方法上出现无限循环,它甚至不允许 eatPancake 方法执行。

public class PanCakeTask {

    int consumedPancake = 0;
    int availablePancake = 0;
    int totalPancakeProduced = 0;
    List<String> usersList = new ArrayList<>();

     void producePancake(HashMap<String,Integer>users){
        // first check the total pancake produced so far by checking the number
        // of pancakes eaten by the users plus the available ones

        for (String user: users.keySet()){
            consumedPancake +=users.get(user);
        }
         totalPancakeProduced += consumedPancake + availablePancake;

        while (totalPancakeProduced <12){
            //only continue with the production of pancakes if the number of pancakes
            //produced has not been exhausted (i.e 15)

            System.out.println("Pancake produced");
            //increment the number of pancakes available
            availablePancake +=1;

        }
    }
     void eatPancake(HashMap<String,Integer>users){
        //first pick the user at random
        Collections.shuffle(usersList);

        int numberEaten = users.get(usersList.get(0));
        //get the user that is on index zero after the list has been shuffled
        //then check how many pancake that the picked user has eaten
        //because each user is expected to eat a maximum of 5 pancake

        if (numberEaten ==5){
            //This means that this user has exhausted his/her quota so he/she cannot
            //be allowed to eat again
            //In this case, the eatPancake method is called again to randomly pick another user
            //I am thinking of removing the user from the list here so that he/she cannot be picked again
            eatPancake(users);
        }else {
            //Meaning that the user still has some quota left for him/her to eat
            //In this other condition, it will check if there is an available pancake produced

            if (availablePancake >=1){
                //user will now eat the pancake
                //then increment the number of pancake eaten by that user
                // and decrement the number of pancakes available

                int newCount = numberEaten + 1;
                availablePancake -=1;

                //finally update the user that just eaten the pancake
                users.put(usersList.get(0),newCount);
                System.out.println("Eaten a pancake");
            }
        }
    }
    public void performTask(){

        //Hashmap is used to store the users because of key-value pairs
        HashMap <String,Integer> users = new HashMap<>();
        users.put("user1",0);
        users.put("user2",0);
        users.put("user3",0);

        usersList.addAll(users.keySet());

        System.out.println(users);
        System.out.println(usersList);


        //This task can only be executed in 20 seconds

        long startTimeInSeconds = System.currentTimeMillis()/1000;
        long requiredTime = startTimeInSeconds +20;

        while (startTimeInSeconds !=requiredTime){
            //only continue the process if it has not exceeded 20 seconds
            System.out.println("Start time is "+startTimeInSeconds);
            System.out.println("End time is "+requiredTime);

            //This two method is to be executed concurrently without using Thread or ExecutorService
            // I need help on how to achieve that without the use of Thread or ExecutorService
            producePancake(users);
            eatPancake(users);
            startTimeInSeconds = System.currentTimeMillis()/1000;

        }
        System.out.println("Total number of cake produced is "+totalPancakeProduced);
        System.out.println("Total number of cake eaten is "+consumedPancake);
        System.out.println("Total number of pancake remained is "+availablePancake);
        System.out.println("Time start is "+startTimeInSeconds);
        System.out.println("Time completed is "+requiredTime);
    }
}
java concurrency
1个回答
0
投票

线程并不是真正并行的,除非运行在同一 JVM 中的不同 CPU 上。 不是真正并行意味着线程可能需要在同一个 CPU 上执行几轮,使其看起来像是并行的。

你不能利用Java的线程模型,因此你必须想出类似的东西:

  • 创建一种方法来创建煎饼/资源(或仅一步)
  • 创建一种将消耗煎饼/资源的方法(或仅一个步骤)
  • 创建一个循环,重复调用上述方法,直到定义20秒后退出
© www.soinside.com 2019 - 2024. All rights reserved.