I am presently trying to find a bug in a long java code. This code reads an input text file and it runs many CFD simulations and then it collects results and analyzes them. In the input file there is a flag that only changes the order in which that bunch of completely independent threads is run. However, if I change this flag I have that the java program crashes after all the simulations are run, while it should actually behave the same. In order to debug this, I opened two different Eclipse sessions, and in each session I run the code. The only difference between the 2 sessions is the value of that flag I mentioned above. I have set up a breakpoint in a line after all the 37 threads (i.e. simulations) are run and results saved on file. That line is in a subroutine where only the main thread is active (all the other threads stopped) and it collects and elaborate further the results of the 37 simulations. I would like to have a way to quickly compare all the variables (in the present scope) of one Eclipse session with the other, in order to find possible differences. Saving them in a text file and comparing the 2 text files would make the trick for me. Do you know how I can do it, if even possible at all? Any other working solutions is also welcomed.
Related
This is not like a code help or code review thing.I am trying to make a counter for my program that can define the output of my program.For eg: If i want my program do specific thing after it has been run two times or else it should do another thing.Another example of what i am trying to say is that when i run my program first and second time it should print 3,but when i run it third time it should print 5.
I believe there are many ways already out there,but i don't know how to search such type of specific query on google.
I am working on complex program,where such type of thing is required,but i don't know where and how to start or look.I tried to make a counter,but every time the program runs,it initializes the value of counter and so it doesn't increment.
You have to maintain a state of the each time it runs. It means in the program, maintain a counter and store it in a file. You can do it in the following way.
If there is running state counter stored in a file, it means the application is run first time.
Before running the application, read the file which maintains running counter.
Based upon the running counter number like even , odd etc, you can write the logic.
You have to save the value in a file or some other storage method that persists after the program runs and ends.
Every time the program runs:
If there's no file, then create one and write the number 1 in it to show that the program has run once.
If there is already a file when you run the program, then read the number stored in it and interpret it as the number of previous runs. Write a number to the file that is 1 higher than the number of previous runs.
Basically, I would like to create a "library" of sorts in another class that is created when I run the program's main class the first time. But instead of repeating the action when I click run for a second time, I want the manipulated library in the first run to be initialized. For example if I have a static array with capacity 5 and put the int 1 in index 0, the next time I run the entire thing, I want to start with a static array with 1 in index 0. Is this even possible (in Eclipse) and if so, how can I do this?
All of your program's variables and other state are discarded when it is terminated and reinitialized when it is launched again. (This is a common cause of bugs in Android apps, which are frequently discarded and then re-opened.) To save state between program runs, you need to store it in some place that's intended for this. Common choices include files, databases (H2 with a file-backed database might be a good option), Android SharedPreferences, servers, and more exotic tools such as prevalent systems.
I have followed below link which is for Java Script
Sonarqube: Is it possible to adapt duplication metric for javascript code?
Similarly I have done for my Java project.
And as per this if we wish to change the duplication criteria, i.e. by default 10 lines, we have to add one line in sonar.properties file which is stored in project.
sonar.projectKey=Test
sonar.projectName=Test
sonar.projectVersion=1.0
sonar.sources=src
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.cpd.java.minimumLines=5
But its not working for Java, is there anything else I need to configure?
Per SonarQube's Duplications documentation:
A piece of code is considered duplicated as soon as there are at least 100 successive and duplicated tokens (can be overridden with property sonar.cpd.${language}.minimumTokens) spread on at least 10 lines of code (can be overridden with property sonar.cpd.${language}.minimumLines). For Java projects, the duplication detection mechanism behaves slightly differently. A piece of code is considered as duplicated as soon as there is the same sequence of 10 successive statements whatever the number of tokens and lines. This threshold cannot be overridden.
here's the situation.
In a Java Web App i was assigned to mantain, i've been asked to improve the general response time for the stress tests during QA. This web app doesn't use a database, since it was supposed to be light and simple. (And i can't change that decision)
To persist configuration, i've found that everytime you make a change to it, a general object containing lists of config objects is serialized to a file.
Using Jmeter i've found that in the given test case, there are 2 requests taking up the most of the time. Both these requests add or change some configuration objects. Since the access to the file must be sinchronized, when many users are changing config, the file must be fully written several times in a few seconds, and requests are waiting for the file writing to happen.
I have thought that all these serializations are not necessary at all, since we are rewriting the most of the objects again and again, the changes in every request are to one single object, but the file is written as a whole every time.
So, is there a way to reduce the number of real file writes but still guarantee that all changes are eventually serialized?
Any suggestions appreciated
One option is to do changes in memory and keep one thread on the background, running at given intervals and flushing the changes to the disk. Keep in mind, that in the case of crash you'll lost data that wasn't flushed.
The background thread could be scheduled with a ScheduledExecutorService.
IMO, it would be better idea to use a DB. Can't you use an embedded DB like Java DB, H2 or HSQLDB? These databases support concurrent access and can also guarantee the consistency of data in case of crash.
If you absolutely cannot use a database, the obvious solution is to break your single file into multiple files, one file for each of config objects. It would speedup serialization and output process as well as reduce lock contention (requests that change different config objects may write their files simultaneously, though it may become IO-bound).
One way is to to do what Lucene does and not actually overwrite the old file at all, but to write a new file that only contains the "updates". This relies on your updates being associative but that is usually the case anyway.
The idea is that if your old file contains "8" and you have 3 updates you write "3" to the new file, and the new state is "11", next you write "-2" and you now have "9". Periodically you can aggregate the old and the updates. Any physical file you write is never updated, but may be deleted once it is no longer used.
To make this idea a bit more relevant consider if the numbers above are records of some kind. "3" could translate to "Add three new records" and "-2" to "Delete these two records".
Lucene is an example of a project that uses this style of additive update strategy very successfully.
I am making an app for Android, in my Activity I need to load an array of about 10000 strings. Loading it from database was slow, so I decided to put it directly into one .java file (as a private field). I have about 20 of these classes containing string arrays and my question is, are all the classes loaded into memory after my application is started? If so the Activity in which I need these strings would be loaded quickly, but the application as a whole would have a slow start...
Is there other way, how to very quickly load an 10000 string array from a file?
UPDATE:
Why I need these strings? My Android app allows you to find "journeys" in Prague's public transit - you choose departure stop, arrival stop and it finds your journey (have a look here). My app has a suggestions feature - you enter leter "c" as your departure stop and a suggestions ListView appears with stops starting with "c". For these suggestions I need the strings. Fetching the suggestions from database is slow (about 400ms on G1).
First, 400ms to perform a simple database query is really slow. So slow that I'd suspect that there is some problem in your database schema (e.g. indices) or your database connection configuration.
But if you a serious about not using a database, there are a couple of alternatives to what you are currently doing:
Arrange that the classes containing the arrays are lazily loaded as required, using Class.forName(...). If you implement it right, it should be possible for the garbage collector to reclaim the classes after they have been loaded and the strings have been added to your primary data structure.
Turn the 10000 Strings into a flat file, put the file into your app's JAR file. Then use Class.getResourceAsStream(...) to open the file and read it into the in-memory array.
As above, but using an indexed file and replacing the array with a data structure that allows you to read Strings from the file lazily. (This will be a bit complicated, but if you are worried by the memory consumed by the 10000 Strings, this will help address that.)
A class is loaded only when it is first referenced.
Though you need an array of 10000, you may not need all at once. Here is where the concept of paging comes in. This link indicates that Paging is often done in Android.Initialy have only a small amount of array in memory, and as you need it, keep loading it in to memory and unloading any previous data from memory if not wanted.
For e.g. in any table, at one shot, the user may see at best 50 records, then he will have to scroll(considering his screen is not size of an iMax movie theatre). When he scrolls, load the next chunk of data and unload any data that is now inivsible to the user.
When is a Type Loaded? This is a
surprisingly tricky question to
answer. This is due in large part to
the significant flexibility afforded,
by the JVM spec, to JVM
implementations. Loading must be
performed before linking and linking
must be performed before
initialization. The VM spec does
stipulate the timing of
initialization. It strictly requires
that a type be initialized on its
first active use (see Appendix A for a
list of what constitutes an "active
use"). This means that loading (and
linking) of a type MUST be performed
at or before that type's first active
use.
From http://www.developer.com/java/other/article.php/2248831/Java-Class-Loading-The-Basics.htm
I don't think that you will be happy with maintaining 10K Strings, hardcoded at Java files.
Rather check if you are using the right database for your problem and if your indices are set correctly. A wrong index can cause really poor performance.
Additionally you should limit the amount of results returned by the query, but make sure you don't fetch the entries one by one.
If nothing fits, you can still preload the Strings from the database at startup.
You could preload, let's say 10 entries, for each character. If a character is keyed in, you can preload the entries with that character following another and so on.