Question

Write a program that launches 50 threads. Each thread should add one to a variable named...

Write a program that launches 50 threads. Each thread should add one to a variable named sum that is initialized to zero. Run the program with and without synchronization to see its effect.

IN JAVA PLEASE

ALSO PLEASE SPECIFY HOW TO RUN THIS PROGRAM - WITH AND WITHOUT SYNCHRONIZATION

THANK YOU

Homework Answers

Answer #1

Program without Synchronization

class Demo implements Runnable {
   static int sum = 0;// static sum variable

   @Override
   public void run() {
       ++sum;// adding 1 to sum
   }
}

public class Test {

   public static void main(String[] args) {
       Thread[] ar = new Thread[50];// creating an array of 50 threads
       for (int i = 0; i < 50; i++) {
           // initilizing the array
           ar[i] = new Thread(new Demo());
       }
       long start = System.nanoTime();// getting start time
       for (int i = 0; i < 50; i++) {
           ar[i].start();// starting the threads
       }
       long time = System.nanoTime() - start;// calculating time taken in nanoseconds
       System.out.printf("Took %.1f ms to complete the task\n", time / 1e6);// showing time in milliseconds
       System.out.println("Sum: " + Demo.sum);// showing sum value
   }
}

Program with Synchronization

class Demo implements Runnable {
   static int sum = 0;// static sum variable

   @Override
   public synchronized void run() {//synchronized method
       ++sum;// adding 1 to sum
   }
}

public class Test {

   public static void main(String[] args) {
       Thread[] ar = new Thread[50];// creating an array of 50 threads
       for (int i = 0; i < 50; i++) {
           // initilizing the array
           ar[i] = new Thread(new Demo());
       }
       long start = System.nanoTime();// getting start time
       for (int i = 0; i < 50; i++) {
           ar[i].start();// starting the threads
       }
      
       long time = System.nanoTime() - start;// calculating time taken in nanoseconds
       System.out.printf("Took %.1f ms to complete the task\n", time / 1e6);// showing time in milliseconds
       System.out.println("Sum: " + Demo.sum);// showing sum value
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
program in C with one parent thread and two child threads. Both child threads work on...
program in C with one parent thread and two child threads. Both child threads work on the single global variable sum. Each of them implements one for loop, and the for loop is designed to complete 10000 iterations. • One child thread add 1 to sum in each iteration of the for loop. starting with 0 and finishing with 10000. • The other child thread, decrements on 1 the value of sum in each iteration of the for loop.
Solve the following using java Write a program that runs three threads, each thread randomizes a...
Solve the following using java Write a program that runs three threads, each thread randomizes a number between 1 and 100. The main thread waits for all the others to finish, calculates the maximum of the numbers, which were randomized, and prints it. Random number 1: 10 Random number 2: 38 Random number 3: 81 Max: 81 Note: You should create 3 threads in adition to the main thread. Also, you can use a single thread class and create 3...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
Complete the following program. This program should do the following: 1. Creates a random integer in...
Complete the following program. This program should do the following: 1. Creates a random integer in the range 10 to 15 for variable: allThreads, to create a number of threads. 2. Creates a random integer for the size of an ArrayList: size. 3. Each thread obtains a smallest number of a segment of the array. To give qual sized segment to each thread we make the size of the array divisible by the number of threads: while(size%allThreads != 0)size++ 4....
**C++** a. Write a program containing the following expressions. x should be an int variable. Add...
**C++** a. Write a program containing the following expressions. x should be an int variable. Add a statement after each one to print out the current value of x in the following format (of course, you will print the value of x, not the constant 4!): Problem 1. x has the value 4 1. x = 4; 2. x = 2; 3. x = 24 – 6 * 2; (show each computation) 4. x = -15 * 2 + 3;...
JAVA use the concepts in concurrent programming to write a basic multi-threaded program. You will be...
JAVA use the concepts in concurrent programming to write a basic multi-threaded program. You will be creating a program that simulates the sound of soldiers marching: “Left, Left, Left, Right, Left”. You will need one class to print “Left” and one to print “Right”, as well as a class to drive the program. LeftThread.java public class LeftThread extends Thread { public void run() { for(int i = 0; i < 10; i++) { //TODO: Print "Left" //TODO: Print "Left" //TODO:...
1. Create a new project. Type in the following program. Add a comment at the top...
1. Create a new project. Type in the following program. Add a comment at the top to include your name and the date. 2. Compile and Run with different values. What data values should you choose? Think about what we discussed in class. 3. Add code to compute the modulus. Hint: you will also have to declare a new variable. //this program demonstrates the use of various operators #include <iostream > using namespace std; int main() { int num1; int...
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT