This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Run same class in multi threads at the same time

 
Joe Walt
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class that opens a database file, reads the records by batch# then closes the file.
I am trying to read a list of batch# and run a seperate thread for each batch#.

public static synchronized String createBatchFile(String batchNumber) {

String fileName = downLoadFile;
PriceBatchCreator batchCreator = new PriceBatchCreator(batchNumber);
Thread runner = new Thread(batchCreator);
runner.start();
}

When more then one Thread is running at the same time I get an error that the file is closed when I try to read the file with the second Thread after the first Thread has closed the file.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is impossible to tell what the problem is based on the information you provided. You say the error says that the file is closed when you read the file. What file? Your code doesn't show any file (opening, closing, reading, or anything else). You need to find the relevant code and show us that, the code you showed here is irrelevant.

I guess the only thing I we can guess from the code you provided is that PriceBatchCreator is probably not thread safe and probably allows Objects to be shared between instances (such as via static variables, or some other mechanism.)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's not really enough information there. It would help if you could post an SSCCE and copy/paste the exact, complete error message, and indicate which line is causing it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even without knowing what your code is actually doing, this bit looks odd:

I get an error that the file is closed when I try to read the file with the second Thread after the first Thread has closed the file.


So, you're acknowledging that you've clsoed the file, and are trying to read from that closed file, but you're surprised that you're getting an error about trying to read from the closed file? The file that you said that you closed and are still trying to read from?

 
Rakesh K. Cherukuri
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree to the point that there is not enough information.

Looking at code
Joe Walt wrote:
String fileName = downLoadFile;
its possible that you are using the same file across threads. That file may not be supporting multithreaded/concurrent access. Might want to check that part.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic