Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
parallel_encryption
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tammam.alsoleman
parallel_encryption
Commits
d3536480
Commit
d3536480
authored
Nov 28, 2025
by
tammam.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
create the FileWriterConsumer class
parent
1b762e3a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
2 deletions
+70
-2
FileWriterConsumer.java
src/main/java/writer/FileWriterConsumer.java
+70
-2
No files found.
src/main/java/writer/FileWriterConsumer.java
View file @
d3536480
package
writer
;
public
class
FileWriterConsumer
{
}
import
model.SequencedLine
;
import
java.io.BufferedWriter
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.util.concurrent.PriorityBlockingQueue
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
FileWriterConsumer
implements
Runnable
{
private
final
String
outputFilePath
;
private
final
PriorityBlockingQueue
<
SequencedLine
>
outputQueue
;
private
final
AtomicInteger
activeConsumers
;
public
FileWriterConsumer
(
String
outputFilePath
,
PriorityBlockingQueue
<
SequencedLine
>
outputQueue
,
AtomicInteger
activeConsumers
)
{
this
.
outputFilePath
=
outputFilePath
;
this
.
outputQueue
=
outputQueue
;
this
.
activeConsumers
=
activeConsumers
;
}
@Override
public
void
run
()
{
System
.
out
.
println
(
" Writer started: "
+
outputFilePath
);
int
writtenCount
=
0
;
try
(
BufferedWriter
writer
=
new
BufferedWriter
(
new
FileWriter
(
outputFilePath
)))
{
// Continue writing until all consumers are done and queue is empty
while
(
activeConsumers
.
get
()
>
0
||
!
outputQueue
.
isEmpty
())
{
try
{
SequencedLine
line
=
outputQueue
.
take
();
// Write the line to the output file
writer
.
write
(
line
.
getLine
());
writer
.
newLine
();
writtenCount
++;
// Progress reporting
if
(
writtenCount
%
25
==
0
)
{
System
.
out
.
println
(
" Writer: "
+
writtenCount
+
" lines written (last sequence: "
+
line
.
getSequenceNumber
()
+
")"
);
}
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
" Writer was interrupted while waiting for data"
);
Thread
.
currentThread
().
interrupt
();
break
;
}
}
System
.
out
.
println
(
" Writer finished: "
+
writtenCount
+
" lines written in perfect order"
);
}
catch
(
IOException
e
)
{
System
.
err
.
println
(
" File write error: "
+
e
.
getMessage
());
e
.
printStackTrace
();
}
// Final status report
System
.
out
.
println
(
" Writer completed. Final queue size: "
+
outputQueue
.
size
());
}
public
String
getOutputFilePath
()
{
return
outputFilePath
;
}
public
int
getRemainingQueueSize
()
{
return
outputQueue
.
size
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment