Kyle B.

asked • 12/08/21

Check whether the number is divisible by the multipliers of two and the rest of integers, java multithreading

I am trying to find the largest factor of a number (except than itself) by using multiple

Threads in JAVA. the template for the code is ready but I don't know what to put in the missing methods. the thread classes will hold the missing synchronized methods in run() so the threads can run in the main class, but I still don't understand what exactly wanted from me in the methods.


this is the template:

public class LargestFactor {
// Do not change this method
public int testLargestFactor(int n) {
FactorChecker fc = new FactorChecker(n, 2, 1);
Thread t1 = new Thread(new Factor2(fc));
Thread t2 = new Thread(new FactorRest(fc));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
return fc.getMaxFactor();
}
}

class FactorChecker {
// n: number that'd be queried
// div: divisor
// maxFactor: maximum factor of the number (except than itself)
private int n, div;
private int maxFactor;
public FactorChecker(int n, int div, int maxFactor) {
this.n = n;
this.div = div;
this.maxFactor = maxFactor;
}
// Check whether the number is divisible by the multipliers of two (e.g. 2,4,6,...)
public synchronized void checkFactor2() {
// *** Fill this method ***

}
// Check whether the number is divisible by the rest of the integers
public synchronized void checkFactorRest() {
// *** Fill this method ***
}
public int getN() {
return n;
}
public int getDiv() {
return div;
}
public int getMaxFactor() {
return this.maxFactor;
}
public void setDiv(int div) {
this.div = div;
}
}


// Thread for checking divisibility by multipliers of 2
class Factor2 implements Runnable {
// *** Fill this class ***
}

// Thread for checking divisibility by the rest of the integers
class FactorRest implements Runnable {

// *** Fill this class ***
}

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.