Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6992533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:38:48+00:00 2026-05-27T19:38:48+00:00

In revising for an upcoming exam here is my solution to finding the min

  • 0

In revising for an upcoming exam here is my solution to finding the min value of an array using 2 threads.

I had to create a wrapper for int to pass the minimum value around by reference.

What do you guys think?

public class FindMin {

    public static void main(String[] args) {

        int[] data = {99,9,14,5,7,33,6,8,21,29,33,44,55,66,77,88,2, 3, 1};
        IntObj min = new IntObj(data[0]);

        Proc p1 = new Proc(0, (data.length/2)-1, data, min);
        Proc p2 = new Proc(data.length/2, data.length, data, min);

        p1.start();
        p2.start();

        try {
            p1.join();
            p2.join();
        }
        catch (InterruptedException e) {}
        System.out.println("Min value: "+min.value);
    }

}

class Proc extends Thread {
    int ub, lb;
    int[] data;
    IntObj min;

    public Proc(int lb, int ub, int[] data, IntObj _min) {
        this.ub = ub;
        this.lb = lb;
        this.data = data;
        min = _min;
    }
    public void run() {
        for(int i = lb; i < ub; i++) {
            compareSet(data[i]);
        }
    }
    public void compareSet(int val) {
        synchronized(min) {
            if(val < min.value) {
                min.value = val;
            }
        }
    }
}
class IntObj {
    int value;
    public IntObj(int _value) {
        value = _value;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:38:48+00:00Added an answer on May 27, 2026 at 7:38 pm

    If you have to synchronise for each comparison, any potential gain from using two threads is more than compensated for by the synchronisation overhead. Let each thread find the minimum in its half and write the result to their own output variable. Then compare the two results in the main thread.

    And your code will give the wrong result if the minimum is located at data[data.length/2-1].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm revising for an upcoming Haskell exam and I don't understand one of the
I am revising for a networks exam and I am not sure of the
I'm revising for a introduction to programming exam and I have a question which
Ok, I'm revising for my upcoming year 2 exams on a CS course and
So I am revising for an exam and have struck a big rock in
Hey I'm just revising for my functional programming exam coming up on friday and
I am revising for an exam tomorrow, and going over a previous years one.
I'm currently revising for an exam and I'm stuck on a question which is:
I am revising for my exam and I am a bit struggling with SQL
I am revising for am exam and one of the topics is regular expressions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.