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方法
  • 相关阅读:
    java 实现串口通信
    下载 Microsoft .NET Framework 4和4.5.2(独立安装程序)
    vm15.5.1安装Mac os X
    开源网盘系统
    Photoshop另存为图片为.ico格式(图标文件)的插件
    VeraCrypt(文件加密工具)创建和加载“文件型加密卷”的步骤
    强制浏览器字体为宋体或其他指定的字体
    超微主板风扇模式
    通过URL获取该页面内的所有超链接
    photoshop新建时提示“无法完成请求,因为暂存盘已满”的解决方案
  • 原文地址:https://www.cnblogs.com/sea-breeze/p/7018442.html
Copyright © 2011-2022 走看看