This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Copying file issue.

 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day,

I have a 2 module which is run in parallel, the first one is writing a file in directory temp, and the other one is copying file from temp to some directory.

The problem is that, there is an instance that the copy module copies the file while its not yet done writing by the writing module.

Example: writing module suppose to write a 1MB file, but the copy module is copying it while the file is not yet done (20KB).

How can we prevent this?

Regards
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are the pieces of code run in parallel when clearly they should run sequentially?
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a reason behind it. One reason is if they dont want to run the copy module.

Any idea sir?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it doesn't really matter what the reason is, you can't copy a file while it is still being created, so that needs to change.

they dont want to run the copy module.

That being the case, why *is* the copy module run, especially if that causes a problem?
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any function that will not allow to copy a file while file is using by other process?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so. Your code needs to keep track of which files are for copying, and which are not. Or, to keep it simple, the file creation process can notify the file copy process of which files are ready for copying. A java.util.concurrent.BlockingQueue might work for that.
 
Hisouka Devera
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the idea.. Appreciate it a lot. I will try it.
 
Luan Cestari
Ranch Hand
Posts: 172
C++ Redhat Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I so in many different companies using the file name (or extension or moving it between folders) as a way to makr a status of the current file, liek processing and done. In this solution, the other program/thread/etc would just watch more specificy files (or folder), removing any problem with concurrency
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic