Question

Suppose i want to make two threads . one thread A to be accessed multiple times by different proc...

suppose i want to make two threads . one thread A to be accessed multiple times by different processes and one time B to be accessed only one at a time.

please explain the concept and code by using synchronized() method keyword

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thread synchronization is the concurrent execution of two or more threads that share critical resources.

Synchronized Method: Includes the synchronized keyword in its declaration. When a thread invokes a synchronized method, synchronized method automatically acquires the intrinsic lock for that method's object and releases it when the method returns, even if that return was caused by an uncaught exception.

CODE

class Table{

synchronized void printTable(int n){ // synchronized method  

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

     System.out.println(n*i);  

     try{  

      Thread.sleep(500);  

     }catch(Exception e){System.out.println(e);}  

   }  

   }  

}  

class Addition{

void printAddition(){ /

int x=6,y=9,z;

z = x+y;

System.out.println(z);  

     try{  

      Thread.sleep(500);  

     }catch(Exception e){System.out.println(e);}  

   }  

}  

  class ThreadA extends Thread{  

Table t;  

Addition ad;

ThreadA(Table t){  

this.t=t;  

}

ThreadA(Addition ad){  

this.ad=ad;  

}

public void run(){  

t.printTable(5);

ad.printAddition();

}  

}  

class ThreadB extends Thread{  

Table t;  

ThreadB(Table t){  

this.t=t;  

}  

public void run(){  

t.printTable(100);  

}  

}  

  

public class SynchronizationMethod{  

public static void main(String args[]){  

Table obj = new Table(); //only one object

Addition obj1 = new Addition();   

ThreadA a = new ThreadA(obj);  

ThreadB b = new ThreadB(obj);  

   ThreadA an = new ThreadA(obj1);

a.start();  

b.start();  

an.start();

}  

}

Add a comment
Know the answer?
Add Answer to:
Suppose i want to make two threads . one thread A to be accessed multiple times by different proc...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of...

    //In this assignment, we use multiple threads to calculate the frequencies of the first digits //of the numbers in an array of long integers #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <assert.h> //#define NUM_THREADS 2 #define DIGITS 10 #define MAX 100000000 unsigned long a[MAX]; struct thread_data { int thread_num; int i, j;                           //the staring and ending index unsigned long freq[DIGITS];           // results }; //initialize the array void init_array(unsigned...

  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • 7. Multiple Choice Question Suppose that we want to compare the waiting times between repairs of...

    7. Multiple Choice Question Suppose that we want to compare the waiting times between repairs of two systems (in days), S, and Sy. Here are histograms of the waiting times: 4.8 3.6 Frequency 24 12 0.0 20 60 20 SI Aanbas 100 8 150 200 250 300 350 52 Is it reasonable to assume that A. the populations are normal with equal variances B. the populations are normal with unequal variances C. the populations are not normal D. only one...

  • When you want a loop to iterate exactly n times, you will typically use one of...

    When you want a loop to iterate exactly n times, you will typically use one of two standard for loops patterns (see p. 95-96): for (int i = 1; i <= n; i++) for (int i = 0; i < n; i++) Although both of these for loops will output n times, they have different initialization and test conditions.   Please give two *examples to describe why it is beneficial to have both patterns. (i.e. think of the initialization condition and...

  • In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE...

    In this assignment, you will implement a Memory Management System(MMS). Using C Programming Language..... MAKE SURE YOU USE C PROGRAMMING Your MMS will handle all requests of allocation of memory space by different users (one thread per user) …. HINT(You will use Pthreads and Semaphores). Your MMS will provide the user with an interface for making memory requests and also for freeing up memory that is no longer needed by the user. One of the jobs of your memory management...

  • answer asap please 2. (25 pts) One mole of a monoatomic ideal gas is initially at state i (P isobanic 150 kPa V-10.0L) and it is being expanded to 2.5 times its original volume along two different pro...

    answer asap please 2. (25 pts) One mole of a monoatomic ideal gas is initially at state i (P isobanic 150 kPa V-10.0L) and it is being expanded to 2.5 times its original volume along two different processes: £ d. #l isobaric and #2 isothermal. sothermal a) What are the final temperatures at the two different final states fi and fi? b) What is the amount of heat (Qi and ) absorbed by the gas in the two different processes?...

  • I want to this question solution by programming c code. I need this solution as soon...

    I want to this question solution by programming c code. I need this solution as soon as possible i have to submit this code after 24 hours. please someone help this question thanks :D Programming Question: (15pts) Assume a finite number of resources of a single resource type must be managed. Processes may ask for a number of these resources and will return them once finished. If all resources are in use the request is denied and the process terminates....

  • Suppose I want to make a list or contacts that allows me to lookup someone’s phone...

    Suppose I want to make a list or contacts that allows me to lookup someone’s phone number by first name. Suppose this list is Bill 385-353-1234 Rich 385-269-1234 Jane 801-352-1234 (A)Write a snippet of code that will store this information in a dictionary called “Contact” with each name as the key and corresponding phone number (as a string) as the value. You dont have to parse the data, just create a dictionary that has the information in it. (B) write...

  • I have two lines of h2 size of texts in my CSS style page. I want...

    I have two lines of h2 size of texts in my CSS style page. I want to Make 200% larger only for first letter for my 2nd text, but when I apply 200% larger font then it changes both text`s first letter to 200%. How can I apply for just one h2 text and get rid from the other one? Please provide with correct code.

  • Suppose that we want to compare the waiting times between repairs of two systems (in days),...

    Suppose that we want to compare the waiting times between repairs of two systems (in days), S, and S2. Here are histograms of the waiting times: 4.8 3.6 Frequency 2.4 1.2 0.0 20 30 40 50 60 70 S1 B 6 Frequency NU 0 50 100 150 200 250 300 350 S2 Is it reasonable to assume that A. the populations are normal with equal variances B. the populations are normal with unequal variances C. the populations are not normal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT