简体   繁体   中英

Conceptual: what is the fastest way to get exogenous factors for a Java program to run?

I have a Java program that simulates a kind of market. It has two different exogenous binary variables that determine its state: Continuous/Fixed and Long/Short. A market in the Continuous Long state, for example, acts differently from one in the Fixed Long state, etc. Since I expect the program to receive a large-ish number of requests in production, I'd like to make the process of getting these exogenous factors very fast and resource-light (I think those two mean the same thing in terms of performance, but if that's not the case, I'd want to emphasize the former).

Here are the two ways I've thought about implementing this:

  1. Create a table with two columns corresponding to the above variables (and a single row with the default values for those columns initiated). Before I run my go() method in my program that runs the market, I'd run a getState() method that might look something like this pseudocode (using JDBC to connect to mySQL):

     try (Connection connection = connect(); Statement stmt = connection.createStatement()) { ResultSet rs = stmt.executeQuery(GET_STATE); while(rs.next()) { String contOrFixed = rs.getString("ContOrFixed"); String longOrShort = rs.getString("LongOrShort"); } [set market variables accordingly] }

This is assuming I have a separate class with a setState() method that doesn't have to run every time.

  1. Another implementation might be a different runner method that sets the state through variables and then calls the main method of the class that holds my market. It might look like this:

     public static void main(String[] args) { args = new String[2]; args[0] = contOrFixed; args[1] = longOrShort; myMarket.main(args); }

This is assuming I have a main method in the myMarket class, and that I have a separate class that sets continuousOrFixed and longOrShort that doesn't have to run every time.

Now, what would be the way to accomplish my goal of a fast and resource-light way of getting the state of the market every run? Is it one of the two above or something different? Of course, this is a small task so I assume the time taken will be negligible for something like 100 runs, but not for a greater order of magnitude.

public static void main.... is your real problem. Whatever you do inside of it hardly matters as Java starts slow. Parsing command line arguments is much faster than connecting to the database, but the latter is usually still much faster than starting a JVM.

Java itself is pretty fast, but you need a running process and sending requests telling it what to do. You can use sockets or HTTP(S) or something more complicated. I'd go for HTTPS as it's the most common way.


I may have misunderstood your question, but then consider clarifying it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM