[CareerCup] 16.6 Synchronized Method 同步方法
2016-04-17 12:17
471 查看
16.6 You are given a class with synchronized method A and a normal method B. If you have two threads in one instance of a program, can they both execute A at the same time? Can they execute A and B at the same time?
当我们给一个方法加了synchronized关键字,我们确保了两个线程不能同时执行同一个对象的该方法。所以对于第一问的答案是根据情况而定,如果两个线程有对象的同一个实例,那么答案是不行,它们不能同时执行方法A,但是当它们有对象的不同实例,那么它们就可以。我们可以用锁的概念来理解,一个synchronized方法就是在该实例对象上加了锁的方法,这样就阻止了其他线程执行该实例的其他synchronized方法。
对于第二部分,由于方法B并没有synchronized关键字,所以当线程2运行方法B时不会有东西阻碍线程1运行方法A,而且不论线程1和2是否有相同的实例对象。总而言之,需要牢记的是,只有一个同步synchronized方法可以在对象中的一个实例中执行,其他线程可以执行该实例的非同步non-synchronized方法,或者他们可以执行该对象的其他实例的任何方法。
CareerCup All in One 题目汇总
当我们给一个方法加了synchronized关键字,我们确保了两个线程不能同时执行同一个对象的该方法。所以对于第一问的答案是根据情况而定,如果两个线程有对象的同一个实例,那么答案是不行,它们不能同时执行方法A,但是当它们有对象的不同实例,那么它们就可以。我们可以用锁的概念来理解,一个synchronized方法就是在该实例对象上加了锁的方法,这样就阻止了其他线程执行该实例的其他synchronized方法。
对于第二部分,由于方法B并没有synchronized关键字,所以当线程2运行方法B时不会有东西阻碍线程1运行方法A,而且不论线程1和2是否有相同的实例对象。总而言之,需要牢记的是,只有一个同步synchronized方法可以在对象中的一个实例中执行,其他线程可以执行该实例的非同步non-synchronized方法,或者他们可以执行该对象的其他实例的任何方法。
public class Foo { private String name; public Foo(String nm) { name = nm; } public String getName() { return name; } public void pause() { try { Thread.sleep(1000 * 3); } catch(InterruptedException e) { e.printStackTrace(); } } public synchronized void methodA (String threadName) { System.out.println("thread" + threadName + " starting; " + name + ".methodA()"); pause(); System.out.println("thread" + threadName + " ending: " + name + ".methodA()"); } public void methodB(String threadName) { System.out.println("thread " + threadName + " starting: " + name + ".methodB()"); pause(); System.out.println("thread " + threadName + " ending: " + name + ".methodB()"); } } public class MyThread extends Thread { private Foo foo; public String name; public String firstMethod; public MyThread(Foo f, String nm, String fM) { foo = f; name = nm; firstMethod = fM; } public void run() { if (firstMethod.equals("A")) { foo.methodA(name); } else { foo.methodB(name); } } } public class j { public static void main(String[] args) { System.out.println("Part 1 Demo with same instance."); Foo fooA = new Foo("ObjectOne"); MyThread thread1a = new MyThread(fooA, "Dog", "A"); MyThread thread2a = new MyThread(fooA, "Cat", "A"); thread1a.start(); thread2a.start(); while (thread1a.isAlive() || thread2a.isAlive()) {} System.out.println("\n\n"); System.out.println("Part 1 Demo with different instance."); Foo fooB1 = new Foo("ObjectOne"); Foo fooB2 = new Foo("ObejctTwo"); MyThread thread1b = new MyThread(fooB1, "Dog", "A"); MyThread thread2b = new MyThread(fooB1, "Cat", "A"); thread1b.start(); thread2b.start(); while (thread1b.isAlive() || thread2b.isAlive()) {}; System.out.println("\n\n"); System.out.println("Part 2 Demo."); Foo fooC = new Foo("ObjectOne"); MyThread thread1c = new MyThread(fooC, "Dog", "A"); MyThread thread2c = new MyThread(fooC, "Cat", "B"); thread1c.start(); thread2c.start(); } }
CareerCup All in One 题目汇总
相关文章推荐
- C++编程基础笔记章节八
- 第七周项目1-成员函数、友元函数和一般函数有区别
- 20145232 韩文浩 《Java程序设计》第7周学习总结
- html基础开发-jQuery框架基础语法攻略(攻略一)
- Arduino中的数据类型
- A率13折线PCM编码的C语言实现
- 仿微信主界面导航栏图标字体颜色的变化
- python 多进程处理 multiprocessing模块
- iOS入门时常用的一些代码段——字典转模型
- centos7 实现跨终端bash命令,vim文本等的粘贴复制
- [2016,CVPR] Learning to Read Chest X-Rays Recurrent Neural Cascade Model for Automated Image Annotat
- Cowardly refusing to `sudo brew install`
- 手把手教你本地安装Chrome应用启动器
- c语言之模拟实现求子串(strstr)函数
- 窥探 Swift 之 函数与闭包的应用实例
- IMF传奇行动第82课:Spark Streaming第一课:案例动手实战并在电光石火间理解其工作原理
- Java集合(六):专用集合和遗留类
- MySQL5.6 Replication主从复制(读写分离) 配置完整版
- 垃圾是放错位置的人才
- DiskGenius Disk Manager