In Java, set how many seconds to execute a method and report an error after timeout
Set how many seconds to execute a method in Java, timeout Report error /*** Determine whether the instruction is completed** @param isOverFlag Flag whether the execution is completed * @param isOverAddr The address to determine whether execution is completed* @return*/public boolean isOver(boolean isOverFlag, String isOverAddr, PlcInterface plcInterface,long totalMillis) {// Loop reading whether Status of completion of executionint count = 0 ;while (!isOverFlag) {try {LOG.info(“–Loop execution ” + count + “times”);//Read the status of whether execution is completedisOverFlag = plcInterface .readBit(isOverAddr);// The completion status has not been returned within 5 minutes,Indicates that the machine may have failed Errorif (isOverFlag) {LOG.info (“Execution instruction completed”);; span>break;}if (count * 500 > totalMillis) {LOG.info(“Device running timeout …”);break ;}count++ ;//Set to read every 500 millisecondsThread. sleep(500);} catch (Exception e) {}}return isOverFlag ;}
In Java, classes can be defined as local variables. Does it have any practical significance?
The questions I encountered in today’s written test caught me off guard. I would like to ask the master if there are any practical application scenarios for this. 123456789101112131415161718192021222324 public class Test { into local variables class C{ br /> return c.num; . > // Define the class B as a local variable class b { Public int n = 3; /> Object o = (Object) new B(); B b = (B) o; System.out.println(b.n); System.out.println(new Test().test()); >
In java, int division takes two decimal places or a limited number of digits
Java divides two integers with two decimal places: http://blog.sina.com.cn/s/blog_624d755d0101cvuq.html In Java, when two integers are divided, because the numbers after the decimal point will be truncated, the operation result will be an integer. At this time, if you want the operation result to be a floating point number, you must divide one or both of the two integers. Both are cast to floating point numbers. For example:(float)a/b // Force one of the integers to a floating point number, and then divide it by the other integera/(float)b(float)a/ (float)b // Convert two integers to floating point numbers at the same time and then divide them Java code float num= (float)2/3; DecimalFormat df = new DecimalFormat(“0.00”);//Format decimals String s = df.format(num);//The returned type is String Convert decimals to percentages: import java.text.NumberFormat; public class TeachYou { public static void main(String[] args) { // Adding “D” after the number here indicates that it is a Double type. Otherwise, if it is divided, it will be rounded and cannot be used normally double percent = 50.5D / 150D; //Output it to make sure your decimal is correct System.out.println(“Decimal: ” + percent); //Get the formatting object NumberFormat nt = NumberFormat.getPercentInstance(); //Set the percentage precision to 2, i.e.…
In Java, exchange the positions of two elements in an array
Title: Exchange the positions of two different elements in the array [Problem Solving Core]: In Java, to exchange the subscript positions of two elements in an array, you must pass by reference to exchange. It cannot be exchanged directly [Code Demonstration]: public class Swap { public static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } public static void main(String[] args) { int[] a = { 1, 2, 3, 4 , 5 }; // Swap subscript 0 and subscript Data of mark 3 swap(a,0, span>3);//Pass in the reference of the array, two subscripts //In Java, the method to print all elements of an array is Arrays.toString(); //You cannot use System.out.println(a); System.out.println(a);//(Error) Use this to print the first line in the picture below System.out.println (Arrays.toString(a)) ;//Print array } } [Run result]:
In Java, is there a way to force a method not to accept null type parameters?
Let a method clearly state: I do not accept null parameters. If null is passed, I will crash immediately! And, the error should be thrown in It is thrown during compilation phase, do not run to runtime
In Java, what does it mean when a type is followed by angle brackets (as shown in List)?
java list http Write your review! Come on, watch it all Member login | User registration Recommended reading js How bad is the front end? Recently, I feel that I can’t keep up with the development of the front end, so I wrote an article to sigh. I know that there are some schools that teach some simple web page production, the kind that uses Dreamweaver to make a few clicks. Most of them also leave their homework behind, and when they finally hand it in they look like a model… [detailed] Crayon Shin-chan 2023-09-23 01:21:49 js RHEL7.4 compile and install zabbix3.0.24 RHEL7.4X64 compile and install zabbix-3.0.24 official website download source code package: zabbix-3.0.24.tar.gz address: https:www.zabbix.comdownload 1. System… [detailed] Crayon Shin-chan 2023-09-23 01:13:46
In java, what is responsible for interpreting and executing byte code?
In java, what is responsible for interpreting and executing byte codes? In Java, the virtual machine is responsible for interpreting and executing byte codes. The execution mode of the Java language is The source of this article is gao!%daima.com engaging in $ code*! code $ net 3 Semi-compiled and semi-interpreted. Programs written in Java are first converted into standard byte codes by the compiler, and then interpreted and executed by the Java virtual machine. Byte code is a binary file, but it cannot be run directly on the operating system. It can be regarded as the machine code of a virtual machine. The virtual machine separates the bytecode program from each operating system and hardware, making the Java program independent of the platform. The virtual machine in Java is a very important concept and is the basis of the Java language. Mastering it will help you understand the implementation of the Java language. Recommended tutorial: “Java Learning” The above is the detailed content of what is responsible for interpreting and executing byte codes in Java. For more information, please pay attention to other related articles of gaodaima!
In Java, how to combine two JSON object arrays?
I have several strings, each containing a JSON representation of an array of objects. Here is an example from the code to illustrate, although this is not my actual code (passing in a JSON string): String s1 = “[{name: “Bob”, car: “Ford”},{name: “Mary”, car: “Fiat”}]”;String s2 = “[ {name: “Mack”, car: “VW”},{name: “Steve”, car: “Mercedes Benz”}]”; I need to combine these two JSON arrays are combined into one large JSON array. I could treat this as a string manipulation problem and replace the inner square brackets with commas, but that’s not particularly robust (although I’m guaranteed to get valid JSON). I would rather treat the two strings as JSON arrays and just combine them together in some way. This is a great plan, except that I don’t know about the “somehow” part. Does anyone know of a solution in Java that does not require building a Java Object representation of the JSON object? Thank you! Workaround: You really only have two options: parse the JSON (which always involves constructing an object) or not parse the JSON. Of course, parsing won’t be cheaper. At first glance, your idea of treating this as a string manipulation problem might sound flimsy, but the…
In java, how to write logs to log4j’s specific file appender?
How to ensure in Java that the log is written to a specific file if the log4j attribute has multiple file appenders. log4j.rootLogger=INFO,out log4j.appender .SUCCESS_FILE=org.apache.log4j.FileAppenderlog4j.appender.SUCCESS_FILE.File=${dd.log.dir}/success.loglog4j.appender.VALID_FILE=org.apache.log4j.FileAppenderlog4j.appender.VALID_FILE.File=${dd.log.dir}/valid_error.loglog4j.appender.TEMP_FILE=org.apache.log4j.FileAppenderlog4j.appender.TEMP_FILE.File=$ {dd.log.dir}/Temp_error.tmp_log In a Java class, what can I do to write some messages to, say, SUCCESS_FILE and some messages to TEMP_FILE Logger log = Logger.getLogger(Test.class);log.debug(“This message should go to SUCCESS_FILE”);log.debug(“This message should go to TEMP_FILE”); Workaround: This may help: – log4j.appender .successLog=org.apache.log4j.FileAppenderlog4j.appender.successLog.File=${dd.log.dir}/success.loglog4j.appender.tempLog=org.apache.log4j.FileAppenderlog4j.appender.tempLog.File=${dd.log.dir}/Temp_error.tmp_loglog4j.category.successLogger=INFO, successLoglog4j.additivity.successLogger=falselog4j.category .tempLogger=INFO, tempLoglog4j.additivity.tempLogger=false Access them like: – static final Logger successLog = Logger.getLogger(“successLogger”);static final Logger tempLog = Logger.getLogger(“tempLogger”);
In java, in java, you want one class to inherit another class
List of contents of this article: 1. What is the difference between direct static{} and {} in Java class? 2. Java declares an integer array as a member variable in a class. If no value is assigned to it, the value of the numerical element will be empty. Why is this sentence wrong 3. In what areas is the Java language generally used? 4. Where is java mainly used? 5. Where is the line in Java 6. How to use wait correctly in Java? notify and notifyAll java What is the difference between direct static{} and {} in the class? The differences are as follows: static{ //Static code blocks are executed prior to ordinary code blocks. The variables used in static code blocks must be static variables. //Generally, it is used to load the properties file information and will only be executed once. } { //Ordinary code block is executed after the static code block and before the constructor, so the execution order is [static code block-ordinary code block-constructor] //Every time a new object is executed, it will be executed once } java declares an integer array as a member variable in the class, if no value is assigned to…