Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with threads  RSS feed

 
Murali Jaya Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a swing application in which i am prompting the user to select a zip file name and path(JFilechooser).after he enters file name i am performing read/write operations.The problem is when two instances running i.e if the user opens another swing ui with first one alive and if he chooses the same zip file so the application is crashing.

how to solve this problem.can anyone help regarding this..

should i implement any threading mechanism then how to implement.

Thanks
Murali
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murali:
if the user opens another swing ui with first one alive and if he chooses the same zip file so the application is crashing.

Can you elaborate as to what you mean by the application is crashing? Is the JVM crashing or the thread of execution dies because of an exception?
What is the exception that you are getting?

Murali:should i implement any threading mechanism

Since two different JVMs are working on the same file, you would not be able to use conventional synchronization techniques to control access to the file.
However, you can think of using FileLock to do so. One thing to remember is the behavior of this class is OS dependent and so must be used if the target environment is known.
This topic has a relevant discussion on FileLock.
 
Murali Jaya Rao
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here one instance will open a file and performing writing operation and closes the stream in the meanwhile another instance is also doing the same thing.so if first instance done with that work and closes the stream ,but second instance is trying to write the content in to the file in which stream is closed.

Thanks
Murali.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murali:
here one instance will open a file and performing writing operation and closes the stream in the meanwhile another instance is also doing the same thing.so if first instance done with that work and closes the stream ,but second instance is trying to write the content in to the file in which stream is closed.


From your original post, it seems that the two programs modifying the zip file runs on different JVMs. If that is the case, then the streams used will be different and hence closing a stream in one program does not affect the other program working on the same file. However, since the contents of the same zip file is being changed, modification from one program *can* cause un predictable damages to other cocurrently running program.
If you want to avoid this situation, you have to tell the later program that someone else is already working on this file. One such way is to use FileLocks as told in my previous post. One more option is specified in the post I have provided a link to.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!