Giter Club home page Giter Club logo

fifth-assignment-multithread-basics's Introduction

Fifth-Assignment-Multithread-Basics

Table of contents

score:100/100

Introduction ๐Ÿ‘‹

Welcome to your Fifth Advanced Programming (AP) journey, where you'll delve into the fascinating world of multithreading in Java. This project is divided into two main sections:

  1. Theoretical Questions: This section is designed to deepen your understanding of key multithreading concepts in Java. You'll have to analyze three code blocks and answer questions about them.

  2. Practical Questions: In this section, you'll get hands-on experience with multithreading in Java. Test cases are provided for each problem, but your code will still be manually checked to ensure you've implemented

Objectives ๐ŸŽฏ

By completing this assignment, you will:

  • Deepen your understanding of multithreading in Java and apply the concepts effectively.
  • Gain familiarity with key multithreading concepts such as the Runnable interface, the Thread class, the interrupt() method, and the run() method.

Note that while this assignment covers many important aspects of multithreading, there are some advanced topics such as race condition and synchronization that won't be covered in this assignment and will be introduced in the following week. However, a solid understanding of the concepts covered in this assignment is crucial for grasping those advanced topics.

Tasks ๐Ÿ“‹

Theoretical Questions ๐Ÿ“

Note: Please answer these questions in a Markdown file and place it in the root directory of your fork. Include code or screenshots where you see fit.

  1. What will be printed after interrupting the thread?
public static class SleepThread extends Thread {
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                System.out.println("Thread was interrupted!");
            } finally {
                System.out.println("Thread will be finished here!!!");
            }
        }
    }

    public static void main(String[] args) {
        SleepThread thread = new SleepThread();
        thread.start();
        thread.interrupt();
    }
  1. In Java, what would be the outcome if the run() method of a Runnable object is invoked directly, without initiating it inside a Thread object?
public class DirectRunnable implements Runnable {
    public void run() {
        System.out.println("Running in: " + Thread.currentThread().getName());
    }
}

public class Main {
    public static void main(String[] args) {
        DirectRunnable runnable = new DirectRunnable();
        runnable.run();
    }
}
  1. Elaborate on the sequence of events that occur when the join() method of a thread (let's call it Thread_0) is invoked within the Main() method of a Java program.
public class JoinThread extends Thread {
    public void run() {
        System.out.println("Running in: " + Thread.currentThread().getName());
    }
}

public class Main {
    public static void main(String[] args) {
        JoinThread thread = new JoinThread();
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Back to: " + Thread.currentThread().getName());
    }
}

Practical Questions ๐Ÿ’ป

Task Scheduler:

  • In this problem, you are given an ArrayList of tasks, each with two fields: taskName and processingTime. The goal is to sort these tasks based on their priority, where tasks with longer processing times have higher priority. After sorting, weโ€™ll create separate threads for each task and execute them. Importantly, weโ€™ll wait for each thread to finish its work to ensure the desired execution order. Finally, weโ€™ll return the order of task execution.

Parallelizing Matrix Multiplication:

  • In this problem, you are asked to write a program that can parallelize matrix multiplication using multithreading.

  • In the ParallelizeMatMul method, you will be given two matrices, A and B. Matrix A has dimensions p ร— q, and matrix B has dimensions q ร— r (where both p and r are even numbers). Your task is to compute the product of matrices A and B to obtain a resulting matrix, C. To achieve faster execution, weโ€™ll utilize multithreading.

  • Hint:

    • Divide the final matrix C into four equal quarters, as shown in the figure below. Assign each quarter to a separate thread for calculations.

    • Procedure:
      1. Divide the quarters among four threads in the ParallelizeMatMul method.
      2. Specify how each thread should calculate the elements in its assigned quarter within its run() method.
      3. Store the calculated elements from each thread in temporary matrices.
      4. Combine the temporary matrices to construct the final matrix C.

Evaluation โš–๏ธ

Your work on this assignment will be evaluated based on:

  • Understanding of Multithreading Concepts: Your ability to accurately answer the theoretical questions, demonstrating a deep understanding of multithreading in Java. Remember that the answers to the theoretical questions should be provided separately in a markdown file.

  • Test Cases: Your code should pass all the tests provided in the test directory. Make sure to enable GitHub Actions to run the tests on GitHub.

  • Code Quality: Your code should be well-structured, readable, and efficient. Proper use of Java conventions, including variable naming, class structure, and comments, will also be considered.

Submission โŒ›

  1. Add your mentor as a contributor to the project.
  2. Create a develop branch for implementing features.
  3. Use Git for regular code commits.
  4. Push your code and the answers file to the remote repository.
  5. Submit a pull request to merge the develop branch with main.

The deadline for submitting your code is Wednesday, May 8 (19th of Ordibehesht)

Resources ๐Ÿ“š

For assistance with this assignment, you may refer to the following resources:

๐Ÿ”— Multithreading in Java on Java Point

๐Ÿ”— Multithreading in Java on Tutorials Point

๐Ÿ”— Multithreading in Java on Geeks for Geeks

Also, you can find a wealth of knowledge from various YouTube courses. They can be a great source of learning. Alongside, joining discussions on forums and reading helpful documents can also be beneficial.

fifth-assignment-multithread-basics's People

Contributors

mahan-m47 avatar mmdproject avatar mohammadghaderi83 avatar rouzbeh1384 avatar seyed0123 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.