Java Program on Block Synchronization
Free Java courses with 37 real-time projects - Learn Java
Program 1
// Outer resource method package dataflair; public class MyTable { void showTable(int n) { System.out.println("Data Flair Free Coruse"); System.out.println("Wel come you all in Data Flair Free Coruse"); synchronized(this) { try { int i; for(i=1;i<=10;i++) { System.out.println(n*i); Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println(e); } } System.out.println("Hello MyTable"); System.out.println("This is Block code"); } }
Program 2
// User defefine thread package dataflair; public class MyThread extends Thread { private MyTable M; private int n; MyThread(MyTable M,int n) { this.M=M; this.n=n; } public void run() { M.showTable(n); } }
Program 3
package dataflair; public class TestMain { public static void main(String[] args) throws InterruptedException { MyTable M=new MyTable(); MyThread mt1=new MyThread(M, 5); MYThread1 mt2=new MYThread1(M, 8); mt1.start(); mt2.start(); mt1.join(); mt2.join(); } }
Program 4
// User defefine thread package dataflair; public class MYThread1 extends Thread { private MyTable M; private int n; MYThread1(MyTable M,int n) { this.M=M; this.n=n; } public void run() { M.showTable(n); } }
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google