Click here to watch in Youtube :
https://www.youtube.com/watch?v=vexva-R2xOo&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial: Java Synchronization (Static synchronization block | Synchronization in java) |
class Table { static void printTable(int n) { /* * The block synchronizes on the lock of the object * denoted by the reference .class name .class */ synchronized (Table.class) { System.out.println(Thread.currentThread().getName()); for (int i = 1; i <= 5; i++) { System.out.println(n * i); try { Thread.sleep(400); } catch (Exception e) { } } System.out.println("------------------------"); } } } public class StaticSynchronizationDemo { public static void main(String t[]) { Thread t1 = new Thread() { public void run() { Table.printTable(1); } }; Thread t2 = new Thread() { public void run() { Table.printTable(10); } }; t1.start(); t2.start(); } }Output
Thread-0 1 2 3 4 5 ------------------------ Thread-1 10 20 30 40 50 ------------------------Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_static_synchronization_block_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ThreadDemo_static_synchronization_block_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d83d752fbc13c2d1cd6c6655b89d6e57fdda2f22/BasicJava/ThreadDemo_static_synchronization_block_App/?at=master
See also:
No comments:
Post a Comment