• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read three Files Using MultiThreading and Write into a new File  RSS feed

 
Anand Kk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Team,

I got a question in my last interview, its all about multithreading, interviewer asked me to write a program, to write the contents of three files(F1,F2,F3) in to a new File F4, using multithreading , first thread should read the first file and second thread should read second file, so the File F4 should contain F1's contents in first then F2's contents after that etc. I tried to give my best shot, but i couldn't get a way to ensure that the first thread is reading the first file and write to F4 then second thread reading the second file and writing once first file is written completely into F4 and so on ..You people have any idea how to do this?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You would need to use thread synchronization techniques to make the threads wait for each other.

The assignment they gave you was not a good, practical example of why programming with multiple threads is useful. Having multiple threads would not make anything go faster, if they all have to write to the same file. They probably just wanted to see if you know how to synchronize threads.
 
Anand Kk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to use synchronization but i couldn't get a way ensure that the first thread is reading the first file and writing it to File F4 and second reading the second file and writing the contents to F4 once file one is completely written, and there was one more condition that i cannot use join. Can you give me sample code fragments to get the above mentioned thing to be identified.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand Kk wrote:I tried to give my best shot, but i couldn't get a way to ensure that the first thread is reading the first file and write to F4 then second thread reading the second file and writing once first file is written completely into F4 and so on...

Well, firstly, you don't have to make sure that they read in sequence; only that they write in sequence.

Just one way of doing it might be to have each Thread know what its "previous" and "next" Thread is, with each one looking something like this:
1. Read "my" file into an array or List.
2. If there is a "previous" Thread to "me", wait until it notifies me to continue.
3. Write the contents of my array/List to F4.
4. If there is a "next" Thread to "me", notify it to continue.

And the setup might look something like this:And there may well be much easier ways of doing it, but that's where I'd start.

PS: You're also probably better off making your "Threads" Runnables (java.lang.Runnable).

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!