Commit a73deabb authored by OlaBola98's avatar OlaBola98
Browse files

Task 1 and beginning of task 3

parent 4c771063
##Task 1
Notes for task 1
Task one contains Java concurrency, by utilising the properties of threads(wait, clear).
SPEED:
##Task 2
Notes for task 2:
2.1 public String[] searchQuery returns a query to string, and splits the query
on the key ";".
2.2 public Document call() translates a voc with parser and translats it to a document.
2.3 If the thread is no interrupted it makes a document into a completionService with a
take-method to create an array which cant be made bigger or smaller than it is on creation.
It then creates a Futur method for document to check if the task is being done, or done.
If there is no task, it breaks. If the task is done, it retrieves the information with
the get-method, updating the Inverted Index with the documents voc and file name.
##Task 3
3.1
n is the number of processes, and m is the number of threads, find the answer using
this formula: (n*m)! / m!^n
(3*2)! / 2!^3 = 720 / 8 = 90
3.2
printer() {'
semaphore s1 = new semaphore(0);
semaphore s2 = new semaphore(0);
process P1 {
write("1");
write("2");
s1.V();
}
process P2 {
s1.P();
write("3");
write("4");
s2.V();
}
process P3 {
s2.P()
write("5");
write("6");
}
\ No newline at end of file
package src.task1;
import java.util.Random;
public class Main {
public static void main(String[] args) {
}
public static double[][] generate(int rows, int columns) {
double[][] ret = new double[rows][columns];
Random random = new Random();
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
ret[i][j] = random.nextDouble() * 10;
return ret;
}
}
package src.task1;
import java.util.ArrayList;
import java.util.List;
public class MatrixMult {
public static void multiply( double[][] A, double[][] B, double[][] C) {
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < A.length; i++) {
RowMultiplierTask task = new RowMultiplierTask(C, A, B, i);
Thread thread = new Thread(task);
// TODO: Your solution goes here
thread.start(); //hmm
threads.add(thread); //hmm
if (threads.size() % 10 == 0) waitForThreads(threads);}
// TODO: Your solution goes here
}
private static void waitForThreads(List<Thread> threads) {
for (Thread thread : threads) {
try {
// TODO: Your solution goes here
thread.join(); //hmm
} catch (InterruptedException e) {
e.printStackTrace();}}
threads.clear(); }
}
package src.task1;
public class RowMultiplierTask implements Runnable {
private final double[][] C, A, B;
private final int row;
public RowMultiplierTask(double[][] C, double[][] A, double[][] B, int i) {
this.C = C;
this.A = A;
this.B = B;
this.row = i;
}
@Override
public void run() {
for (int j = 0; j < B[0].length; j++) {
C[row][j] = 0;
for (int k = 0; k < A[row].length; k++) {
// TODO: Your solution goes here
C[row][j] += A[row][k] * B[k][j]; //hmm
}
}
}
}
package src.task1;
public class SerialMult {
public static void multiply (double[][] A, double[][] B, double[][] C){
int m = A.length; int n = A[0].length; int k = B[0].length;
for (int I = 0; I < m; I++)
for (int J = 0; J < k; J++) {
C[I][J] = 0;
for (int K = 0; K < n; K++)
C[I][J] += A[I][K] * B[K][J]; }
}
}
/**package src.task2;
public class ConcurrentIndexing {
private ConcurrentHashMap<String, StringBuffer> invertedIndex;
private ExecutorCompletionService<Document> completionService;
private int executionTime;
public int getExecutionTime() {
return executionTime;
}
public void index(File[] files) {
int numCores = Math.max(Runtime.getRuntime().availableProcessors() - 1, 1);
ThreadPoolExecutor executor = (ThreadPoolExecutor)
Executors.newFixedThreadPool(numCores);
completionService = new ExecutorCompletionService<>(executor);
invertedIndex = new ConcurrentHashMap<>();
InvertedIndexTask invertedIndexTask = new InvertedIndexTask();
Thread thread1 = new Thread(invertedIndexTask);
thread1.start();
InvertedIndexTask invertedIndexTask2 = new InvertedIndexTask();
Thread thread2 = new Thread(invertedIndexTask2);
thread2.start();
for (File file : files) {
IndexingTask task = new IndexingTask(file);
completionService.submit(task);
if (executor.getQueue().size() > 1000) {
do {
try {
TimeUnit.MILLISECONDS.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} while (executor.getQueue().size() > 1000);
}
}
executor.shutdown();
try {
executor.awaitTermination(1, TimeUnit.DAYS);
thread1.interrupt(); thread2.interrupt();
thread1.join(); thread2.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}*/
/**package src.task2;
import javax.swing.text.Document;
import javax.swing.text.html.parser.DocumentParser;
import java.io.File;
import java.util.Map;
import java.util.concurrent.Callable;
public class IndexingTask implements Callable<Document> {
private final File file;
public IndexingTask(File file) {
this.file = file;
}
@Override
public Document call() {
DocumentParser parser = new DocumentParser();
Map<String, Integer> voc = parser.parse(file.getAbsolutePath());
Document document = new Document();
document.setFileName(file.getName());
document.setVoc(voc);
return document;
}
}
*/
\ No newline at end of file
/**package src.task2;
import javax.swing.text.Document;
import java.util.Map;
import java.util.concurrent.Future;
public class InvertedIndexTask implements Runnable {
@Override
public void run() {
try {
while (!Thread.interrupted()) {
Document document = completionService.take().get();
updateInvertedIndex(document.getVoc(), document.getFileName());
}
while (true) {
Future<Document> future = completionService.poll();
if (future == null) break;
Document document = future.get();
updateInvertedIndex(document.getVoc(), document.getFileName());
}
} catch (InterruptedException | ExecutionException e) {
Thread.currentThread().interrupt();
}}
private void updateInvertedIndex(Map<String, Integer> voc, String fileName) {
for (String word : voc.keySet()) {
if (word.length() >= 3) {
StringBuffer buffer =
invertedIndex.computeIfAbsent(word, k -> new StringBuffer());
buffer.append(fileName + ";");
}
}
}
}
*/
package task3;
public class printer {
public void printer(){
process P1
}
}
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