Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

synchronized java

synchronized blocks the next thread's call to method 
 as long as the previous thread's execution is not finished. 
  Threads can access this method one at a time. 
  Without synchronized all threads can access this method simultaneously.
Comment

synchronized block java

public class MyCounter {

  private int count = 0;

  public synchronized void add(int value){
      this.count += value;
  }
}
Comment

Java Synchronized Method

//example of java synchronized method  
class Table{  
 synchronized void printTable(int n){//synchronized method  
   for(int i=1;i<=5;i++){  
     System.out.println(n*i);  
     try{  
      Thread.sleep(400);  
     }catch(Exception e){System.out.println(e);}  
   }  
  
 }  
}  
  
class MyThread1 extends Thread{  
Table t;  
MyThread1(Table t){  
this.t=t;  
}  
public void run(){  
t.printTable(5);  
}  
  
}  
class MyThread2 extends Thread{  
Table t;  
MyThread2(Table t){  
this.t=t;  
}  
public void run(){  
t.printTable(100);  
}  
}  
  
public class TestSynchronization2{  
public static void main(String args[]){  
Table obj = new Table();//only one object  
MyThread1 t1=new MyThread1(obj);  
MyThread2 t2=new MyThread2(obj);  
t1.start();  
t2.start();  
}  
}  
Comment

Synchronized Method in Java

// Example illustrates multiple threads are executing
// on the same Object at same time without synchronization.
import java.io.*;
  
class Line
{
    // if multiple threads(trains) will try to
    // access this unsynchronized method,
    // they all will get it. So there is chance
    // that Object's  state will be corrupted.
    public void getLine()
    {
        for (int i = 0; i < 3; i++)
        {
            System.out.println(i);
            try
            {
                Thread.sleep(400);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
        }
    }
}
  
class Train extends Thread
{
    // reference to Line's Object.
    Line line;
  
    Train(Line line)
    {
        this.line = line;
    }
  
    @Override
    public void run()
    {
        line.getLine();
    }
}
  
class GFG
{
    public static void main(String[] args)
    {
        // Object of Line class that is shared
        // among the threads.
        Line obj = new Line();
  
        // creating the threads that are
        // sharing the same Object.
        Train train1 = new Train(obj);
        Train train2 = new Train(obj);
  
        // threads start their execution.
        train1.start();
        train2.start();
    }
}
Comment

Synchronized Block in Java

class Table  
{      
 void printTable(int n){    
   synchronized(this){//synchronized block    
     for(int i=1;i<=5;i++){    
      System.out.println(n*i);    
      try{    
       Thread.sleep(400);    
      }catch(Exception e){System.out.println(e);}    
     }    
   }    
 }//end of the method    
}    
    
class MyThread1 extends Thread{    
Table t;    
MyThread1(Table t){    
this.t=t;    
}    
public void run(){    
t.printTable(5);    
}    
    
}    
class MyThread2 extends Thread{    
Table t;    
MyThread2(Table t){    
this.t=t;    
}    
public void run(){    
t.printTable(100);    
}    
}    
    
public class TestSynchronizedBlock1{    
public static void main(String args[]){    
Table obj = new Table();//only one object    
MyThread1 t1=new MyThread1(obj);    
MyThread2 t2=new MyThread2(obj);    
t1.start();    
t2.start();    
}    
}    
Comment

PREVIOUS NEXT
Code Example
Java :: Rotate Left k cells java 
Java :: run jar with logback config file 
Java :: java 8 validate based on pair of strings 
Java :: compile time exception in java 
Java :: Java TestNG Data provider Provider with multiple variables 
Java :: how to find the size of table in java 
Java :: change replication factor hadoop cluster command 
Java :: Algorithms - decision 
Java :: system.out.println(h [2] [1] [1] [0]); 
Java :: check whether an entry in hashmap is deleted in java 
Java :: Java Get float, double and String Input 
Java :: exmple of methods in java 
Java :: docker how to pass params to spring boot application 
Java :: Java Numbers and Strings 
Java :: java circular buffer implementation on array 
Java :: How to adjust layout when soft keyboard appears 
Java :: how do I change the opacity of a JButton 
Java :: java map get value 
Java :: how to create a 2d arraylist java 
Java :: if statement with string java 
Java :: java character in string 
Java :: java tutorialspoint 
Java :: java stream group by multiple fields 
Java :: java division of 2 numbers with decimal places 
Java :: how to find a string in a sentence in java 
Java :: im retarded 
Java :: how can i put infirmation of arraylist in a dropdown java 
Sql :: mysql create user with remote access 
Sql :: Port 5432 is already in use Usually this means that there is already a PostgreSQL server running on your Mac. If you want to run multiple servers simultaneously, use different ports. 
Sql :: uninstall mysql on ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =