Write a program in java to demonstrate multi-threading problem?

Multi-threading problem –

class Table

{

      public void printTable(int n)

      {

            for(int i=1;i<=10;i++)

            {

                  System.out.println(n*i);

                  try

                  {

                        Thread.sleep(1000);

                  }

                  catch(Exception e)

                  {}

            }

      }

}

class MyThread1 extends Thread

{

      Table t;

      public MyThread1(Table t)

      {

            this.t=t;

      }

      public void run()

      {

            t.printTable(6);

      }

}

class MyThread2 extends Thread

{

      Table t;

      public MyThread2(Table t)

      {

            super();

            this.t=t;

      }

      public void run()

      {

            t.printTable(10);

      }

}

public class MyThreadTestProblem

{

      public static void main(String arg[])

      {

            Table ta=new Table();

            MyThread1 t1=new MyThread1(ta);

            MyThread2 t2=new MyThread2(ta);

            t1.start();

            t2.start();

      }

}

Multi-Threading Problem.PNG

Multi-threading problem to solution in using synchronized method –

class Table

{

      public synchronized void printTable(int n)

      {

            for(int i=1;i<=10;i++)

            {

                  System.out.println(n*i);

                  try

                  {

                        Thread.sleep(1000);

                  }

                  catch(Exception e)

                  {}

            }

      }

}

class MyThread1 extends Thread

{

      Table t;

      public MyThread1(Table t)

      {

            this.t=t;

      }

      public void run()

      {

            t.printTable(6);

      }

}

class MyThread2 extends Thread

{

      Table t;

      public MyThread2(Table t)

      {

            super();

            this.t=t;

      }

      public void run()

      {

            t.printTable(10);

      }

}

public class MyThreadTestProblem

{

      public static void main(String arg[])

      {

            Table ta=new Table();

            MyThread1 t1=new MyThread1(ta);

            MyThread2 t2=new MyThread2(ta);

            t1.start();

            t2.start();

      }

}

Multi-threading problem to solution in using synchronized method.PNG

Multi-threading problem to solution in using synchronized block –

class Table

{

      public void printTable(int n)

      {

            synchronized(this)

            {

                  for(int i=1;i<=10;i++)

                  {

                        System.out.println(n*i);

                        try

                        {

                              Thread.sleep(1000);

                        }

                        catch(Exception e)

                        {}

                  }

            }

      }

}

class MyThread1 extends Thread

{

      Table t;

      public MyThread1(Table t)

      {

            this.t=t;

      }

      public void run()

      {

            t.printTable(6);

      }

}

class MyThread2 extends Thread

{

      Table t;

      public MyThread2(Table t)

      {

            super();

            this.t=t;

      }

      public void run()

      {

            t.printTable(10);

      }

}

public class MyThreadTestProblem

{

      public static void main(String arg[])

      {

            Table ta=new Table();

            MyThread1 t1=new MyThread1(ta);

            MyThread2 t2=new MyThread2(ta);

            t1.start();

            t2.start();

      }

}

Multi-threading problem to solution in using synchronized block.PNG

Multi-threading problem to solution in using static synchronized method –

class Table

{

      public static synchronized void printTable(int n)

      {

            for(int i=1;i<=10;i++)

            {

                  System.out.println(n*i);

                  try

                  {

                        Thread.sleep(1000);

                  }

                  catch(Exception e)

                  {}

            }

      }

}

class MyThread1 extends Thread

{

      Table t;

      public MyThread1(Table t)

      {

            this.t=t;

      }

      public void run()

      {

            Table.printTable(6);

      }

}

class MyThread2 extends Thread

{

      Table t;

      public MyThread2(Table t)

      {

            super();

            this.t=t;

      }

      public void run()

      {

            Table.printTable(9);

      }

}

class MyThread3 extends Thread

{

      Table t;

      public MyThread3(Table t)

      {

            super();

            this.t=t;

      }

      public void run()

      {

            Table.printTable(5);

      }

}

class MyThread4 extends Thread

{

      Table t;

      public MyThread4(Table t)

      {

            super();

            this.t=t;

      }

      public void run()

      {

            Table.printTable(7);

      }

}

public class MyThreadTestProblem

{

      public static void main(String arg[])

      {

            Table ta1=new Table();

            Table ta2=new Table();

            MyThread1 t1=new MyThread1(ta1);

            MyThread2 t2=new MyThread2(ta1);

            MyThread3 t3=new MyThread3(ta2);

            MyThread4 t4=new MyThread4(ta2);

            t1.start();

            t2.start();

            t3.start();

            t4.start();

      }

}

Multi-threading problem to solution in using static synchronized method.PNG

, , , , , , , ,

Leave a Reply