博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多线程-同步代码块
阅读量:4231 次
发布时间:2019-05-26

本文共 2822 字,大约阅读时间需要 9 分钟。

如果一个对象作为同步代码块的对象,那么该对象同步的代码段和该对象内部通过同步方法或者自身对象的代码段会被同步。

看例子:

public class OutClass {    static class InnerClass1{        public void method1(InnerClass2 class2){            String threadName=Thread.currentThread().getName();            synchronized (class2){                System.out.println(threadName+" 进入InnerClass1类中的method1方法");                for(int i=0;i<10;i++){                    System.out.println("i="+i);/*                    try {                        //Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }*/                }                System.out.println(threadName+" 离开InnerClass1类中的method1方法");            }        }        public synchronized void method2(){            String threadName=Thread.currentThread().getName();            System.out.println(threadName+" 进入InnerClass1类中的method2方法");            for(int j=0;j<10;j++){                System.out.println("i="+j);                try {                    Thread.sleep(100);                } catch (InterruptedException e) {                }            }            System.out.println(threadName+" 离开InnerClass1类中的method2方法");        }    }    static class InnerClass2{        public synchronized void method1(){            String threadName=Thread.currentThread().getName();            System.out.println(threadName+" 进入InnerClass2类中的method1方法");            for(int k = 0;k<10;k++  ){                System.out.println("k="+k);                try {                    Thread.sleep(100);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            System.out.println(threadName+" 离开InnerClass2类中的method1方法");        }    }}
public class Run {    public static void main(String[] args){        final OutClass.InnerClass1 in1=new OutClass.InnerClass1();        final OutClass.InnerClass2 in2=new OutClass.InnerClass2();        Thread t1=new Thread(new Runnable() {            @Override            public void run() {                in1.method1(in2);            }        },"T1");        Thread t2=new Thread(new Runnable() {            @Override            public void run() {                in1.method2();            }        },"T2");        Thread t3=new Thread(new Runnable() {            @Override            public void run() {                in2.method1();            }        },"T3");        t1.start();        t2.start();        t3.start();    }}
T2 进入InnerClass1类中的method2方法
T1 进入InnerClass1类中的method1方法
i=0
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
T1 离开InnerClass1类中的method1方法
T3 进入InnerClass2类中的method1方法
k=0
k=1
i=1
i=2
k=2
i=3
k=3
k=4
i=4
k=5
i=5
i=6
k=6
k=7
i=7
i=8
k=8
k=9
i=9
T2 离开InnerClass1类中的method2方法
T3 离开InnerClass2类中的method1方法

in2.method1方法一定要等到in1.method1方法执行完后才执行。

转载地址:http://eljqi.baihongyu.com/

你可能感兴趣的文章
Pro JSP 2, Fourth Edition (Expert's Voice in Java)
查看>>
Microsoft SQL Server 2005 Reporting Services 2005
查看>>
User Mode Linux(R) (Bruce Perens Open Source)
查看>>
Enterprise JavaBeans 3.0 (5th Edition)
查看>>
Eclipse 3 Live
查看>>
Java P2P Unleashed: With JXTA, Web Services, XML, Jini, JavaSpaces, and J2EE
查看>>
Java(TM) Network Programming and Distributed Computing
查看>>
Java Cookbook
查看>>
Intelligent Agent Software Engineering
查看>>
Project Management Training
查看>>
Microsoft Reporting Services in Action
查看>>
Successful Software Development (2nd Edition)
查看>>
How to Design Programs: An Introduction to Programming and Computing
查看>>
Beginning Relational Data Modeling, Second Edition
查看>>
Winternals Defragmentation, Recovery, and Administration Field Guide
查看>>
Video Conferencing over IP : Configure, Secure, and Troubleshoot
查看>>
Pro ASP.NET 2.0 Website Programming
查看>>
Disassembling Code : IDA Pro and SoftICE
查看>>
Building Online Communities With Drupal, phpBB, and WordPress
查看>>
C# 2.0 : The Complete Reference
查看>>