• synchronized 同步对象概念


     


    解决上述问题之前,先理解
    synchronized关键字的意义
    如下代码:
     
    Object someObject =new Object();
    synchronized (someObject){
    //此处的代码只有占有了someObject后才可以执行
    }
     


    synchronized表示当前线程,独占 对象 someObject
    当前线程独占 了对象someObject,如果有其他线程试图占有对象someObject,就会等待,直到当前线程释放对someObject的占用。
    someObject 又叫同步对象,所有的对象,都可以作为同步对象
    为了达到同步的效果,必须使用同一个同步对象

    释放同步对象的方式: synchronized 块自然结束,或者有异常抛出
    synchronized 同步对象概念
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    package multiplethread;
      
    import java.text.SimpleDateFormat;
    import java.util.Date;
       
    public class TestThread {
         
        public static String now(){
            return new SimpleDateFormat("HH:mm:ss").format(new Date());
        }
         
        public static void main(String[] args) {
            final Object someObject = new Object();
              
            Thread t1 = new Thread(){
                public void run(){
                    try {
                        System.out.println( now()+" t1 线程已经运行");
                        System.out.println( now()+this.getName()+ " 试图占有对象:someObject");
                        synchronized (someObject) {
                              
                            System.out.println( now()+this.getName()+ " 占有对象:someObject");
                            Thread.sleep(5000);
                            System.out.println( now()+this.getName()+ " 释放对象:someObject");
                        }
                        System.out.println(now()+" t1 线程结束");
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t1.setName(" t1");
            t1.start();
            Thread t2 = new Thread(){
      
                public void run(){
                    try {
                        System.out.println( now()+" t2 线程已经运行");
                        System.out.println( now()+this.getName()+ " 试图占有对象:someObject");
                        synchronized (someObject) {
                            System.out.println( now()+this.getName()+ " 占有对象:someObject");
                            Thread.sleep(5000);
                            System.out.println( now()+this.getName()+ " 释放对象:someObject");
                        }
                        System.out.println(now()+" t2 线程结束");
                    catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t2.setName(" t2");
            t2.start();
        }
           
    }
  • 相关阅读:
    C++命名规则
    protobuf_1
    以太网帧格式
    LinQ
    asp.mvc 基本知识
    Lucene.Net 优化索引生成,即搜索显示优化
    HTML Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用
    DataSet
    伪Excel导出新版代码
    WebUI 常用
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10194370.html
Copyright © 2020-2023  润新知