Write a program to print table using Threads (synchronization).
The following code will help you to write a table using threads.
Table of Contents
Java Program Code
/*;==========================================
; Title: Write a program to print table using Threads (synchronization).
; Author: codenaive littleboy8506@
; Date: 13 Dec 2021
;==========================================*/
class Table
{
void tab(int n)
{
for(int i=1;i<=10;i++)
{
System.out.println(n+" x "+i+" = " +n*i);
try
{
Thread.sleep(400);
}
catch(Exception e)
{
}
}
}
}
class First extends Thread{
Table t;
First(Table t){
this.t=t;
}
synchronized public void run(){
t.tab(9); // here is the input example : 9
}
}
class _2nd extends Thread{
Table t;
_2nd(Table t){
this.t=t;
}
public void run(){
t.tab(5); // here is the 2nd input example : 5
}
}
class TableC
{
public static void main(String ar[])
{
Table tab=new Table();
First obj=new First(tab);
// _2nd o=new _2nd(tab);
obj.start();
//o.start();
}
}
Output
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
Leave a Reply