• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Run same class in multi threads at the same time

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic