Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
S
Synchronization_of_threads
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
yazan.halloul
Synchronization_of_threads
Commits
9614576c
Commit
9614576c
authored
Nov 28, 2023
by
yazan.halloul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
build main pipeline classes (Producer, multi Consumer for text processing, Consumer)
parent
4f54e8fa
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
129 additions
and
0 deletions
+129
-0
Consumer.java
src/main/java/main_pipeline/Consumer.java
+42
-0
Producer.java
src/main/java/main_pipeline/Producer.java
+35
-0
TextProcessorConsumer.java
src/main/java/main_pipeline/TextProcessorConsumer.java
+52
-0
No files found.
src/main/java/main_pipeline/Consumer.java
0 → 100644
View file @
9614576c
package
main_pipeline
;
import
benefit_classes.LineSequence
;
import
java.io.BufferedWriter
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.util.concurrent.BlockingQueue
;
public
class
Consumer
implements
Runnable
{
private
final
BlockingQueue
<
LineSequence
>
outputQueue
;
private
final
String
outputFilePath
;
public
Consumer
(
BlockingQueue
<
LineSequence
>
outputQueue
,
String
outputFilePath
)
{
this
.
outputQueue
=
outputQueue
;
this
.
outputFilePath
=
outputFilePath
;
}
@Override
public
void
run
()
{
int
sequence
=
1
;
try
(
BufferedWriter
writer
=
new
BufferedWriter
(
new
FileWriter
(
outputFilePath
)))
{
while
(
true
)
{
LineSequence
result
=
outputQueue
.
peek
();
if
(
result
!=
null
&&
result
.
getSequence
()
==
sequence
)
{
result
=
outputQueue
.
take
();
// Retrieve processed line from the output queue
sequence
+=
1
;
if
(
result
.
getLine
().
equals
(
"EOF"
))
{
break
;
}
else
{
writer
.
write
(
result
.
getLine
());
// Write the result to the output file
writer
.
newLine
();
writer
.
flush
();
}
}
}
}
catch
(
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/main_pipeline/Producer.java
0 → 100644
View file @
9614576c
package
main_pipeline
;
import
benefit_classes.LineSequence
;
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.io.IOException
;
import
java.util.concurrent.BlockingQueue
;
public
class
Producer
implements
Runnable
{
private
final
BlockingQueue
<
LineSequence
>
inputQueue
;
private
final
String
inputFilePath
;
public
Producer
(
BlockingQueue
<
LineSequence
>
inputQueue
,
String
inputFilePath
)
{
this
.
inputQueue
=
inputQueue
;
this
.
inputFilePath
=
inputFilePath
;
}
@Override
public
void
run
()
{
int
sequence
=
1
;
// Read lines from file and add to queue
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
FileReader
(
inputFilePath
)))
{
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
LineSequence
lineSequence
=
new
LineSequence
(
line
,
sequence
);
inputQueue
.
put
(
lineSequence
);
// Put line into the input queue
sequence
+=
1
;
}
inputQueue
.
put
(
new
LineSequence
(
"EOF"
,
sequence
));
}
catch
(
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/main_pipeline/TextProcessorConsumer.java
0 → 100644
View file @
9614576c
package
main_pipeline
;
import
benefit_classes.DESAlgorithm
;
import
benefit_classes.LineSequence
;
import
javax.crypto.BadPaddingException
;
import
javax.crypto.IllegalBlockSizeException
;
import
javax.crypto.NoSuchPaddingException
;
import
java.io.UnsupportedEncodingException
;
import
java.security.InvalidKeyException
;
import
java.security.NoSuchAlgorithmException
;
import
java.util.concurrent.BlockingQueue
;
public
class
TextProcessorConsumer
implements
Runnable
{
private
final
BlockingQueue
<
LineSequence
>
inputQueue
;
private
final
BlockingQueue
<
LineSequence
>
outputQueue
;
private
static
boolean
fileEnded
=
false
;
private
final
boolean
typeOfProcess
;
public
TextProcessorConsumer
(
BlockingQueue
<
LineSequence
>
inputQueue
,
BlockingQueue
<
LineSequence
>
outputQueue
,
boolean
typeOfProcess
)
{
this
.
inputQueue
=
inputQueue
;
this
.
outputQueue
=
outputQueue
;
this
.
typeOfProcess
=
typeOfProcess
;
}
@Override
public
void
run
()
{
try
{
DESAlgorithm
des
=
new
DESAlgorithm
(
"fzI0SlHWAfc="
);
while
(!
fileEnded
)
{
LineSequence
lineSequence
=
inputQueue
.
take
();
// Retrieve line from the input queue
if
(
lineSequence
.
getLine
().
equals
(
"EOF"
))
{
fileEnded
=
true
;
outputQueue
.
put
(
lineSequence
);
break
;
}
else
{
System
.
out
.
println
(
Thread
.
currentThread
().
getName
()
+
" "
+
lineSequence
.
getLine
()
+
" "
+
lineSequence
.
getSequence
());
// Process the line
if
(
typeOfProcess
)
lineSequence
.
setLine
(
des
.
encryption
(
lineSequence
.
getLine
()));
else
lineSequence
.
setLine
(
des
.
decryption
(
lineSequence
.
getLine
()));
outputQueue
.
put
(
lineSequence
);
// Put the processed line into the output queue
}
}
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
}
catch
(
NoSuchPaddingException
|
UnsupportedEncodingException
|
IllegalBlockSizeException
|
NoSuchAlgorithmException
|
BadPaddingException
|
InvalidKeyException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
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