如何添加另一个控制台与多线程程序交互 [英] how to add another console to interact with multi-threaded program

查看:61
本文介绍了如何添加另一个控制台与多线程程序交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单线程程序在做一些长时间运行的计算工作.主线程正在使用控制台进行一些输出.我正在考虑添加另一个线程与用户交互以查询诸如作业进度或进程内部状态之类的内容.

I have a single-threaded program doing some long-running computing job. The main thread is using the console for some output. I'm considering adding another thread to interact with user to query something like the job progress or process internal state.

假设同步处理得当,并且用户通过命令行控制台(不需要 GUI)之类的东西与第二个线程交互.实现此功能的好方法是什么?我想我可以让第二个线程在某个命名管道上等待命令,并让用户使用 GNU 屏幕之类的东西连接到这个管道(我不确定它是否会工作).

Assuming synchronization is properly handled and the user interact with the second thread through something like a command line console (no GUI needed). What is a good way to implement this function? I guess I can make the second thread wait for commands at certain named pipe and let the user connect to this pipe using something like a GNU screen (I'm not sure whether it will work).

是否有任何库(Java 或非 Java)或工具来实现这一点?我的程序在 Linux 上运行.

Are there any libraries (Java or non-Java) or tools to implement this? My program is running on Linux.

推荐答案

您需要另一个线程来读取用户的输入.这是我将如何做...

You need another thread to read the input from the user. Here's how I would do it...

package com.mycompany.readthread;

import java.io.*;

public class Launcher {
    public static void main(String[] args) {
        new Launcher().run();
    }

    public void run() {
        LongRunningJob job = new LongRunningJob();
        ReadInputLoop readInputLoop = new ReadInputLoop(job);
        Thread readInputLoopThread = new Thread(readInputLoop);
        readInputLoopThread.start();  // run read input loop in another thread
        job.run();  // run long running job in this thread
    }

    class LongRunningJob implements Runnable {
        private double percentageComplete;

        @Override
        public void run() {
            // long running job
            // update percentageComplete in a synchronized block
        }

        public synchronized double getPercentageComplete() {
            return percentageComplete;
        }
    }

    class ReadInputLoop implements Runnable {
        private LongRunningJob job;               

        public ReadInputLoop(LongRunningJob job) {
            this.job = job;            
        }

        @Override
        public void run() {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                while (true) {
                    String line = br.readLine();
                    if ("exit".equalsIgnoreCase(line)) {
                        break;
                    }
                    if ("progress".equals(line)) {
                        double percentageComplete = job.getPercentageComplete();
                        System.out.println(String.format("%.3g", percentageComplete)+ "%");
                    }
                }                
            }
            catch (IOException ex) {
                System.err.println("Unexpected exception");
            }
        }
    }
}

这篇关于如何添加另一个控制台与多线程程序交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆