Commit 3d8f7a20 authored by abdullh.alsoleman's avatar abdullh.alsoleman

DataStructure_Sync

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
<?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="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="250c7cd5-e4dc-4c06-beaf-c8d32899cd02" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Input/input.txt" afterDir="false" />
<change afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Consumer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Producer.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/org/example/Writer.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="2YigWntePwRdOXHc4xQnMYiwg92" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"last_opened_file_path": "C:/Users/Abdullah/Desktop/Producer_Consumer"
}
}]]></component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="250c7cd5-e4dc-4c06-beaf-c8d32899cd02" name="Changes" comment="" />
<created>1701013857104</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1701013857104</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
<?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.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Consumer implements Runnable {
// Queue of lines before processing them
private LinkedBlockingQueue<String> linesQueue;
// Queue of lines after processing them
private ConcurrentLinkedQueue<String> processedLines;
// Constructor to initialize the queues
public Consumer(LinkedBlockingQueue<String> linesQueue, ConcurrentLinkedQueue<String> processedLines) {
this.linesQueue = linesQueue;
this.processedLines = processedLines;
}
@Override
public void run() {
try {
// Infinite loop for continuous processing
while (true) {
// Print the name of the current thread for debugging purposes
System.out.println(Thread.currentThread().getName());
// Retrieve a line from the linesQueue
String line = linesQueue.poll();
// Check if there are no more lines to process
if (line == null) {
break; // Exit the loop if no more lines
}
// Convert each lowercase letter to uppercase and vice versa
StringBuilder swappedString = new StringBuilder();
for (char c : line.toCharArray()) {
if (Character.isUpperCase(c)) {
swappedString.append(Character.toLowerCase(c));
} else if (Character.isLowerCase(c)) {
swappedString.append(Character.toUpperCase(c));
} else {
swappedString.append(c);
}
}
// Offer the processed line to the processedLines queue
processedLines.offer(swappedString.toString());
}
} catch (Exception e) {
// Print the exception stack trace for debugging purposes
e.printStackTrace();
}
}
}
package org.example;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
public class Producer implements Runnable {
// File to read lines from
private String file;
// Queue to store lines read from the file
private LinkedBlockingQueue<String> linesQueue;
// Constructor to initialize the file and linesQueue
public Producer(String file, LinkedBlockingQueue<String> linesQueue) {
this.file = file;
this.linesQueue = linesQueue;
}
@Override
public void run() {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
// Read lines from the file until the end is reached
String line;
while ((line = reader.readLine()) != null) {
// Put the line into the linesQueue, blocking if the queue is full
linesQueue.put(line);
}
} catch (IOException | InterruptedException e) {
// Print the exception stack trace for debugging purposes
e.printStackTrace();
}
}
}
package org.example;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class Writer {
public static void main(String[] args) {
// File paths
String inputFile = "Input/input.txt";
String outputFile = "Output/output.txt";
// Number of consumer threads
int numConsumers = 3;
// Create a blocking queue to hold the lines read from the file
LinkedBlockingQueue<String> linesQueue = new LinkedBlockingQueue<>();
// Create a concurrent queue to store the processed results for each consumer
ConcurrentLinkedQueue<String> processedResults = new ConcurrentLinkedQueue<>();
// Create a producer thread to read lines from the file and put them into the linesQueue
Producer producer = new Producer(inputFile, linesQueue);
Thread producerThread = new Thread(producer);
producerThread.start();
// Create a thread pool with the specified number of consumers
ExecutorService executor = Executors.newFixedThreadPool(numConsumers);
// Submit consumer tasks to the thread pool
for (int i = 0; i < numConsumers; i++) {
Consumer consumer = new Consumer(linesQueue, processedResults);
executor.submit(consumer);
}
// Wait for all consumer tasks to complete
executor.shutdown();
while (!executor.isTerminated()) {
// Wait for termination
}
// Write the processed results to the output file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) {
for (String result : processedResults) {
writer.write(result);
writer.newLine();
}
} catch (IOException e) {
// Print the exception stack trace for debugging purposes
e.printStackTrace();
}
}
}
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