❮ Previous Next ❯

Java Static synchronization:

In simple words a static syncronized method will lock the class instead of the object, and it will lock the class because the keyword static means: "class instead of instance". The keyword syncronized means that only one thread can access the method at a time.

Example

class Test
{
void show(int n)
{
for(int i=1;i<=3;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e)
{
}
}
}
}
class MyThread1 extends Thread
{
public void run()
{
Test.show(2);
}
}
class MyThread2 extends Thread
{
public void run()
{
Test.show(3); }
class MyThread3 extends Thread3
{
public void run()
{
Test.show(4);
}
}
class MyThread4 extends Thread
{
public void run()
{
Test.show(5);
}
}
class StaticSynchronizeDemo
{
public static void main(String[] args)
{
MyThread1 ob1= new MyThread1();
MyThread2 ob2= new MyThread2();
MyThread3 ob3= new MyThread3();
MyThread4 ob4= new MyThread4();
ob1.start();
ob2.start();
ob3.start();
ob4.start();
}
}
Output

2
4
6
3
6
9
4
8
12
5
10
15

❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved