aspose file tools*
The moose likes Threads and Synchronization and the fly likes Run same class in multi threads at the same time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Run same class in multi threads at the same time" Watch "Run same class in multi threads at the same time" New topic
Author

Run same class in multi threads at the same time

Joe Walt
Greenhorn

Joined: Sep 28, 2012
Posts: 8
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.)


Steve
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jun 01, 2010
Posts: 48

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.



Warm Regards,
Rakesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Run same class in multi threads at the same time