• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why would a thread fail?

 
Zak Tacc
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have the following assignment:

"Write a Java program that reads a csv file, parses the data, distributes the data to multiple threads, where each line of data in the file is written to a database row. The program should not exit until the entire file is processed and written to the database. Each data set should be assigned to a specific thread, and only the owning thread should process the data. Metrics should be kept in terms of success/failures. Failures should be placed in a cache and retried once and only once by a thread other than the thread that made the original attempt."

I'm confused as to what the last sentence means. Why would a thread fail? Could anyone clear this up for me? Thanks
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think the assignment meant that the thread itself failed, but that performing the task failed in some way (maybe because writing to the database resulted in an IOException, or something similar?).
 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zak,
The thread could fail because of any business error(e.g some unique key constraint violated on database). Essentially failure here means that thread execution resulted in a exception.

Thanks,
Satish
 
Zak Tacc
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish Kumarkk wrote:Hi Zak,
The thread could fail because of any business error(e.g some unique key constraint violated on database). Essentially failure here means that thread execution resulted in a exception.

Thanks,
Satish


So if I designed the program right, shouldn't there be no failures?
 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not necessarily. Failure can be programming (for NPE and all), resource related(DB connection, IO exceptions), business related(some business rules e.g salary can not be negative)

Your design should state what to do in case of any failures. ie should you consume the exception and do nothing or do some exception handling.
typically programming errors should not be handled.

resource related errors should be handled.
Business errors should definitely be handled.

Your prof wants to have retry logic so that user(client) doesn't suffer due to no fault of his(e.g IO exception normally means system issue and not user issue).
In short, your prof wants to have error handling built into your logic.

Hope it helps

Thanks,
Satish
 
Zak Tacc
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thank you. I'm still a bit confused on how to structure my multi-threading though. Basically what I've decided to do is read from a .csv file consisting of 6 columns and 248 rows, call .readLine() to grab a row, and then start a new thread to insert that row's information into a MySQL database, then a new thread with the next row. I'm new to multi-threading, but that would give me 248 separate threads. Is that the right idea or am I approaching this wrong?

Thank you by the way, you're helping me a lot.
 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct as per the assignment objective.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The structure aspects of the program are being discussed here. Let's keep this thread for discussing the possible reasons for thread failure, otherwise you are going to be wasting people's time giving answers that you have already got in the other thread (no pun intended )
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!