Synchronization in Java is the capability to control the access of multiple
threads to any shared resource.
Java Synchronization is better option where we want to allow only one thread
to access the shared resource.
The synchronization is mainly used to
1. To prevent thread interference.
2. To prevent consistency problem.
1. class Table{
2. void printTable(int n){//method not synchronized
3. for(int i=1;i<=5;i++){
4. System.out.println(n*i);
5. try{
6. Thread.sleep(400);
7. }catch(Exception e){System.out.println(e);}
8. }
9. }
10. }
11. class MyThread1 extends Thread{
12. Table t;
13. MyThread1(Table t){
14. this.t=t;
15. }
16. public void run(){
17. t.printTable(5);
18. }
19.
20. }
21. class MyThread2 extends Thread{
22. Table t;
23. MyThread2(Table t){
24. this.t=t;
25. }
26. public void run(){
27. t.printTable(100);
28. }
29. }
30.
31. class TestSynchronization1{
32. public static void main(String args[]){
33. Table obj = new Table();//only one object
34. MyThread1 t1=new MyThread1(obj);
35. MyThread2 t2=new MyThread2(obj);
36. t1.start();
37. t2.start();
38. }
39. }
5
100
10
200
15
300
20
400
25
500
Java Synchronized Method
If you declare any method as synchronized, it is known as synchronized
method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires the
lock for that object and releases it when the thread completes its task.
1. class Table{
2. synchronized void printTable(int n){//synchronized method
3. for(int i=1;i<=5;i++){
4. System.out.println(n*i);
5. try{
6. Thread.sleep(400);
7. }catch(Exception e){System.out.println(e);}
8. }
9.
10. }
11. }
12. class MyThread1 extends Thread{
13. Table t;
14. MyThread1(Table t){
15. this.t=t;
16. }
17. public void run(){
18. t.printTable(5);
19. }
20.
21. }
22. class MyThread2 extends Thread{
23. Table t;
24. MyThread2(Table t){
25. this.t=t;
26. }
27. public void run(){
28. t.printTable(100);
29. }
30. }
31. public class TestSynchronization2{
32. public static void main(String args[]){
33. Table obj = new Table();//only one object
34. MyThread1 t1=new MyThread1(obj);
35. MyThread2 t2=new MyThread2(obj);
36. t1.start();
37. t2.start();
38. }
39. }
Synchronized block can be used to perform synchronization on any specific
resource of the method.
Suppose we have 50 lines of code in our method, but we want to synchronize
only 5 lines, in such cases, we can use synchronized block.
If we put all the codes of the method in the synchronized block, it will work
same as the synchronized method.
o Synchronized block is used to lock an object for any shared resource.
o Scope of synchronized block is smaller than the method.
o A Java synchronized block doesn't allow more than one JVM, to provide
access control to a shared resource.
o The system performance may degrade because of the slower working
of synchronized keyword.
o Java synchronized block is more efficient than Java synchronized
method.
1. class Table
2. {
3. void printTable(int n){
4. synchronized(this){//synchronized block
5. for(int i=1;i<=5;i++){
6. System.out.println(n*i);
7. try{
8. Thread.sleep(400);
9. }catch(Exception e){System.out.println(e);}
10. }
11. }
12. }
Java provides a convenient way to group multiple threads in a single object. In such
a way, we can suspend, resume or interrupt a group of threads by a single method
call.
Java thread group is implemented by java.lang.ThreadGroup class.
A ThreadGroup represents a set of threads. A thread group can also include
the other thread group. The thread group creates a tree in which every
thread group except the initial thread group has a parent.
A thread is allowed to access information about its own thread group, but it
cannot access the information about its thread group's parent thread group
or any other thread groups.
ThreadGroup tg1 = new ThreadGroup("Group A");
Thread t1 = new Thread(tg1,new MyRunnable(),"one");
Thread t2 = new Thread(tg1,new MyRunnable(),"two");
Thread t3 = new Thread(tg1,new MyRunnable(),"three");
Now we can interrupt all threads by a single line of code only.
Thread.currentThread().getThreadGroup().interrupt();
public class ThreadGroupDemo implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
ThreadGroupDemo runnable = new ThreadGroupDemo();
ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup");
Thread t1 = new Thread(tg1, runnable,"one");
t1.start();
Thread t2 = new Thread(tg1, runnable,"two");
t2.start();
Thread t3 = new Thread(tg1, runnable,"three");
t3.start();
System.out.println("Thread Group Name: "+tg1.getName());
tg1.list();
}
}