+ 2
How to know when the operation is done?
Hey guys! I want to know how can I know that the operation is completed whether it succeeded or not for example I have a code to copy files or to send some data to database or anything but it needs some time to complete so the codes after that code don't wait for it to succeed. Another important information which is my has a for statement which can do one or even thousands of operations and I tried to use a boolean so the for statement sets it to true after it finishes and the codes after it check for the boolean before starting but I couldn't make it so if you have any idea please let me know. Thanks in advance.
12 Respostas
+ 2
If this operation is something you're implementing, you have a few options:
- synchronous. Block until the operation completes. This is how nearly all code works but it might cause problems if the step takes more than a 100ms or so. That's when it could interfere with responsiveness for a user interface. More than several seconds and it could time out a server. A return value or throwing exception would be a good way to return status of the operation after completion.
- asynchronous. In Java, you could pass an object where one of its methods gets called when the operation completes or progresses to different stages. Parameters to the method could express the status of the operation.
A similar approach is the observer pattern. That is the design pattern used by a lot of event listeners and dispatchers in Java for things like user input events. Here, you could have an object responsible for your lengthy task dispatch events as it gets completed. You could have one or more listeners react to the progress change.
If this operation isn't code you wrote, you need to be a lot more specific. For example, if this was a query using JDBC, paste the relevant code.
+ 1
Thanks a lot Josh Greig actually the code is a for statement to copy files from path to another one I will paste it here so you can check it and guide me through
+ 1
Here's my code basically I have two things to do first to copy some files if exist and second to archive (zip) them.
Check my code and you will understand it.
What I want to say is that I have more than one copy function placed before the zip function so if you want let's start with one and if it worked we head to more.
+ 1
private void _copyFilesfromPath (final String _path) {
// my copy function starts from here
FileUtil.makeDir(app_path.concat("temp backup/".concat(_path)));
temp_copyL.clear();
FileUtil.listDir(sketchware_path.concat(_path), temp_copyL);
temp_copyN = 0;
for(int _repeat36 = 0; _repeat36 < (int)(temp_copyL.size()); _repeat36++) {
if (FileUtil.isFile(temp_copyL.get((int)(temp_copyN)))) {
FileUtil.copyFile(temp_copyL.get((int)(temp_copyN)), app_path.concat("temp backup/".concat(_path.concat("/".concat(Uri.parse(temp_copyL.get((int)(temp_copyN))).getLastPathSegment())))));
}
temp_copyN++;
}
// my copy function ends here
}
private void _zipDirectory (final String _sourcePath, final String _destPath) {
// my zip function starts here
verifyStoragePermissions();
String dir = _sourcePath;
String zipDirName = _destPath;
zipDirectory(dir, zipDirName);
}
List<String> filesListInDir = new ArrayList<String>();
public void zipDirectory(String dir, Strin
0
That code clears it up quite well. You have some synchronous code that takes a while to complete and you want to find a way to run that in parallel with something else?
Have you used Java's Thread class to run something in parallel? Here is a tutorial: https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
If you don't care about making this very generic, the observer pattern and my previous answer may complicate the code unnecessarily. You might just want to implement your file copy and zip steps in the run() method of a subclass of Thread and then call "start()" on an instance of your new class. This will cause those steps to run in parallel with whatever else you want to do.
Do you have a graphical user interface that locks up currently while this runs?
0
Not actually I just wanted to know when the copy operation completes so I start the one after it until all copy operations complete then start the zip process so if I can do it it will be easy to link a progress bar or something but for now nothing locks when I execute this.
0
ok. From what I can see _copyFilesfromPath should return only after all files are copied. There shouldn't need to be a check to see if it succeeds from what I see because merely returning should imply success.
FileUtils is from org.apache.commons.io, right?
It is documented here and says nothing about doing the copy asynchronously:
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#copyFile(java.io.File,%20java.io.File)
Are you sure the paths you're passing to it are what you expect? Are you sure you aren't ignoring some exceptions? Maybe you have a try-catch block like:
try {
// some code that calls _copyFilesfromPath
}
catch (Exception e) {
// do nothing which causes any copy failures to get ignored. (very bad programming practice)
}
0
Yes it is.
I don't think I have but some times the path can be empty so no copy function will be executed can this be wrong?
Well the problem which I face is that I try to run about 7 _copyFilesfromPath processes but the second one starts before the first finishes and like that, I hope it is clear now.
0
Mahmood wrote, "I try to run about 7 _copyFilesfromPath processes but the second one starts before the first finishes and like that"
Response:
Since _copyFilesfromPath is synchronous, how are you running _copyFilesfromPath without the previous one finished? What do you mean by "run about 7 _copyFilesfromPath processes"? Are you using multiple threads or running the Java application multiple times together?
If you're running _copyFilesfromPath using multithreading like I mentioned in a previous answer, can you share that multithreading code?
0
Actually this how I do it :
private void _wholeCopyProcess (final String _path) {
_copyFilesfromPath("data/".concat(_path.concat("/")));
_copyFilesfromPath("files/".concat(_path.concat("/")));
_copyFilesfromPath("mysc/".concat(_path.concat("/")));
_copyFilesfromPath("mysc/list/".concat(_path.concat("/")));
_copyFilesfromPath("resources/broadcast/".concat(_path.concat("/")));
_copyFilesfromPath("resources/comments/".concat(_path.concat("/")));
_copyFilesfromPath("resources/fonts/".concat(_path.concat("/")));
_copyFilesfromPath("resources/icons/".concat(_path.concat("/")));
_copyFilesfromPath("resources/images/".concat(_path.concat("/")));
_copyFilesfromPath("resources/import/".concat(_path.concat("/")));
_copyFilesfromPath("resources/java/".concat(_path.concat("/")));
_copyFilesfromPath("resources/service/".concat(_path.concat("/")));
_copyFilesfromPath("resources/sounds/".concat(_path.concat("/")));
}
0
How do you know that one step is happening before the previous file copying completed? The code looks like it'll finish everything in _copyFilesfromPath before returning which includes all the file copying.
Is a file unexpectedly missing on you?
If you keep reproducing the same bug, try this to see if it makes any difference. It adds a delay for a few seconds. I'm not suggesting this as a permanent solution but it could help determine if you're correct about some incomplete work continuing after _copyFilesfromPath returns. If something really is happening in the background, a long enough delay in the current thread should give the work in the background time to complete and prevent the bug you're seeing.
Add this to the end of your _copyFilesfromPath implementation so it gives 4 extra seconds for things to complete:
try {
System.out.println("About to pause.");
Thread.sleep(4000); // sleep for 4 seconds.
System.out.println("Done pausing.");
}
catch (InterruptedException e) {
e.printStackTrace();
}
0
In fact my has to be dynamic @Josh Greig as some files is under 1 mb and some other can be 100 mb so if you have another idea?