Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've created a java project in ItelliJ IDEA which consists of:
Main.java Class (contains the main() method)
FileOperations.java,
Zipper.java,
SFTPTransfer.java
CleanUp.java
Classes from 2 to 5 are instantiated in main() method (lies inside Main.java). This workflow works smoothly. Creates, Zips and ships a single file to SFTP server.
But I want to run the above program (i.e. call the main() method) at least over ten thousand times as I need to generate and ship that many files.
What is the best way to do so? Can it be simply achieved using some kind of batch files or would threads be a better alternative?
If I use threads then I'm not sure yet how to call main so many no of times.
One way to solve your problem is to:
Move the code from the main method to a separate method in the same class that represents performing the entire sequence of steps for a single file
In the main method, call your new method as many times as you need using whatever construct you prefer (for loop, while loop, thread executor, etc) for each file that needs to be processed.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I wish to implement a variable (in Java) whose value is either stored somewhere or is not reset every time I run the program.
It's related to a "Booking reference Number" for a flight program. I know database connectivity but make a new data base for one variable is pretty pointless. Any ideas as to what I should/can do?
Also I don't want the numbers to be random I want them in order like if the first booking ID is 100 then the next one should be 101 and so on.
Organize your data in a structure and then serialize it.When you re-run your program, look for that serialized version in the file system, if there is any, read it. Viola.!
You should write the variable to a file and then read it from the file the next time you run the program.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I generally write most of my code without worrying about threading and such, and get it working and debugged before trying to offload pieces to other threads. For me, the simplest way to do it is to break the function down into a Runnable or two. From there I can start a piece via new Thread(runnable).start() and other pieces I start on the main thread via handler.post().
The problem is that I can't pass arguments. I can sometimes get around this, but too often I end up using non-local variables, which makes things a real mess. Any ideas on the "correct" way to pass arguments to a runnable?
I usually create a new class implementing Runnable and pass arguments as constructor parameters and store them in final fields.
If I need a result back from computation I implement Callable instead.
I am also using executors rather than threads directly.
Definitely a public synchronized queue.
Create a global queue of data to process, in order, and then use that queue to access external variables/data. Create a method to add data to the queue, and then one to get the last element and shift it.
This ensures that the data gets across to your thread from an external thread, such as the main program thread, without interrupting the program flow.
The queue can be processed by adding value when data needs to be passed, and the get last element method can be called to get the last date element passed to the thread.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a Utils class that loads a large list of Strings (a static variable) and defines a function that uses this list.
I use this function in another class Solution. Currently, I am calling Utils.my_function every time I use it (in a big for loop, so it is called thousands times). Would it be faster if I instantiate a Utils in Solution? (would the list of words defined in Utils be defined only one time?)
a large list of Strings (a static variable)
...
would the list of words defined in Utils be defined only one time?
By definition, a static variable is loaded only once. So it's already the fastest you can do.
EDIT : the devs who code Java are smart. It's very likely that the JVM can detect your array is accessed very often, and will optimise its operations, whether it is a static or instance variable. However I cannot give you more information than this, and maybe some Java experts can give you a more accurate answer.
Well, as already pointed out that static is only called once anyway. Another thing you can do is batch processing -- http://java.dzone.com/articles/batch-processing-best
Instead of looping by each line -- call a set of lines at a time then perform your functions, then move to the next set, etc. You would need to profile your app to see how many lines would yield a good response for the app.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
my main.java file has a length of about 1000 lines. My code is getting more and more confused, and I would like to "split" it in different parts (e.g. in one file I would have essential stuff like OnCreate, in another file I would have for instance GetHttpRequest).
I already tried to put GetHttpRequest in a different class, but is there no simpler way? (It would take a really long time to adjust the code if I used this method)
You have to use classes and methods, and optionally packages.
This will solve your problem. There's no simpler way than that.
Please do not hard-code your program. There are several patterns on how to code a program, so it is efficient, everybody can easily read and understand it. I think you also have a "GUI", assuming to this, I recommend you to use the MVC pattern. It means Model-View-Controller, so you organize your program in Packages: "model", "view", "controller" and in those packages you put the classes. For instance, you have a simple Calculator. Then you have a class in view thats called "CalculatorView", where your graphical interface is and in controller you have your "CalculatorController" that works out the things like calculations. (You call the controller from the view) and you do not need model at all.
I hope that helps you. But you will have to rewrite all your code...
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Let us say that I have a method that writes to a file or database. What if different parts of application calls this method too many times at the same time or in same interval of time. All those method calls are maintained in some stack/queue in memory and wait for previous requests to be served ?
Writing to the same file is platform dependent, like Unix allows concurrent writes to the same file.
You need to see the synchronization techniques - how you want to manage the read write operations.
If you see from DB perspective the db engine handles it properly - whichever comes first will be served. The next insert would depend on the first insert(in case you already inserted with the same key in the previous operation - then obviously it ll throw an exception)
Also I would say if different parts of your application are appending data to the same file at the same time - there could be design flaw and you need to reconsider the design