• 当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法?


    分两种情况

             1):进入此对象的非同步方法

                  答案:可以

             2):进入此对象的同步方法

                 答案:不可以

    第一种情况原代码

    /**
     * 
     */
    package thread;
    
    
    /**
     * @author Administrator
     *
     */
    public class TestClass {
        /**
         * @param args
         */
        public static void main(String[] args) {
            TestClass tc = new TestClass();
            Thread1 t1 = tc.new Thread1(tc);
            t1.start();
            Thread2 t2 = tc.new Thread2(tc);
            t2.start();
            
        }
    
        class Thread1 extends Thread{
            TestClass tc = null;
            public Thread1(TestClass tc) {
                this.tc = tc;
            }
            @Override
            public void run() {
                tc.method1();
            }
        }
        class Thread2 extends Thread{
            TestClass tc = null;
            public Thread2(TestClass tc) {
                this.tc = tc;
            }
            @Override
            public void run() {
                // TODO Auto-generated method stub
                tc.method2();
            }
        }
        
        public synchronized void method1(){
            System.out.println("method1");
            try {
                Thread.sleep(1000*10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        
        }
        public  void method2(){
            System.out.println("method2");
        }
    }

    第二种情况原代码

    /**
     * 
     */
    package thread;
    
    
    /**
     * @author Administrator
     *
     */
    public class TestClass {
        /**
         * @param args
         */
        public static void main(String[] args) {
            TestClass tc = new TestClass();
            Thread1 t1 = tc.new Thread1(tc);
            t1.start();
            Thread2 t2 = tc.new Thread2(tc);
            t2.start();
            
        }
    
        class Thread1 extends Thread{
            TestClass tc = null;
            public Thread1(TestClass tc) {
                this.tc = tc;
            }
            @Override
            public void run() {
                tc.method1();
            }
        }
        class Thread2 extends Thread{
            TestClass tc = null;
            public Thread2(TestClass tc) {
                this.tc = tc;
            }
            @Override
            public void run() {
                // TODO Auto-generated method stub
                tc.method2();
            }
        }
        
        public synchronized void method1(){
            System.out.println("method1");
            try {
                Thread.sleep(1000*10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        public synchronized void method2(){
            System.out.println("method2");
        }
    }
  • 相关阅读:
    Linux中grep命令的12个实践例子
    进程调度函数schedule()分析
    spin_lock、spin_lock_irq、spin_lock_irqsave区别
    耳机接电话挂断功能
    英文月份
    Linux 学习之路:认识shell和bash
    Solution:Cannot pull with rebase: You have unstaged changes in Github
    Selenium Webdriver——操作隐藏的元素(四)
    Selenium Webdriver——操作隐藏的元素(三)switchTo().frame()
    selenium webdriver处理HTML5 的视频播放
  • 原文地址:https://www.cnblogs.com/azhqiang/p/4610251.html
Copyright © 2020-2023  润新知