zoukankan      html  css  js  c++  java
  • Java的线程同步

    synchronized获取的锁是对象,而不是函数或语句块。

    项目结构

    资源类

    import java.util.concurrent.TimeUnit;
    
    public class myResource {
        public void x(){
            System.out.println(Thread.currentThread().getName()+" :等待进入 synchronized in x方法");
            synchronized (this) {
                for(int i = 0;i<10;i++){
                    System.out.println(Thread.currentThread().getName()+" : synchronized in x方法");
                    try {
                        TimeUnit.MILLISECONDS.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public void y(){
            System.out.println(Thread.currentThread().getName()+" :等待进入 synchronized in y方法");
            synchronized (this) {
                for(int i = 0;i<10;i++){
                    System.out.println(Thread.currentThread().getName()+" : synchronized in y方法");
                    try {
                        TimeUnit.MILLISECONDS.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public void z(){
            System.out.println(Thread.currentThread().getName()+" :等待进入 synchronized in z方法");
            synchronized (this) {
                for(int i = 0;i<10;i++){
                    System.out.println(Thread.currentThread().getName()+" : synchronized in z方法");
                    try {
                        TimeUnit.MILLISECONDS.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    运行类

    public class Test {
    
        public static void main(String[] args) {
            myResource resource = new myResource();
            // 线程0
            new Thread(){
                public void run() {
                    resource.x();
                };
            }.start();
            // 线程1
            new Thread(){
                public void run() {
                    resource.y();
                };
            }.start();
            // 线程main
            resource.z();
        }
    
    }

    运行结果

    Thread-0 :等待进入 synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-1 :等待进入 synchronized in y方法
    main :等待进入 synchronized in z方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    Thread-0 : synchronized in x方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    main : synchronized in z方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
    Thread-1 : synchronized in y方法
  • 相关阅读:
    记一个SharePoint Workflow一睡永不醒来的问题
    .NET Tracing简介
    !aspxpages(即!dumphttpcontext)命令输出的解释
    MOSS定时爬网无法启动的问题
    Persistent cookies和Session cookies的定义与区别
    跨SharePoint服务器场的Content Deployment的一个知识点
    如何明确指定命令所在的debugger extension?
    理解SharePoint中的备用访问映射(Alternate Access Mapping)
    biztalk中ACK、NACK详测示例【转】
    深入biztalk消息以及消息订阅发布路由机制(一)-消息概述【转】
  • 原文地址:https://www.cnblogs.com/sea-breeze/p/7018442.html
Copyright © 2011-2022 走看看