Link to home
Start Free TrialLog in
Avatar of Swaminathan K
Swaminathan KFlag for India

asked on

Multithreading in Java

Hi Team,

iam new to Java , I need to write a program which involves a multi threading . I have an input file ,   from this file I need to read data line by line and then after reading a line , I need to write the logic to store in DB .  This line read needs to be split into individual fields and then stored into database.

I know how to read and split the data , bu tthe logic of simultaneously doing the task using thread is what i want help.

Any help with any sample program is really appreciated.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

And why do you need to multithread it?
Avatar of Swaminathan K

ASKER

actually I want to perform the actrivity simultaneously
So what methodology are you going to use to coordinate the reading and the writing?
in my mind , i want to write an synchronized block  and create two threads one will read line by line into a variable and another thread will write to DB . I want help on writing this code
Is this an exercise or a real-world problem?
its an real word problem . I have to write a code which performs three tasks simultaneously.
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
CHEJ asked, "And why do you need to multithread it?" because...

Writing code as you describe will run orders of magnitudes slower than beginning a transaction + write data + ending a transaction.

If you're just learning about multi-threading, any coding method will suffice.

If you're writing a production application which must complete quickly, using a single transaction to write all data at once will be faster.
... as long as that transaction is not called on the EDT thread.