Home > front end >  In this synchronization is not working. Why? I have synchronized all methods. Can anyone please tell
In this synchronization is not working. Why? I have synchronized all methods. Can anyone please tell

Time:05-07

Write a program to print tables with the help of multiple threads create three threads named as T1, T2 and T3. T1 thread prints the table of 11 and 12, T2 prints table of 13 and 14 and T3 prints table of 15 and 16. Any thread can start first but once any one starts to print table of any number it is not allowed to print different table by another thread. For example, suppose thread t3 starts first and start printing 15 table now until table of 15 gets completed no one prints another table. once the printing of 15 table has ended any one allowed to print another table.

class A implements Runnable{
    public void run(){
        first();
        second();


    }
    synchronized public void first(){
        for(int i=0;i<=10;i  ){
            System.out.println("Table of 11 is " 11*i);
        }
    }
    synchronized public void second(){
        for(int i=0;i<=10;i  ){
            System.out.println("Table of 12 is " 12*i);
        }
    }
   
}
class B implements Runnable{
    public void run(){
        third();
        fourth();


    }
    synchronized public void third(){
        for(int i=0;i<=10;i  ){
            System.out.println("Table of 13 is " 13*i);
        }
    }
    synchronized public void fourth(){
        for(int i=0;i<=10;i  ){
            System.out.println("Table of 14 is " 14*i);
        }
    }
}
class C implements Runnable{
    public void run(){
        fifth();
        sixth();


    }
    synchronized public void fifth(){
        for(int i=0;i<=10;i  ){
            System.out.println("Table of 15 is " 15*i);
        }
    }
    synchronized public void sixth(){
        for(int i=0;i<=10;i  ){
            System.out.println("Table of 16 is " 16*i);
        }
    }
}




public class threading3 {
    public static void main(String args[]){
        A a1=new A();
        B b1=new B();
        C c1=new C();
        Thread t1=new Thread(a1);
        Thread t2=new Thread(b1);
        Thread t3=new Thread(c1);
        t1.start();
        t2.start();
        t3.start();

    }
    
}

Its printing table in random way.Even though i used synchronized in all methods.

CodePudding user response:

A synchronized method will prevent other threads to enter into a synchronized block on the same object the method is synchronized on. Each thread has two methods in your example, so for instance while first is running, second cannot run, but there is nothing to prevent third from running.

You need to synchronize all methods on the same object:

Object s=new Object();
A a1=new A(s);
B b1=new B(s);
C c1=new C(s);
class A implements Runnable{
  Object s;
  public A(s Object) {
     this.s=s;
  }
  public void first(){
      synchronized(s) {
         for(int i=0;i<=10;i  ){
             System.out.println("Table of 13 is " 13*i);
         }
      }
  }
  • Related