gpt4 book ai didi

java - 在单独的连续行中打印 3 个 JAVA 程序的输出,共享同一控制台,在多处理场景中使用\r

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:54 27 4
gpt4 key购买 nike

我有 3 个 java 程序,它们具有单独的 主函数,它们由 3 个独立线程上的父程序调用。所有三个程序都使用以下代码在单个控制台中打印计数器的值

对于程序#1

   System.out.print(ANSI_PURPLE + "  \r  EEG Sensor count =" + Sensor1_tuple_count);
System.out.flush();

对于程序#2

    System.out.print(ANSI_PURPLE + " \r  BP Sensor count = " + Sensor2_tuple_count + " ");
System.out.flush();

对于程序#3

    System.out.print(ANSI_PURPLE + "\r  ECG Sensor count =" + Sensor3_tuple_count);
System.out.flush();

所有这些都在 lambda 函数内更新,现在所有这些值都相互覆盖,我如何获得这样的输出

EEG Sensor count = X
BP Sensor count = Y
ECG Sensor count = Z
<小时/>

编辑1

下面给出了使用 Flink 的单个程序的代码

  public class bp_sensor {

public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";


public static void main(String[] args) throws Exception {

Constants constants = null;

//setting the envrionment variable as StreamExecutionEnvironment
StreamExecutionEnvironment envrionment = StreamExecutionEnvironment.getExecutionEnvironment();

envrionment.setParallelism(1);


DataStream<Sensor_Event> bp_stream = envrionment
.addSource(new EventGenerator_bp_sensor(constants.bp_data_rate,constants.bp_run_time_sec,1,1))
.name("BP stream")
.setParallelism(1);



if(constants.send_to_timekeeper){

//Sending the stream to timekeeper
bp_stream.map(new RichMapFunction<Sensor_Event, String>() {
@Override
public String map(Sensor_Event event) throws Exception {
String tuple = event.toString();
System.out.println(tuple);
return tuple + "\n";
}
}).writeToSocket(constants.timekeeper_ip, 8003, new SimpleStringSchema() );

}




// Sending the stream to mobile phone

if(constants.send_to_android){

DataStreamSink<String> total_tuples = bp_stream.map(new RichMapFunction<Sensor_Event, String>() {

IntCounter Sensor2_tuple_count;

@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
this.Sensor2_tuple_count = getRuntimeContext().getIntCounter("total_tuples");
}

@Override
public String map(Sensor_Event event) throws Exception {
String tuple = event.toString();
Sensor2_tuple_count.add(1);

System.lineSeparator();
System.out.print(ANSI_PURPLE + " \r BP Sensor count = " + Sensor2_tuple_count + " ");
System.out.flush();

// System.out.println(ANSI_BLUE + tuple);

return tuple + "\n";
}
}).writeToSocket(constants.mobile_ip, 7003, new SimpleStringSchema() );


}



//start the execution
JobExecutionResult executionResult = envrionment.execute();

Integer number_of_tuples = (Integer) executionResult.getAllAccumulatorResults().get("total_tuples");
int input_rate = number_of_tuples/constants.bp_run_time_sec;

System.out.println("\n");
System.out.println(ANSI_BLUE + " Expected Input rate of BP Sensor = " + constants.bp_data_rate + " tuples/second");
System.out.println(ANSI_RED + " Actual Input rate of BP Sensor = " + input_rate + " tuples/second");
System.out.println(ANSI_PURPLE + " Total # of tuples sent by BP Sensor = " + number_of_tuples );



}// main



} //class

父程序的代码是

public class start_sensors {

public static void main(String[] args) throws Exception {

ecg_sensor ecg_sensor = null;
bp_sensor bp_sensor = null;
eeg_sensor eeg_sensor = null;


Thread thread1 = new Thread() {

@Override
public void run() {
try {

ecg_sensor.main(null);
} catch (Exception e) {
e.printStackTrace();
}
}
};


Thread thread2 = new Thread() {

@Override
public void run() {
try {
bp_sensor.main(null);
} catch (Exception e) {
e.printStackTrace();
}
}
};


Thread thread3 = new Thread() {

@Override
public void run() {
try {
eeg_sensor.main(null);
} catch (Exception e) {
e.printStackTrace();
}
}
};


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


} //main
} //class

最佳答案

对于线程,要控制它们的工作顺序,您必须使用某种同步。至于控制台的行为,它超出了 Java 的范围,并且取决于环境。

关于java - 在单独的连续行中打印 3 个 JAVA 程序的输出,共享同一控制台,在多处理场景中使用\r,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105031/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com