Commit 9d6db561 authored by OlaBola98's avatar OlaBola98
Browse files


parent 17d59736
......@@ -6,15 +6,37 @@
##Task 2
Notes for task 2:
2.1 public String[] searchQuery returns a query to string, and splits the query
on the key ";".
on the key ";".
2.2 public Document call() translates a voc with parser and translats it to a document.
2.2 The IndexingTask class will parse the document to get its vocabulary, by implementing the "Callable" interface
with Document as a parameter. The File object is the document being parsed. The call() method parses the document
to find the vocabulary of the document, and returns the data found, together with the document object.
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.
The run() method obtains the future object from CompletionService.take().get(). The while(true)
will run until the thread is interrupted, and when it is interrupted, it uses the poll() method
to process all the Future objects. The inverted index is updated with the object from the take()
method by using the updateInvertedIndex() method. The updateInvertedIndex() method uses the parameters
vocabulary from document, inverted index and the file name, processing all the words.
If a word is not in the invertedIndex, the updateInvertedIndex() method will use
computeIfAbsent() method to add it.
ConcurrentIndexing initilizes its variables ConcurrentHashMap for storing the inverted index,
ExecutorComletionService for executing the tasks, and executionTime which returns
the runtime of the processes with the getExecutionTime()-method.
It creates two threads to execute two InvertedIndexTask.
For every file it creates an InvertedTask object and delivers it to the CompletionService
class with the submit()-method. There is a prevetion where if the queue of tasks is larger
than size 1000, the thread sleeps. At the end it shuts down with the shutdown()-method
and a awaitTermination()-method which returns when the InvertedTask has finished.
It then interrupts the remaining threads.
Source: "Mastering Concurrency Programming with Java 9" (page 156-160) - by Javier Fernandez Gonzalez
##Task 3
......@@ -3,6 +3,8 @@ import java.util.Random;
public class Main {
public static void main(String[] args) {
......@@ -14,14 +14,14 @@ public class MatrixMult {
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) {
threads.clear(); }
package src.task3;
public class printer {
public void printer(){
process P1;
......@@ -4,23 +4,24 @@ public class Sem {
private int permits;
public Sem(int permits) {
this.permits = permits;
public synchronized void acquire() throws InterruptedException {
if (permits > 0) {
// TODO: Your solution goes here.
} else {
// TODO: Your solution goes here.
while (permits == 0) {
permits = -1;
public synchronized void release() {
// TODO: Your solution goes here.
permits = 1;
public synchronized void release() {
// TODO: Your solution goes here.
......@@ -9,6 +9,15 @@ public class Doll {
public String toString() {
return "Doll{" +
"id=" + id +
", qualityScoreMachine=" + qualityScoreMachine +
", imperfect=" + imperfect +
", isPainted=" + isPainted +
public boolean setPainted(boolean b) {
return isPainted;
package src.task5;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
......@@ -8,7 +10,7 @@ import java.util.concurrent.CyclicBarrier;
public class DollFactory {
List<Doll> dolls;
public CyclicBarrier stageA, stageB, stageC;
private void execution(int dollsNumber) throws InterruptedException {
public void execution(int dollsNumber) throws InterruptedException {
stageA = new CyclicBarrier(dollsNumber);
stageB = new CyclicBarrier(dollsNumber);
// TODO stageC = new CyclicBarrier(...);
......@@ -16,7 +18,7 @@ public class DollFactory {
dolls = new ArrayList<>(dollsNumber);
for (int i = 0; i < dollsNumber; i++) {
Process task = new Process(i);
Process task = new Process(i, dolls, stageA, stageB, stageC);
// TODO: Your solution goes here
Thread thread = new Thread(task);
......@@ -25,8 +27,13 @@ public class DollFactory {
System.out.println("Packaging process D");
// TODO: print results
for (Doll d : dolls) {
} catch (BrokenBarrierException e) {
e.printStackTrace(); }}// c
......@@ -43,7 +43,6 @@ public class Process implements Runnable {
} catch (BrokenBarrierException e) {
void painting(Doll d) {
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