Java教程

Java Synchronization

Java Synchronization是控制多个线程对任何共享资源的访问的功能。
如果要只允许一个线程访问共享资源,则Java同步是更好的选择。

为什么使用Synchronization

同步主要用于
防止线程干扰。 为防止一致性问题。

同步类型

有两种同步类型
进程同步 线程同步
在这里,我们将只讨论线程同步。

线程同步

有两种类型的线程同步: 互斥和线程间通信。
互斥 同步方法。 同步块。 静态同步。 合作(java中的线程间通信)

互斥

互斥有助于防止线程在共享数据时相互干扰。这可以通过Java中的三种方式完成:
通过同步方法 通过同步块 通过静态同步

Java中的锁概念

同步是围绕称为锁或监视器的内部实体建立的。每个对象都有与之关联的锁。按照惯例,需要对对象的字段进行一致访问的线程必须在访问对象之前获取对象的锁,然后在完成对它们的锁定后释放该锁。
从Java 5开始,包java.util.concurrent.locks包含多个锁实现。

了解没有同步的问题

在此示例中,没有同步,因此输出不一致。让我们看一下示例:
class Table{
    void printTable(int n){
        //method not synchronized 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);
    }
}
class TestSynchronization1{
    public static void main(String args[]){
        Table obj = new Table();
        //only one objectMyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start();
        t2.start();
    }
}
Output: 5 100 10 200 15 300 20 400 25 500

Java同步方法

如果将任何方法声明为同步方法,则称为同步方法。
同步方法用于锁定
当线程调用同步方法时,它会自动获取该对象的锁,并在线程完成其任务时释放该锁。
//example of java synchronized methodclass 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 objectMyThread1 t1=new MyThread1(obj);
        MyThread2 t2=new MyThread2(obj);
        t1.start();
        t2.start();
    }
}
Output: 5 10 15 20 25 100 200 300 400 500

使用匿名类的同步方法示例

在此程序中,我们通过匿名类创建了两个线程,因此所需的编码更少。
//Program of synchronized method by using annonymous classclass 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);
            }
        }
    }
}
public class TestSynchronization3{
    public static void main(String args[]){
        final Table obj = new Table();
        //only one objectThread t1=new Thread(){
            public void run(){
                obj.printTable(5);
            }
        }
        ;
        Thread t2=new Thread(){
            public void run(){
                obj.printTable(100);
            }
        }
        ;
        t1.start();
        t2.start();
    }
}
Output: 5 10 15 20 25 100 200 300 400 500

Java线程同步知识

Java 同步块 Java 线程静态同步 Java 线程死锁 Java 线程间通信 Java 中断线程
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4