Looking for answers? Westonci.ca is your go-to Q&A platform, offering quick, trustworthy responses from a community of experts. Experience the ease of finding quick and accurate answers to your questions from professionals on our platform. Experience the convenience of finding accurate answers to your questions from knowledgeable experts on our platform.

(Synchronized threads) Write a program that launches 1000 threads. Each thread adds a random integer (ranging from 1 to 3, inclusive) to a variable sum that is initially 0. You need to pass sum by reference to each thread. In order to pass it by reference, define an Integer wrapper object to hold sum. Run the program with and without synchronization to see its effect.

Sagot :

Using the knowledge in computational language in JAVA it is possible to write a code that organizes and synchronizes the programs that will run on the computer.

Writting the JAVA code as:

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

public class Main

{

Integer sum=new Integer(0);

Main(){

ExecutorService e=Executors.newFixedThreadPool(1000);

final Sum s = new Sum();

for(int i=0;i<1000;i++){

e.execute(s);

}

e.shutdown();

while(!e.isTerminated()){

}

System.out.println(sum);

}

public static void main(String[]args){

new Main();

}

class Sum implements Runnable{

public void run() {

m();

}

public void m(){

sum=sum+1;

}

}

}

See more about JAVA at brainly.com/question/12978370

#SPJ1

View image lhmarianateixeira