You need to sign in or sign up before continuing.
Commit 8aa458c4 authored by areej.mohammad's avatar areej.mohammad

Initial commit for the producer-consumer project

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
Working hard on concurrent processing tasks is rewarding. The consumers are running efficiently across the lines of data.
Several processors started working on the files. The systems were designed to handle many incoming requests.
We are writing a large list of words. The process is concluding now. Our engineers succeeded in optimizing the processing.
The final results included many stemmed words, which shows the processing was successful. We were waiting for all the consumers to finish their work.
\ No newline at end of file
List of extracted roots
a
acros
all
are
are
conclud
concurrent
consumer
consumer
data
design
efficiently
engineer
file
final
finish
for
handle
hard
in
includ
incom
is
is
large
line
list
many
many
now
of
of
on
on
optimiz
our
proces
process
process
process
processor
request
result
reward
runn
several
show
start
stemm
succeed
successful
system
task
the
the
the
the
the
the
the
the
the
their
to
to
wait
was
we
we
were
were
which
word
word
work
work
work
writ
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>producer_consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
package org.example;
import java.util.*;
import java.util.concurrent.*;
class MultipleConsumers implements Runnable {
private final BlockingQueue<String> inputQueue;
private final BlockingQueue<List<String>> outputQueue;
private final String eofSignal;
public MultipleConsumers(BlockingQueue<String> inputQueue,
BlockingQueue<List<String>> outputQueue,
String eofSignal) {
this.inputQueue = inputQueue;
this.outputQueue = outputQueue;
this.eofSignal = eofSignal;
}
// Simualion of stemming
private String performStemming(String word) {
if (word.length() > 3) {
if (word.endsWith("ing")) {
return word.substring(0, word.length() - 3);
}
if (word.endsWith("s")) {
return word.substring(0, word.length() - 1);
}
if (word.endsWith("ed")) {
return word.substring(0, word.length() - 2);
}
}
return word;
}
@Override
public void run() {
System.out.println("Prosser " + Thread.currentThread().getName() + " The stemming process has begun");
List<String> localStems = new ArrayList<>();
try {
while (true) {
String line = inputQueue.take();
if (line.equals(eofSignal)) {
outputQueue.put(localStems);
inputQueue.put(eofSignal);
break;
}
String[] words = line.toLowerCase().replaceAll("[^a-zA-Z\\s]", "").split("\\s+");
for (String word : words) {
if (!word.isEmpty()) {
String stem = performStemming(word);
localStems.add(stem);
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
System.err.println("fault in prosseing: " + e.getMessage());
} finally {
System.out.println("Prosser " + Thread.currentThread().getName() + " The stemming process has finished.");
}
}
}
\ No newline at end of file
package org.example;
import java.io.*;
import java.util.concurrent.*;
class Producer implements Runnable {
private final String filePath;
private final BlockingQueue<String> outputQueue;
private final String eofSignal;
public Producer(String filePath, BlockingQueue<String> outputQueue, String eofSignal) {
this.filePath = filePath;
this.outputQueue = outputQueue;
this.eofSignal = eofSignal;
}
@Override
public void run() {
System.out.println("➡️ Producer started reading from file: " + filePath);
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
// Placing the line in the Blocking Queue (may block if the queue is full)
outputQueue.put(line);
}
} catch (IOException e) {
System.err.println("Error reading input file: " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
// Sending the End-Of-File (EOF) signal to all consumers
try {
// Send the signal 'N' times, where N is the number of consumers.
// This ensures every consumer receives the signal to stop.
for (int i = 0; i < Stemming.NUM_CONSUMERS; i++) {
outputQueue.put(eofSignal);
}
System.out.println(" Producer finished sending all lines and EOF signal.");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
package org.example;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
class SingleConsumerWriter implements Runnable {
private final String outputPath;
private final BlockingQueue<List<String>> inputQueue;
private final int numExpectedResults;
public SingleConsumerWriter(String outputPath, BlockingQueue<List<String>> inputQueue, int numExpectedResults) {
this.outputPath = outputPath;
this.inputQueue = inputQueue;
this.numExpectedResults = numExpectedResults;
}
@Override
public void run() {
System.out.println("The writing consumer began collecting and writing the roots. .");
List<String> finalStems = new ArrayList<>();
int resultsReceived = 0;
try {
while (resultsReceived < numExpectedResults) {
List<String> partialStems = inputQueue.take();
finalStems.addAll(partialStems);
resultsReceived++;
}
try (PrintWriter writer = new PrintWriter(new FileWriter(outputPath))) {
writer.println("List of extracted roots");
finalStems.stream()
.sorted()
.forEach(writer::println);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (IOException e) {
System.err.println("Error writing the output file: " + e.getMessage());
} finally {
System.out.println(" The writing consumer has completed the merging and writing process.");
}
}
}
\ No newline at end of file
package org.example;
import java.util.*;
import java.util.concurrent.*;
public class Stemming {
// 1. Shared Blocking Queues
// Queue 1: To transfer raw lines from the Producer to the Multiple Consumers
private static final BlockingQueue<String> queue1_Lines = new LinkedBlockingQueue<>(100);
// Queue 2: To transfer partial results from Consumers to the Single Writer
private static final BlockingQueue<List<String>> queue2_PartialResults = new LinkedBlockingQueue<>();
// Special signal to indicate End-Of-File (EOF)
private static final String EOF = "EOF_SIGNAL_XYZ";
public static final int NUM_CONSUMERS = 4; // Number of parallel processing threads
public static void main(String[] args) {
// Define file paths
String inputPath = "input.txt";
String outputPath = "output.txt"; // Will be created in the working directory
// 1. Create Threads
// The Producer reads the file and puts lines into queue1
Thread producer = new Thread(new Producer(inputPath, queue1_Lines, EOF), "Producer");
// The Writer collects all partial maps from queue2 and writes the final output
Thread writer = new Thread(new SingleConsumerWriter(outputPath, queue2_PartialResults, NUM_CONSUMERS), "WriterConsumer");
// 2. Create and start Multiple Consumers
// Uses a fixed thread pool to manage the worker threads
ExecutorService consumerPool = Executors.newFixedThreadPool(NUM_CONSUMERS);
for (int i = 0; i < NUM_CONSUMERS; i++) {
// Each MultipleConsumer thread takes lines from queue1 and puts partial results into queue2
consumerPool.execute(new MultipleConsumers(queue1_Lines, queue2_PartialResults, EOF));
}
// 3. Start all threads
producer.start();
writer.start();
// 4. Shutdown services and wait for termination
try {
// Wait for the Producer to finish reading the entire file
producer.join();
// Shut down the consumer pool and wait for all tasks to complete (since EOF signals were sent)
consumerPool.shutdown();
consumerPool.awaitTermination(1, TimeUnit.MINUTES);
// Wait for the Writer to finish combining results and writing the output file
writer.join();
System.out.println(" Processing completed successfully. Check file: " + outputPath);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println(" Execution process was interrupted.");
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment