Discover the answers to your questions at Westonci.ca, where experts share their knowledge and insights with you. Experience the ease of finding accurate answers to your questions from a knowledgeable community of professionals. Join our platform to connect with experts ready to provide precise answers to your questions in different areas.

(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
Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. Thank you for your visit. We're dedicated to helping you find the information you need, whenever you need it. Discover more at Westonci.ca. Return for the latest expert answers and updates on various topics.