Multithreading in Java
Multithreading in Java
• Java
// Main Class
public class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();
}
}
}
• Java
// Main Class
class Multithread {
public static void main(String[] args)
{
int n = 8; // Number of threads
for (int i = 0; i < n; i++) {
Thread object
= new Thread(new MultithreadingDemo());
object.start();
}
}
}
Synchronization in Java
• Multi-threaded programs may often come to a situation where
multiple threads try to access the same resources and finally produce
erroneous and unforeseen results.
• Java Synchronization is used to make sure by some synchronization
method that only one thread can access the resource at a given point
in time.
• Types of Synchronization
There are two synchronizations in Java mentioned below:
1. Process Synchronization
2. Thread Synchronization
Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while
sharing data. There are three types of Mutual Exclusive mentioned below:
• Synchronized method.
• Synchronized block.
• Static synchronization.
The volatile keyword does not cache the value of the variable and always read the
variable from the main memory. The volatile keyword cannot be used with classes or
methods. However, it is used with variables. It also guarantees visibility and ordering.
It prevents the compiler from the reordering of code.
The contents of the particular device register could change at any time, so you need
the volatile keyword to ensure that such accesses are not optimized away by the
compiler.
o You can use a volatile variable if you want to read and write long and double
variable automatically.
o It can be used as an alternative way of achieving synchronization in Java.
o All reader threads will see the updated value of the volatile variable after
completing the write operation. If you are not using the volatile keyword,
different reader thread may see different values.
o It is used to inform the compiler that multiple threads will access a particular
statement. It prevents the compiler from doing any reordering or any
optimization.
o If you do not use volatile variable compiler can reorder the code, free to write
in cache value of volatile variable instead of reading from the main memory.
TestSynchronization1.java
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. }
12.
13. class MyThread1 extends Thread{
14. Table t;
15. MyThread1(Table t){
16. this.t=t;
17. }
18. public void run(){
19. t.printTable(5);
20. }
21.
22. }
23. class MyThread2 extends Thread{
24. Table t;
25. MyThread2(Table t){
26. this.t=t;
27. }
28. public void run(){
29. t.printTable(100);
30. }
31. }
32.
33. class TestSynchronization1{
34. public static void main(String args[]){
35. Table obj = new Table();//only one object
36. MyThread1 t1=new MyThread1(obj);
37. MyThread2 t2=new MyThread2(obj);
38. t1.start();
39. t2.start();
40. }
41. }
Output:
5
100
10
200
15
300
20
400
25
500
When a thread invokes a synchronized method, it automatically acquires the lock for
that object and releases it when the thread completes its task.
TestSynchronization2.java
Output:
5
10
15
20
25
100
200
300
400
500