当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法?
2015-06-30 14:00
387 查看
分两种情况
1):进入此对象的非同步方法
答案:可以
2):进入此对象的同步方法
答案:不可以
第一种情况原代码
第二种情况原代码
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"); } }
相关文章推荐
- A. Initial Bet(Codeforces Round #273)
- 竞价广告系统-点击率预测和逻辑回归
- 带删除的EditText
- 无行政地域公司(中字头)变更业务一站式办理
- What is Complex Event Processing? (Part 7)
- delphi 基础之二 面向对象概念初步
- PHP垃圾回收机制
- Cannot assign requested address 问题
- WinForm,WPF,SilverLight三者区别和特性
- 《NoSQL精粹》摘要0-前言
- 静态内部类和非静态内部类总结和区分
- 【Java Swing探索之路系列】之四:Java Swing用户界面组件(一)
- org.tinygroup.binarytree-二叉树
- SAT数学知识范围分析
- http://codeforces.com/contest/555/problem/B
- APK反破解之______Android APK 签名比对
- android.view.View
- org.tinygroup.service-服务
- Quartz2D绘图简介
- Linux之文件系统和根文件系统