[CareerCup] 16.6 Synchronized Method 同步方法
2024-10-07 17:20:52
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方法,或者他们可以执行该对象的其他实例的任何方法。
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();
}
}
最新文章
- 在不损坏C盘的情况下为C盘扩容,适用于Win
- js实现div层缓慢收缩与展开的方法
- 使用eclipse开发的兼容性配置
- [原]使用node-mapnik和openstreetmap数据初步搭建瓦片服务
- innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size
- python比较两个列表
- Python打印格式化与字符串
- 安卓MonkeyRunner源码分析之工作原理架构图及系列集合
- nmon安装(转)
- Python概述与安装
- RSA,DES,RC4,3DES ,MD5
- ASP.NET CORE系列【三】使用Entity Framework Core进行增删改查
- PyCharm 开发Django ,错误汇总
- JAVA005-基本数据类型变量的存储
- 20175310 MyCP(课下作业,必做)
- linux 系统进程理解
- TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)
- Wireshark 显示域名列
- smrtlink
- 洛谷P2458 保安站岗