内部类与同步

package 对象及变量的并发访问2;

/**
 * 内部类与同步-1
 */
class OutClassT2221{

    static class Inner{
        public void method1(){
            synchronized ("其他的锁"){
                for (int i=1;i<=10;++i){
                    System.out.println(Thread.currentThread().getName()+" i= "+(i+1));
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        public synchronized void Method2(){
            for (int i=11;i<=20;i++){
                System.out.println(Thread.currentThread().getName()+" i="+(i+1));
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class RunT2221{
    public RunT2221(){
        final OutClassT2221.Inner  inner=new OutClassT2221.Inner();

        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                inner.method1();
            }
        },"A");

        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                inner.Method2();
            }
        },"B");

        thread.start();
        thread1.start();
    }
}
public class T2221 {
    public static void main(String[] args) {
        RunT2221 runT2221=new RunT2221();
    }
}

在这里插入图片描述

package 对象及变量的并发访问2;

/**
 * 内置类与同步-2
 */
class OutClassT2222{
    static class InnerClass1{
        public void method1(InnerClass2 innerClass2){
            String threadName=Thread.currentThread().getName();
            synchronized (innerClass2){
                System.out.println(threadName+"进入InnerClass1类中的method1方法");
                for(int i=0;i<10;i++){
                    System.out.println("i="+1);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println(threadName+"离开InnerClass类中的method1方法");
            }
        }

        public synchronized void method2(){
            String threadName=Thread.currentThread().getName();
            System.out.println(threadName+"进入InnerClass类中方法method2");
            for(int j=0;j<10;j++){
                System.out.println("j="+j);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(threadName +"离开InnerClass类中的方法method2");
        }
    }

    static class InnerClass2{
        public synchronized void method1(){
            String threadName=Thread.currentThread().getName();
            System.out.println(threadName+"进入InnerClass2类中的方法");
            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");
        }
    }
}

class RunT2222{
    public RunT2222(){
        final OutClassT2222.InnerClass1 class1=new OutClassT2222.InnerClass1();
        final OutClassT2222.InnerClass2 class2=new OutClassT2222.InnerClass2();
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                class1.method1(class2);
            }
        },"T1");
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                class1.method2();
            }
        },"T2");
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                class2.method1();
            }
        },"T3");

        thread.start();
        thread1.start();
        thread2.start();
    }
}
public class T2222 {
    public static void main(String[] args) {
        RunT2222 runT2222=new RunT2222();
    }
}

在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页