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 :: java enum values() 
Java :: javafx access fxml elements 
Java :: java to c++ converter 
Java :: Linked list implementation in Java source code 
Java :: types of classes in java 
Java :: create thread 
Java :: can you use java in unity 
Java :: java float 
Java :: taglib in jsp 
Java :: connection data base java code 
Java :: Android java toArray to String array 
Java :: my animal list 
Java :: java forcing user to input int 
Java :: how can i put infirmation of arraylist in a dropdown java 
Sql :: sql server drop temp table if exists 
Sql :: alter session set nls_date_format 
Sql :: convert utc to est sql 
Sql :: forgot my mysql password mac 
Sql :: query for all indexes in table postgres 
Sql :: show tables sql server 
Sql :: Remove mySQL from ubuntu 20.x 
Sql :: mysql group by month 
Sql :: mysql ALTER TABLE ADD COLUMN BOOLEAN AFTER DEFAULT "1"; 
Sql :: mysql users and privileges list 
Sql :: how to remove characters from string in mysql 
Sql :: mysql text to decimal 
Sql :: tsql get beginning of year 
Sql :: microsoft sql server extract hour and minute from datetime 
Sql :: change varchar length mysql 
Sql :: ORA-01950 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =