首页 > 解决方案 > 在 Java 中使用 Thread 组合计数器和用户输入?

问题描述

我是一名学生,最近正在研究线程。我想做的是实现 MVC 模式来管理诸如开始计数、停止计数、反向计数等功能......

我的最终目标是,我需要在计数器从 1 开始计数时获得用户输入,如果我输入 2(假设选项 2 正在停止计数器),则计数器应该停止计数。

例如:

Counting...
1
2
3
(If I press 2 here)
Counter stopped running!

因为这是我大学的作业,所以我不能在这里上传我实现的代码。

我所做的是,

MVC 模式:

Controller class= 使用 Controller 构造函数获取模型和视图。该类还提供了 service() 方法,该方法使用 switch case 让用户输入以选择选项以运行计数功能(例如) case1: startCounting() case2: stopCounting() 等...)

View class = 使用 System.out.println 和 displayMenu() 函数提供选项。

模型类 = 实现 startCounting()、stopCounting 等功能...

我现在需要为此实现添加线程,以便将用户输入与此计数过程进行交互。

我可以得到任何提示吗?例如,我应该扩展哪个类 Thread 以及我应该以什么方式实现 run() 方法?

骨架代码:

计数控制器类

public class CounterController {
    private Counter model;
    private CounterView view;

    public CounterController(Counter model, CounterView view) {
        this.model = model;
        this.view = view;
    }
}

模型类

public class Counter {
    private int count = 0;
    private boolean counting = false;
    private Integer ceiling = null;
    private Integer floor = null;
    private boolean reverse = false;

    public void startCounting() {
        counting = true;
        while (counting && checkLimits()) {
            try {
                Thread.sleep(1000);
                count = reverse ? count - 1 : count + 1;

                // You should replace this print with something observable so the View can handle it
                System.err.println(count);
            } catch (InterruptedException ignored) {}
        }
    }

    public void stopCounting() {
        counting = false;
    }

    public void setReverse(boolean reverse) {
        this.reverse = reverse;
    }

    public void setCeiling(Integer ceiling) {
        this.ceiling = ceiling;
    }

    public void setFloor(Integer floor) {
        this.floor = floor;
    }

    public int getCount() {
        return count;
    }

    public void resetCount() {
        count = 0;
    }

    private boolean checkLimits() {
        if (null != ceiling && count >= ceiling) {
            return false;
        }
        if (null != floor && count <= floor) {
            return false;
        }

        return true;
    }
}

查看课程

public class CounterView {
    private Counter model;

    public CounterView(Counter model) {

        this.model = model;
    }

    public void launch() {

    }
}

ViewUntil 类

    class ViewUtils {
    static int displayMenu(String header, String[] options, String prompt) {
        System.out.println("\n" + header);

        for (int i = 0; i < options.length; i++) {
            System.out.println((i+1) + ". " + options[i]);
        }

        while (true) {
            Integer response = getInt(prompt, true);

            int selection = response != null ? response : -1;

            if (selection > 0 && selection <= options.length) {
                return selection;
            } else {
                System.out.println("Invalid menu selection");
            }
        }
    }

    static String getString(String prompt, boolean allowBlank) {
        Scanner s = new Scanner(System.in);

        String response;
        do {
            System.out.println(prompt);
            response = s.nextLine();

            if (!allowBlank && "".equals(response)) {
                response = null;
                System.out.println("Blank entry is not allowed here.");
            }
        } while (null == response);

        return response;
    }

    static Integer getInt(String prompt, boolean allowBlank) {

        int response;
        do {
            String str = getString(prompt, allowBlank);
            if ("".equals(str)) {
                return null;
            }
            try {
                response = Integer.parseInt(str);
                return response;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input - number required");
            }

        } while (true);
    }

    static Boolean getBoolean(String prompt, boolean allowBlank) {
        prompt = prompt + "(y/n) ";
        Boolean response;
        do {
            String str = getString(prompt, allowBlank);
            if ("".equals(str)) {
                return null;
            }

            if ("y".equals(str.toLowerCase())) {
                return true;
            }

            if ("n".equals((str.toLowerCase()))) {
                return false;
            }

            System.out.println("Invalid input - must be y or n");

        } while (true);
    }
}

主班

public class MainDriver {
    public static void main(String[] args) {
        Counter model  = new Counter();
        CounterView view = new CounterView(model);
        CounterController controller = new CounterController(model, view);

        controller.service();
    }
}

标签: javamultithreadingmodel-view-controller

解决方案


使用“volatile”强制线程计数器检查内存中的最新设置值,而不是其“缓存”。

public class Counter {
    private int count = 0;
    private volatile boolean counting = false;
    private volatile Integer ceiling = null;
    private volatile Integer floor = null;
    private boolean reverse = false; 
    ...
}

推荐阅读